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

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

Introduction

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

Prototype

@Override
    public void start() 

Source Link

Usage

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

@Test
public void testReuse() throws Exception {
    AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
    when(factory.isRunning()).thenReturn(true);
    TcpConnectionSupport mockConn1 = makeMockConnection("conn1");
    TcpConnectionSupport mockConn2 = makeMockConnection("conn2");
    when(factory.getConnection()).thenReturn(mockConn1).thenReturn(mockConn2);
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(factory, 2);
    cachingFactory.start();
    TcpConnection conn1 = cachingFactory.getConnection();
    // INT-3652//from   w  ww  .  ja v a 2  s .  c o m
    TcpConnectionInterceptorSupport cachedConn1 = (TcpConnectionInterceptorSupport) conn1;
    Log logger = spy(TestUtils.getPropertyValue(cachedConn1, "logger", Log.class));
    when(logger.isDebugEnabled()).thenReturn(true);
    new DirectFieldAccessor(cachedConn1).setPropertyValue("logger", logger);
    cachedConn1.onMessage(new ErrorMessage(new RuntimeException()));
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    verify(logger).debug(captor.capture());
    assertThat(captor.getValue(), startsWith("Message discarded; no listener:"));
    // end INT-3652
    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());
    conn1.close();
    conn2.close();
}

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

@Test
public void testReuseNoLimit() throws Exception {
    AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
    when(factory.isRunning()).thenReturn(true);
    TcpConnectionSupport mockConn1 = makeMockConnection("conn1");
    TcpConnectionSupport mockConn2 = makeMockConnection("conn2");
    when(factory.getConnection()).thenReturn(mockConn1).thenReturn(mockConn2);
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(factory, 0);
    cachingFactory.start();
    TcpConnection conn1 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    conn1.close();/*from   w w w .  j  a va 2s.c o  m*/
    conn1 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    TcpConnection conn2 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn2.toString(), conn2.toString());
    conn1.close();
    conn2.close();
}

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

@Test
public void testReuseClosed() throws Exception {
    AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
    when(factory.isRunning()).thenReturn(true);
    TcpConnectionSupport mockConn1 = makeMockConnection("conn1");
    TcpConnectionSupport mockConn2 = makeMockConnection("conn2");
    doAnswer(new Answer<Object>() {

        @Override/*from   ww  w. j av a2 s  .com*/
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return null;
        }

    }).when(mockConn1).close();
    when(factory.getConnection()).thenReturn(mockConn1).thenReturn(mockConn2).thenReturn(mockConn1)
            .thenReturn(mockConn2);
    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());
    conn1.close();
    conn2.close();
    when(mockConn1.isOpen()).thenReturn(false);
    TcpConnection conn2a = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn2.toString(), conn2a.toString());
    assertSame(TestUtils.getPropertyValue(conn2, "theConnection"),
            TestUtils.getPropertyValue(conn2a, "theConnection"));
    conn2a.close();
}

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

@Test(expected = MessagingException.class)
public void testLimit() throws Exception {
    AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
    when(factory.isRunning()).thenReturn(true);
    TcpConnectionSupport mockConn1 = makeMockConnection("conn1");
    TcpConnectionSupport mockConn2 = makeMockConnection("conn2");
    when(factory.getConnection()).thenReturn(mockConn1).thenReturn(mockConn2);
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(factory, 2);
    cachingFactory.setConnectionWaitTimeout(10);
    cachingFactory.start();
    TcpConnection conn1 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    conn1.close();// w w  w .  ja  va  2 s.  co m
    conn1 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn1.toString(), conn1.toString());
    TcpConnection conn2 = cachingFactory.getConnection();
    assertEquals("Cached:" + mockConn2.toString(), conn2.toString());
    cachingFactory.getConnection();
}

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;/*w  w w  . j a  va2 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

@Test
public void testEnlargePool() throws Exception {
    AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
    when(factory.isRunning()).thenReturn(true);
    TcpConnectionSupport mockConn1 = makeMockConnection("conn1");
    TcpConnectionSupport mockConn2 = makeMockConnection("conn2");
    TcpConnectionSupport mockConn3 = makeMockConnection("conn3");
    TcpConnectionSupport mockConn4 = makeMockConnection("conn4");
    when(factory.getConnection()).thenReturn(mockConn1, mockConn2, mockConn3, mockConn4);
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(factory, 2);
    cachingFactory.start();
    TcpConnection conn1 = cachingFactory.getConnection();
    TcpConnection conn2 = cachingFactory.getConnection();
    assertNotSame(conn1, conn2);//from w  w w  . j av a 2  s  . c o m
    Semaphore semaphore = TestUtils.getPropertyValue(TestUtils.getPropertyValue(cachingFactory, "pool"),
            "permits", Semaphore.class);
    assertEquals(0, semaphore.availablePermits());
    cachingFactory.setPoolSize(4);
    TcpConnection conn3 = cachingFactory.getConnection();
    TcpConnection conn4 = cachingFactory.getConnection();
    assertEquals(0, semaphore.availablePermits());
    conn1.close();
    conn1.close();
    conn2.close();
    conn3.close();
    conn4.close();
    assertEquals(4, semaphore.availablePermits());
}

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

@Test
public void testReducePool() throws Exception {
    AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
    when(factory.isRunning()).thenReturn(true);
    TcpConnectionSupport mockConn1 = makeMockConnection("conn1", true);
    TcpConnectionSupport mockConn2 = makeMockConnection("conn2", true);
    TcpConnectionSupport mockConn3 = makeMockConnection("conn3", true);
    TcpConnectionSupport mockConn4 = makeMockConnection("conn4", true);
    when(factory.getConnection()).thenReturn(mockConn1).thenReturn(mockConn2).thenReturn(mockConn3)
            .thenReturn(mockConn4);//from w  ww . jav  a2 s .c  o  m
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(factory, 4);
    cachingFactory.start();
    TcpConnection conn1 = cachingFactory.getConnection();
    TcpConnection conn2 = cachingFactory.getConnection();
    TcpConnection conn3 = cachingFactory.getConnection();
    TcpConnection conn4 = cachingFactory.getConnection();
    Semaphore semaphore = TestUtils.getPropertyValue(TestUtils.getPropertyValue(cachingFactory, "pool"),
            "permits", Semaphore.class);
    assertEquals(0, semaphore.availablePermits());
    conn1.close();
    assertEquals(1, semaphore.availablePermits());
    cachingFactory.setPoolSize(2);
    assertEquals(0, semaphore.availablePermits());
    assertEquals(3, cachingFactory.getActiveCount());
    conn2.close();
    assertEquals(0, semaphore.availablePermits());
    assertEquals(2, cachingFactory.getActiveCount());
    conn3.close();
    assertEquals(1, cachingFactory.getActiveCount());
    assertEquals(1, cachingFactory.getIdleCount());
    conn4.close();
    assertEquals(2, semaphore.availablePermits());
    assertEquals(0, cachingFactory.getActiveCount());
    assertEquals(2, cachingFactory.getIdleCount());
    verify(mockConn1).close();
    verify(mockConn2).close();
}

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

private CachingClientConnectionFactory createCCCFWith2Connections(TcpConnectionSupport conn1,
        TcpConnectionSupport conn2) throws Exception {
    AbstractClientConnectionFactory factory = mock(AbstractClientConnectionFactory.class);
    when(factory.isRunning()).thenReturn(true);
    when(factory.getConnection()).thenReturn(conn1, conn2);
    CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(factory, 1);
    cccf.setConnectionWaitTimeout(1);//  ww  w . j ava 2 s.com
    cccf.start();
    return cccf;
}

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

private void testCloseOnTimeoutGuts(AbstractClientConnectionFactory cf) throws Exception {
    cf.setSoTimeout(100);/*ww w.ja  v a2s .co 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
public void testCachedFailover() throws Exception {
    // Failover//from  w  w  w .j  a v a2s . co  m
    AbstractClientConnectionFactory factory1 = mock(AbstractClientConnectionFactory.class);
    AbstractClientConnectionFactory factory2 = mock(AbstractClientConnectionFactory.class);
    List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
    factories.add(factory1);
    factories.add(factory2);
    TcpConnectionSupport mockConn1 = makeMockConnection();
    TcpConnectionSupport mockConn2 = makeMockConnection();
    when(factory1.getConnection()).thenReturn(mockConn1);
    when(factory2.getConnection()).thenReturn(mockConn2);
    when(factory1.isActive()).thenReturn(true);
    when(factory2.isActive()).thenReturn(true);
    doThrow(new IOException("fail")).when(mockConn1).send(Mockito.any(Message.class));
    doAnswer(invocation -> null).when(mockConn2).send(Mockito.any(Message.class));
    FailoverClientConnectionFactory failoverFactory = new FailoverClientConnectionFactory(factories);
    failoverFactory.start();

    // Cache
    CachingClientConnectionFactory cachingFactory = new CachingClientConnectionFactory(failoverFactory, 2);
    cachingFactory.start();
    TcpConnection conn1 = cachingFactory.getConnection();
    GenericMessage<String> message = new GenericMessage<String>("foo");
    conn1 = cachingFactory.getConnection();
    conn1.send(message);
    Mockito.verify(mockConn2).send(message);
}