Search in sources :

Example 1 with TcpConnectionInterceptorFactoryChain

use of org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain in project spring-integration by spring-projects.

the class TcpSendingMessageHandlerTests method testNetNegotiateSingleNoListen.

@Test
public void testNetNegotiateSingleNoListen() throws Exception {
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<ServerSocket>();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    this.executor.execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
            serverSocket.set(server);
            latch.countDown();
            Socket socket = server.accept();
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            Object 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");
            socket.close();
            server.close();
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    AbstractConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    noopPublisher(ccf);
    ccf.setSerializer(new DefaultSerializer());
    ccf.setDeserializer(new DefaultDeserializer());
    ccf.setSoTimeout(10000);
    TcpConnectionInterceptorFactoryChain fc = new TcpConnectionInterceptorFactoryChain();
    fc.setInterceptors(new TcpConnectionInterceptorFactory[] { newInterceptorFactory(), newInterceptorFactory() });
    ccf.setInterceptorFactoryChain(fc);
    ccf.setSingleUse(true);
    ccf.start();
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(ccf);
    handler.handleMessage(MessageBuilder.withPayload("Test").build());
    done.set(true);
    ccf.stop();
    serverSocket.get().close();
}
Also used : AbstractConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ObjectOutputStream(java.io.ObjectOutputStream) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) MessagingException(org.springframework.messaging.MessagingException) SocketException(java.net.SocketException) IOException(java.io.IOException) DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) TcpConnectionInterceptorFactoryChain(org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 2 with TcpConnectionInterceptorFactoryChain

use of org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain in project spring-integration by spring-projects.

the class TcpSendingMessageHandlerTests method testNioNegotiateSingleNoListen.

@Test
public void testNioNegotiateSingleNoListen() throws Exception {
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<ServerSocket>();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    this.executor.execute(() -> {
        int i = 0;
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
            serverSocket.set(server);
            latch.countDown();
            Socket socket = server.accept();
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            Object 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());
            oos.writeObject("Reply" + (++i));
            socket.close();
            server.close();
        } catch (Exception e) {
            if (i == 0) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    noopPublisher(ccf);
    ccf.setSerializer(new DefaultSerializer());
    ccf.setDeserializer(new DefaultDeserializer());
    ccf.setSoTimeout(10000);
    TcpConnectionInterceptorFactoryChain fc = new TcpConnectionInterceptorFactoryChain();
    fc.setInterceptors(new TcpConnectionInterceptorFactory[] { newInterceptorFactory(), newInterceptorFactory() });
    ccf.setInterceptorFactoryChain(fc);
    ccf.setSingleUse(true);
    ccf.start();
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(ccf);
    handler.handleMessage(MessageBuilder.withPayload("Test").build());
    done.set(true);
    ccf.stop();
    serverSocket.get().close();
}
Also used : AbstractConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ObjectOutputStream(java.io.ObjectOutputStream) TcpNioClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory) MessagingException(org.springframework.messaging.MessagingException) SocketException(java.net.SocketException) IOException(java.io.IOException) DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) TcpConnectionInterceptorFactoryChain(org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 3 with TcpConnectionInterceptorFactoryChain

use of org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain in project spring-integration by spring-projects.

the class TcpReceivingChannelAdapterTests method interceptorsGuts.

private void interceptorsGuts(AbstractServerConnectionFactory scf) throws Exception {
    scf.setSerializer(new DefaultSerializer());
    scf.setDeserializer(new DefaultDeserializer());
    scf.setSingleUse(false);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    TcpConnectionInterceptorFactoryChain fc = new TcpConnectionInterceptorFactoryChain();
    fc.setInterceptors(new TcpConnectionInterceptorFactory[] { newInterceptorFactory(), newInterceptorFactory() });
    scf.setInterceptorFactoryChain(fc);
    scf.setSoTimeout(10000);
    scf.start();
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.setSoTimeout(10000);
    new ObjectOutputStream(socket.getOutputStream()).writeObject("Hello");
    assertEquals("world!", new ObjectInputStream(socket.getInputStream()).readObject());
    new ObjectOutputStream(socket.getOutputStream()).writeObject("Hello");
    assertEquals("world!", new ObjectInputStream(socket.getInputStream()).readObject());
    new ObjectOutputStream(socket.getOutputStream()).writeObject("Test1");
    new ObjectOutputStream(socket.getOutputStream()).writeObject("Test2");
    Set<String> results = new HashSet<String>();
    Message<?> message = channel.receive(10000);
    assertNotNull(message);
    results.add((String) message.getPayload());
    message = channel.receive(10000);
    assertNotNull(message);
    results.add((String) message.getPayload());
    assertTrue(results.contains("Test1"));
    assertTrue(results.contains("Test2"));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) ObjectOutputStream(java.io.ObjectOutputStream) DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) TcpConnectionInterceptorFactoryChain(org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ObjectInputStream(java.io.ObjectInputStream) HashSet(java.util.HashSet)

Example 4 with TcpConnectionInterceptorFactoryChain

use of org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain in project spring-integration by spring-projects.

the class TcpReceivingChannelAdapterTests method singleNoOutboundInterceptorsGuts.

private void singleNoOutboundInterceptorsGuts(AbstractServerConnectionFactory scf) throws Exception {
    scf.setSerializer(new DefaultSerializer());
    scf.setDeserializer(new DefaultDeserializer());
    scf.setSingleUse(true);
    scf.setSoTimeout(10000);
    TcpConnectionInterceptorFactoryChain fc = new TcpConnectionInterceptorFactoryChain();
    fc.setInterceptors(new TcpConnectionInterceptorFactory[] { newInterceptorFactory(), newInterceptorFactory() });
    scf.setInterceptorFactoryChain(fc);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    scf.start();
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.setSoTimeout(10000);
    new ObjectOutputStream(socket.getOutputStream()).writeObject("Hello");
    assertEquals("world!", new ObjectInputStream(socket.getInputStream()).readObject());
    new ObjectOutputStream(socket.getOutputStream()).writeObject("Hello");
    assertEquals("world!", new ObjectInputStream(socket.getInputStream()).readObject());
    new ObjectOutputStream(socket.getOutputStream()).writeObject("Test1");
    socket = SocketFactory.getDefault().createSocket("localhost", port);
    new ObjectOutputStream(socket.getOutputStream()).writeObject("Hello");
    assertEquals("world!", new ObjectInputStream(socket.getInputStream()).readObject());
    new ObjectOutputStream(socket.getOutputStream()).writeObject("Hello");
    assertEquals("world!", new ObjectInputStream(socket.getInputStream()).readObject());
    new ObjectOutputStream(socket.getOutputStream()).writeObject("Test2");
    Message<?> message = channel.receive(10000);
    assertNotNull(message);
    // with single use, results may come back in a different order
    Set<Object> results = new HashSet<Object>();
    results.add(message.getPayload());
    message = channel.receive(10000);
    assertNotNull(message);
    results.add(message.getPayload());
    assertTrue(results.contains("Test1"));
    assertTrue(results.contains("Test2"));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) ObjectOutputStream(java.io.ObjectOutputStream) DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) TcpConnectionInterceptorFactoryChain(org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ObjectInputStream(java.io.ObjectInputStream) HashSet(java.util.HashSet)

Example 5 with TcpConnectionInterceptorFactoryChain

use of org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain in project spring-integration by spring-projects.

the class TcpReceivingChannelAdapterTests method singleSharedInterceptorsGuts.

private void singleSharedInterceptorsGuts(AbstractServerConnectionFactory scf) throws Exception {
    scf.setSerializer(new DefaultSerializer());
    scf.setDeserializer(new DefaultDeserializer());
    scf.setSingleUse(true);
    scf.setSoTimeout(60000);
    TcpConnectionInterceptorFactoryChain fc = new TcpConnectionInterceptorFactoryChain();
    fc.setInterceptors(new TcpConnectionInterceptorFactory[] { newInterceptorFactory(), newInterceptorFactory() });
    scf.setInterceptorFactoryChain(fc);
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(scf);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(scf);
    scf.start();
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    TestingUtilities.waitListening(scf, null);
    int port = scf.getPort();
    Socket socket1 = SocketFactory.getDefault().createSocket("localhost", port);
    socket1.setSoTimeout(60000);
    new ObjectOutputStream(socket1.getOutputStream()).writeObject("Hello");
    assertEquals("world!", new ObjectInputStream(socket1.getInputStream()).readObject());
    new ObjectOutputStream(socket1.getOutputStream()).writeObject("Hello");
    assertEquals("world!", new ObjectInputStream(socket1.getInputStream()).readObject());
    new ObjectOutputStream(socket1.getOutputStream()).writeObject("Test1");
    Socket socket2 = SocketFactory.getDefault().createSocket("localhost", port);
    socket2.setSoTimeout(60000);
    new ObjectOutputStream(socket2.getOutputStream()).writeObject("Hello");
    assertEquals("world!", new ObjectInputStream(socket2.getInputStream()).readObject());
    new ObjectOutputStream(socket2.getOutputStream()).writeObject("Hello");
    assertEquals("world!", new ObjectInputStream(socket2.getInputStream()).readObject());
    new ObjectOutputStream(socket2.getOutputStream()).writeObject("Test2");
    Message<?> message = channel.receive(10000);
    assertNotNull(message);
    handler.handleMessage(message);
    message = channel.receive(10000);
    assertNotNull(message);
    handler.handleMessage(message);
    assertEquals("Test1", new ObjectInputStream(socket1.getInputStream()).readObject());
    assertEquals("Test2", new ObjectInputStream(socket2.getInputStream()).readObject());
}
Also used : DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) QueueChannel(org.springframework.integration.channel.QueueChannel) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) TcpConnectionInterceptorFactoryChain(org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain) ObjectOutputStream(java.io.ObjectOutputStream) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ObjectInputStream (java.io.ObjectInputStream)7 ObjectOutputStream (java.io.ObjectOutputStream)7 ServerSocket (java.net.ServerSocket)7 Socket (java.net.Socket)7 DefaultDeserializer (org.springframework.core.serializer.DefaultDeserializer)7 DefaultSerializer (org.springframework.core.serializer.DefaultSerializer)7 TcpConnectionInterceptorFactoryChain (org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain)7 QueueChannel (org.springframework.integration.channel.QueueChannel)5 IOException (java.io.IOException)4 SocketException (java.net.SocketException)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Test (org.junit.Test)4 AbstractConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory)4 MessagingException (org.springframework.messaging.MessagingException)4 HashSet (java.util.HashSet)2 TcpNetClientConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory)2 TcpNioClientConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory)2 TreeSet (java.util.TreeSet)1