Search in sources :

Example 6 with DefaultSerializer

use of org.springframework.core.serializer.DefaultSerializer 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 7 with DefaultSerializer

use of org.springframework.core.serializer.DefaultSerializer 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 8 with DefaultSerializer

use of org.springframework.core.serializer.DefaultSerializer in project spring-integration by spring-projects.

the class TcpSendingMessageHandlerTests method testNetSerial.

@Test
public void testNetSerial() 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();
            int i = 0;
            while (true) {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                ois.readObject();
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                oos.writeObject("Reply" + (++i));
            }
        } 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);
    ccf.start();
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    handler.setConnectionFactory(ccf);
    TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
    adapter.setConnectionFactory(ccf);
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    handler.handleMessage(MessageBuilder.withPayload("Test").build());
    handler.handleMessage(MessageBuilder.withPayload("Test").build());
    Message<?> mOut = channel.receive(10000);
    assertNotNull(mOut);
    assertEquals("Reply1", mOut.getPayload());
    mOut = channel.receive(10000);
    assertNotNull(mOut);
    assertEquals("Reply2", mOut.getPayload());
    done.set(true);
    ccf.stop();
    serverSocket.get().close();
}
Also used : AbstractConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory) QueueChannel(org.springframework.integration.channel.QueueChannel) 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) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 9 with DefaultSerializer

use of org.springframework.core.serializer.DefaultSerializer 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 10 with DefaultSerializer

use of org.springframework.core.serializer.DefaultSerializer in project spring-integration by spring-projects.

the class SerializationTests method testWriteSerialized.

@Test
public void testWriteSerialized() throws Exception {
    final String testString = "abcdef";
    ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
    final int port = server.getLocalPort();
    server.setSoTimeout(10000);
    final CountDownLatch latch = new CountDownLatch(1);
    Thread t = new Thread(() -> {
        try {
            Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
            DefaultSerializer serializer = new DefaultSerializer();
            serializer.serialize(testString, socket.getOutputStream());
            serializer.serialize(testString, socket.getOutputStream());
            latch.await(10, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    t.setDaemon(true);
    t.start();
    Socket socket = server.accept();
    socket.setSoTimeout(5000);
    InputStream is = socket.getInputStream();
    ObjectInputStream ois = new ObjectInputStream(is);
    assertEquals(testString, ois.readObject());
    ois = new ObjectInputStream(is);
    assertEquals(testString, ois.readObject());
    latch.countDown();
    server.close();
}
Also used : DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) ServerSocket(java.net.ServerSocket) CountDownLatch(java.util.concurrent.CountDownLatch) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

DefaultSerializer (org.springframework.core.serializer.DefaultSerializer)25 DefaultDeserializer (org.springframework.core.serializer.DefaultDeserializer)24 ServerSocket (java.net.ServerSocket)23 Test (org.junit.Test)21 ObjectInputStream (java.io.ObjectInputStream)15 Socket (java.net.Socket)15 ObjectOutputStream (java.io.ObjectOutputStream)14 AbstractClientConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory)14 TcpNetClientConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory)14 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)13 IOException (java.io.IOException)12 CountDownLatch (java.util.concurrent.CountDownLatch)12 QueueChannel (org.springframework.integration.channel.QueueChannel)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 TcpConnectionInterceptorFactoryChain (org.springframework.integration.ip.tcp.connection.TcpConnectionInterceptorFactoryChain)7 SocketException (java.net.SocketException)6 HashSet (java.util.HashSet)6 AbstractConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory)6 TcpNioClientConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory)6