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

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

Introduction

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

Prototype

public abstract void run();

Source Link

Document

When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.

Usage

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

@Test
public void transferHeaders() throws Exception {
    Socket inSocket = mock(Socket.class);
    PipedInputStream pipe = new PipedInputStream();
    when(inSocket.getInputStream()).thenReturn(pipe);

    TcpConnectionSupport inboundConnection = new TcpNetConnection(inSocket, true, false, nullPublisher, null);
    inboundConnection.setDeserializer(new MapJsonSerializer());
    MapMessageConverter inConverter = new MapMessageConverter();
    MessageConvertingTcpMessageMapper inMapper = new MessageConvertingTcpMessageMapper(inConverter);
    inboundConnection.setMapper(inMapper);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Socket outSocket = mock(Socket.class);
    TcpNetConnection outboundConnection = new TcpNetConnection(outSocket, true, false, nullPublisher, null);
    when(outSocket.getOutputStream()).thenReturn(baos);

    MapMessageConverter outConverter = new MapMessageConverter();
    outConverter.setHeaderNames("bar");
    MessageConvertingTcpMessageMapper outMapper = new MessageConvertingTcpMessageMapper(outConverter);
    outboundConnection.setMapper(outMapper);
    outboundConnection.setSerializer(new MapJsonSerializer());

    Message<String> message = MessageBuilder.withPayload("foo").setHeader("bar", "baz").build();
    outboundConnection.send(message);// w  w  w .  ja  va  2s  .com
    PipedOutputStream out = new PipedOutputStream(pipe);
    out.write(baos.toByteArray());
    out.close();

    final AtomicReference<Message<?>> inboundMessage = new AtomicReference<Message<?>>();
    TcpListener listener = new TcpListener() {

        public boolean onMessage(Message<?> message) {
            if (!(message instanceof ErrorMessage)) {
                inboundMessage.set(message);
            }
            return false;
        }
    };
    inboundConnection.registerListener(listener);
    inboundConnection.run();
    assertNotNull(inboundMessage.get());
    assertEquals("foo", inboundMessage.get().getPayload());
    assertEquals("baz", inboundMessage.get().getHeaders().get("bar"));
}