Search in sources :

Example 6 with DefaultDeserializer

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

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

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

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

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

the class TcpMessageMapperTests method testMapMessageConvertingBothWaysJava.

@Test
public void testMapMessageConvertingBothWaysJava() throws Exception {
    Message<String> outMessage = MessageBuilder.withPayload("foo").setHeader("bar", "baz").build();
    MapMessageConverter converter = new MapMessageConverter();
    converter.setHeaderNames("bar");
    MessageConvertingTcpMessageMapper mapper = new MessageConvertingTcpMessageMapper(converter);
    Map<?, ?> map = (Map<?, ?>) mapper.fromMessage(outMessage);
    DefaultSerializer serializer = new DefaultSerializer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    serializer.serialize(map, baos);
    DefaultDeserializer deserializer = new DefaultDeserializer();
    map = (Map<?, ?>) deserializer.deserialize(new ByteArrayInputStream(baos.toByteArray()));
    TcpConnection connection = mock(TcpConnection.class);
    when(connection.getPayload()).thenReturn(map);
    when(connection.getHostName()).thenReturn("someHost");
    when(connection.getHostAddress()).thenReturn("1.1.1.1");
    when(connection.getPort()).thenReturn(1234);
    when(connection.getConnectionId()).thenReturn("someId");
    Message<?> message = mapper.toMessage(connection);
    assertEquals("foo", message.getPayload());
    assertEquals("baz", message.getHeaders().get("bar"));
    assertEquals("someHost", message.getHeaders().get(IpHeaders.HOSTNAME));
    assertEquals("1.1.1.1", message.getHeaders().get(IpHeaders.IP_ADDRESS));
    assertEquals(1234, message.getHeaders().get(IpHeaders.REMOTE_PORT));
    assertEquals("someId", message.getHeaders().get(IpHeaders.CONNECTION_ID));
}
Also used : DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) ByteArrayInputStream(java.io.ByteArrayInputStream) MapMessageConverter(org.springframework.integration.support.converter.MapMessageConverter) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

DefaultDeserializer (org.springframework.core.serializer.DefaultDeserializer)25 DefaultSerializer (org.springframework.core.serializer.DefaultSerializer)24 ServerSocket (java.net.ServerSocket)23 Test (org.junit.Test)21 Socket (java.net.Socket)15 ObjectInputStream (java.io.ObjectInputStream)14 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)14 CountDownLatch (java.util.concurrent.CountDownLatch)12 QueueChannel (org.springframework.integration.channel.QueueChannel)12 IOException (java.io.IOException)11 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