Example usage for org.springframework.integration.ip.tcp.connection CachingClientConnectionFactory stop

List of usage examples for org.springframework.integration.ip.tcp.connection CachingClientConnectionFactory stop

Introduction

In this page you can find the example usage for org.springframework.integration.ip.tcp.connection CachingClientConnectionFactory stop.

Prototype

@Override
    public synchronized void stop() 

Source Link

Usage

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test
public void testStop() throws Exception {
    AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
    when(factory.isRunning()).thenReturn(true);
    TcpConnectionSupport mockConn1 = makeMockConnection("conn1");
    TcpConnectionSupport mockConn2 = makeMockConnection("conn2");
    int i = 3;/*from   w ww .j a  v  a2 s.  com*/
    when(factory.getConnection()).thenReturn(mockConn1).thenReturn(mockConn2)
            .thenReturn(makeMockConnection("conn" + (i++)));
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(factory, 2);
    cachingFactory.start();
    TcpConnection conn1 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    conn1.close();
    conn1 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    TcpConnection conn2 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn2.toString(), conn2.toString());
    cachingFactory.stop();
    Answer<Object> answer = new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return null;
        }

    };
    doAnswer(answer).when(mockConn1).close();
    doAnswer(answer).when(mockConn2).close();
    when(factory.isRunning()).thenReturn(false);
    conn1.close();
    conn2.close();
    verify(mockConn1).close();
    verify(mockConn2).close();
    when(mockConn1.isOpen()).thenReturn(false);
    when(mockConn2.isOpen()).thenReturn(false);
    when(factory.isRunning()).thenReturn(true);
    TcpConnection conn3 = cachingFactory.getConnection();
    assertNotSame(TestUtils.getPropertyValue(conn1, "theConnection"),
            TestUtils.getPropertyValue(conn3, "theConnection"));
    assertNotSame(TestUtils.getPropertyValue(conn2, "theConnection"),
            TestUtils.getPropertyValue(conn3, "theConnection"));
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

private void testCloseOnTimeoutGuts(AbstractClientConnectionFactory cf) throws Exception {
    cf.setSoTimeout(100);//from w  w w.j  a  v a 2 s. c o  m
    CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(cf, 1);
    cccf.start();
    TcpConnection connection = cccf.getConnection();
    int n = 0;
    while (n++ < 100 && connection.isOpen()) {
        Thread.sleep(100);
    }
    assertFalse(connection.isOpen());
    cccf.stop();
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test //INT-3650
public void testRealConnection() throws Exception {
    TcpNetServerConnectionFactory in = new TcpNetServerConnectionFactory(0);
    final CountDownLatch latch1 = new CountDownLatch(2);
    final CountDownLatch latch2 = new CountDownLatch(102);
    final List<String> connectionIds = new ArrayList<String>();
    in.registerListener(message -> {//  w  w  w . j a v  a2s .co  m
        connectionIds.add((String) message.getHeaders().get(IpHeaders.CONNECTION_ID));
        latch1.countDown();
        latch2.countDown();
        return false;
    });
    in.start();
    TestingUtilities.waitListening(in, null);
    int port = in.getPort();
    TcpNetClientConnectionFactory out = new TcpNetClientConnectionFactory("localhost", port);
    CachingClientConnectionFactory cache = new CachingClientConnectionFactory(out, 1);
    cache.setSingleUse(false);
    cache.setConnectionWaitTimeout(100);
    cache.start();
    TcpConnectionSupport connection1 = cache.getConnection();
    connection1.send(new GenericMessage<String>("foo"));
    connection1.close();
    TcpConnectionSupport connection2 = cache.getConnection();
    connection2.send(new GenericMessage<String>("foo"));
    connection2.close();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    assertSame(connectionIds.get(0), connectionIds.get(1));
    for (int i = 0; i < 100; i++) {
        TcpConnectionSupport connection = cache.getConnection();
        connection.send(new GenericMessage<String>("foo"));
        connection.close();
    }
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertSame(connectionIds.get(0), connectionIds.get(101));
    in.stop();
    cache.stop();
}

From source file:org.springframework.integration.ip.tcp.connection.CachingClientConnectionFactoryTests.java

@Test // INT-3728
public void testEarlyReceive() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AbstractClientConnectionFactory factory = new TcpNetClientConnectionFactory("", 0) {

        @Override/*from w w w . jav a 2 s.c  om*/
        protected Socket createSocket(String host, int port) throws IOException {
            Socket mock = mock(Socket.class);
            when(mock.getInputStream()).thenReturn(new ByteArrayInputStream("foo\r\n".getBytes()));
            return mock;
        }

        @Override
        public boolean isActive() {
            return true;
        }

    };
    factory.setApplicationEventPublisher(mock(ApplicationEventPublisher.class));
    final CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(factory, 1);
    final AtomicReference<Message<?>> received = new AtomicReference<Message<?>>();
    cachingFactory.registerListener(message -> {
        if (!(message instanceof ErrorMessage)) {
            received.set(message);
            latch.countDown();
        }
        return false;
    });
    cachingFactory.start();

    cachingFactory.getConnection();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertNotNull(received.get());
    assertNotNull(received.get().getHeaders().get(IpHeaders.ACTUAL_CONNECTION_ID));
    cachingFactory.stop();
}