Example usage for org.springframework.integration.ip.tcp.connection TcpConnectionSupport isOpen

List of usage examples for org.springframework.integration.ip.tcp.connection TcpConnectionSupport isOpen

Introduction

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

Prototype

boolean isOpen();

Source Link

Usage

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

public CachingClientConnectionFactory(AbstractClientConnectionFactory target, int poolSize) {
    super("", 0);
    // override single-use to true to force "close" after use
    target.setSingleUse(true);//from  w  w  w  .j  a  va 2s  . c o  m
    this.targetConnectionFactory = target;
    pool = new SimplePool<TcpConnectionSupport>(poolSize,
            new SimplePool.PoolItemCallback<TcpConnectionSupport>() {

                public TcpConnectionSupport createForPool() {
                    try {
                        return targetConnectionFactory.getConnection();
                    } catch (Exception e) {
                        throw new MessagingException("Failed to obtain connection", e);
                    }
                }

                public boolean isStale(TcpConnectionSupport connection) {
                    return !connection.isOpen();
                }

                public void removedFromPool(TcpConnectionSupport connection) {
                    connection.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   w w  w  .j a  va2  s . c  o m
        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
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 w w.j a va 2  s.c  om*/
    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 TcpConnectionSupport makeMockConnection(String name, boolean closeOk) {
    TcpConnectionSupport mockConn1 = mock(TcpConnectionSupport.class);
    when(mockConn1.getConnectionId()).thenReturn(name);
    when(mockConn1.toString()).thenReturn(name);
    when(mockConn1.isOpen()).thenReturn(true);
    if (!closeOk) {
        doThrow(new RuntimeException("close() not expected")).when(mockConn1).close();
    }/*from  w ww  . j  av a  2  s .  c om*/
    return mockConn1;
}

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

private TcpConnectionSupport makeMockConnection() {
    TcpConnectionSupport connection = mock(TcpConnectionSupport.class);
    when(connection.isOpen()).thenReturn(true);
    return connection;
}

From source file:org.springframework.integration.ip.tcp.TcpOutboundGatewayTests.java

public TcpConnectionSupport makeMockConnection() {
    TcpConnectionSupport connection = mock(TcpConnectionSupport.class);
    when(connection.isOpen()).thenReturn(true);
    return connection;
}