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

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

Introduction

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

Prototype

@Override
public void registerListener(TcpListener listener) 

Source Link

Document

Delegate TCP Client Connection factories that are used to receive data need a Listener to send the messages to.

Usage

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// www  .  j a  v  a2 s . c  o  m
        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();
}