Example usage for org.springframework.integration.ip.tcp.connection AbstractClientConnectionFactory setApplicationEventPublisher

List of usage examples for org.springframework.integration.ip.tcp.connection AbstractClientConnectionFactory setApplicationEventPublisher

Introduction

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

Prototype

@Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) 

Source Link

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//from   w w  w  .  j  av a  2s  . co  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();
}

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

@Test
public void testOutboundGatewayNoConnectionEvents() {
    TcpOutboundGateway gw = new TcpOutboundGateway();
    AbstractClientConnectionFactory ccf = new AbstractClientConnectionFactory("localhost", 0) {
    };/*  w ww .  ja v a  2s. co  m*/
    final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<ApplicationEvent>();
    ccf.setApplicationEventPublisher(new ApplicationEventPublisher() {

        @Override
        public void publishEvent(Object event) {
        }

        @Override
        public void publishEvent(ApplicationEvent event) {
            theEvent.set(event);
        }

    });
    gw.setConnectionFactory(ccf);
    DirectChannel requestChannel = new DirectChannel();
    requestChannel
            .subscribe(message -> ((MessageChannel) message.getHeaders().getReplyChannel()).send(message));
    gw.start();
    Message<String> message = MessageBuilder.withPayload("foo").setHeader(IpHeaders.CONNECTION_ID, "bar")
            .build();
    gw.onMessage(message);
    assertNotNull(theEvent.get());
    TcpConnectionFailedCorrelationEvent event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
    assertEquals("bar", event.getConnectionId());
    MessagingException messagingException = (MessagingException) event.getCause();
    assertSame(message, messagingException.getFailedMessage());
    assertEquals("Cannot correlate response - no pending reply for bar", messagingException.getMessage());

    message = new GenericMessage<String>("foo");
    gw.onMessage(message);
    assertNotNull(theEvent.get());
    event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
    assertNull(event.getConnectionId());
    messagingException = (MessagingException) event.getCause();
    assertSame(message, messagingException.getFailedMessage());
    assertEquals("Cannot correlate response - no connection id", messagingException.getMessage());
}