Search in sources :

Example 11 with DefaultSerializer

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

Example 12 with DefaultSerializer

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

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

Example 14 with DefaultSerializer

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

the class TcpOutboundGatewayTests method testFailoverGWPropagatesSocketClose.

@Test
public void testFailoverGWPropagatesSocketClose() throws Exception {
    ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0);
    final int port = serverSocket.getLocalPort();
    AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port);
    ccf.setSerializer(new DefaultSerializer());
    ccf.setDeserializer(new DefaultDeserializer());
    ccf.setSoTimeout(10000);
    ccf.setSingleUse(false);
    FailoverClientConnectionFactory focf = new FailoverClientConnectionFactory(Collections.singletonList(ccf));
    focf.start();
    testGWPropagatesSocketCloseGuts(port, focf, serverSocket);
    serverSocket.close();
}
Also used : AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) ServerSocket(java.net.ServerSocket) FailoverClientConnectionFactory(org.springframework.integration.ip.tcp.connection.FailoverClientConnectionFactory) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 15 with DefaultSerializer

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

the class TcpOutboundGatewayTests method testGoodNetTimeout.

@Test
public void testGoodNetTimeout() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<>();
    this.executor.execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
            serverSocket.set(server);
            latch.countDown();
            int i = 0;
            Socket socket = server.accept();
            while (true) {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                ois.readObject();
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                Thread.sleep(1000);
                oos.writeObject("Reply" + (i++));
            }
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    ccf.setSerializer(new DefaultSerializer());
    ccf.setDeserializer(new DefaultDeserializer());
    ccf.setSoTimeout(10000);
    ccf.setSingleUse(false);
    ccf.start();
    final TcpOutboundGateway gateway = new TcpOutboundGateway();
    gateway.setConnectionFactory(ccf);
    gateway.setRequestTimeout(1);
    QueueChannel replyChannel = new QueueChannel();
    gateway.setRequiresReply(true);
    gateway.setOutputChannel(replyChannel);
    @SuppressWarnings("unchecked") Future<Integer>[] results = (Future<Integer>[]) new Future<?>[2];
    for (int i = 0; i < 2; i++) {
        final int j = i;
        results[j] = (this.executor.submit(() -> {
            gateway.handleMessage(MessageBuilder.withPayload("Test" + j).build());
            return 0;
        }));
    }
    Set<String> replies = new HashSet<>();
    int timeouts = 0;
    for (int i = 0; i < 2; i++) {
        try {
            results[i].get();
        } catch (ExecutionException e) {
            if (timeouts > 0) {
                fail("Unexpected " + e.getMessage());
            } else {
                assertNotNull(e.getCause());
                assertTrue(e.getCause() instanceof MessageTimeoutException);
            }
            timeouts++;
            continue;
        }
        Message<?> m = replyChannel.receive(10000);
        assertNotNull(m);
        replies.add((String) m.getPayload());
    }
    if (timeouts < 1) {
        fail("Expected ExecutionException");
    }
    for (int i = 0; i < 1; i++) {
        assertTrue(replies.remove("Reply" + i));
    }
    done.set(true);
    gateway.stop();
    ccf.stop();
    serverSocket.get().close();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) ObjectOutputStream(java.io.ObjectOutputStream) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) DefaultSerializer(org.springframework.core.serializer.DefaultSerializer) DefaultDeserializer(org.springframework.core.serializer.DefaultDeserializer) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) EOFException(java.io.EOFException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Future(java.util.concurrent.Future) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ObjectInputStream(java.io.ObjectInputStream) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) 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