Example usage for org.springframework.integration.ip.tcp.connection AbstractConnectionFactory setSoTimeout

List of usage examples for org.springframework.integration.ip.tcp.connection AbstractConnectionFactory setSoTimeout

Introduction

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

Prototype

public void setSoTimeout(int soTimeout) 

Source Link

Usage

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

@Test
public void testNioNegotiateSingleNoListen() throws Exception {
    final int port = SocketUtils.findAvailableServerSocket();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    Executors.newSingleThreadExecutor().execute(new Runnable() {
        public void run() {
            int i = 0;
            try {
                ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(port);
                latch.countDown();/*  w  w  w.  j  av a  2  s.  c  om*/
                Socket socket = server.accept();
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Object in = null;
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                if (i == 0) {
                    in = ois.readObject();
                    logger.debug("read object: " + in);
                    oos.writeObject("world!");
                    ois = new ObjectInputStream(socket.getInputStream());
                    oos = new ObjectOutputStream(socket.getOutputStream());
                    in = ois.readObject();
                    logger.debug("read object: " + in);
                    oos.writeObject("world!");
                    ois = new ObjectInputStream(socket.getInputStream());
                    oos = new ObjectOutputStream(socket.getOutputStream());
                }
                in = ois.readObject();
                oos.writeObject("Reply" + (++i));
                socket.close();
                server.close();
            } catch (Exception e) {
                if (i == 0) {
                    e.printStackTrace();
                }
            }
        }
    });
    AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
    ccf.setSerializer(new DefaultSerializer());
    ccf.setDeserializer(new DefaultDeserializer());
    ccf.setSoTimeout(10000);
    TcpConnectionInterceptorFactoryChain fc = new TcpConnectionInterceptorFactoryChain();
    fc.setInterceptors(new TcpConnectionInterceptorFactory[] { new HelloWorldInterceptorFactory(),
            new HelloWorldInterceptorFactory() });
    ccf.setInterceptorFactoryChain(fc);
    ccf.setSingleUse(true);
    ccf.start();
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(ccf);
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    handler.handleMessage(MessageBuilder.withPayload("Test").build());
    done.set(true);
    ccf.stop();
}