Search in sources :

Example 1 with AbstractConnectionFactory

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

the class TcpSendingMessageHandler method publishNoConnectionEvent.

private void publishNoConnectionEvent(MessageHandlingException messageHandlingException, String connectionId) {
    AbstractConnectionFactory cf = this.serverConnectionFactory != null ? this.serverConnectionFactory : this.clientConnectionFactory;
    ApplicationEventPublisher applicationEventPublisher = cf.getApplicationEventPublisher();
    if (applicationEventPublisher != null) {
        applicationEventPublisher.publishEvent(new TcpConnectionFailedCorrelationEvent(this, connectionId, messageHandlingException));
    }
}
Also used : TcpConnectionFailedCorrelationEvent(org.springframework.integration.ip.tcp.connection.TcpConnectionFailedCorrelationEvent) AbstractConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)

Example 2 with AbstractConnectionFactory

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

the class TcpSendingMessageHandlerTests method testNioSingleUseWithInbound.

@Test
public void testNioSingleUseWithInbound() throws Exception {
    final AtomicReference<ServerSocket> serverSocket = new AtomicReference<ServerSocket>();
    final CountDownLatch latch = new CountDownLatch(1);
    final Semaphore semaphore = new Semaphore(0);
    final AtomicBoolean done = new AtomicBoolean();
    this.executor.execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
            serverSocket.set(server);
            latch.countDown();
            for (int i = 1; i < 3; i++) {
                Socket socket = server.accept();
                semaphore.release();
                byte[] b = new byte[6];
                readFully(socket.getInputStream(), b);
                b = ("Reply" + i + "\r\n").getBytes();
                socket.getOutputStream().write(b);
                socket.close();
            }
            server.close();
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    noopPublisher(ccf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    ccf.setSerializer(serializer);
    ccf.setDeserializer(serializer);
    ccf.setSoTimeout(10000);
    ccf.start();
    ccf.setSingleUse(true);
    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());
    assertTrue(semaphore.tryAcquire(2, 10000, TimeUnit.MILLISECONDS));
    Set<String> replies = new HashSet<String>();
    for (int i = 0; i < 2; i++) {
        Message<?> mOut = channel.receive(10000);
        assertNotNull(mOut);
        replies.add(new String((byte[]) mOut.getPayload()));
    }
    assertTrue(replies.remove("Reply1"));
    assertTrue(replies.remove("Reply2"));
    done.set(true);
    ccf.stop();
    serverSocket.get().close();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) AbstractConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory) QueueChannel(org.springframework.integration.channel.QueueChannel) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) TcpNioClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory) MessagingException(org.springframework.messaging.MessagingException) SocketException(java.net.SocketException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with AbstractConnectionFactory

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

the class TcpSendingMessageHandlerTests method testNioCrLf.

@Test
public void testNioCrLf() 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) {
                byte[] b = new byte[6];
                readFully(socket.getInputStream(), b);
                b = ("Reply" + (++i) + "\r\n").getBytes();
                socket.getOutputStream().write(b);
            }
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    noopPublisher(ccf);
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    ccf.setSerializer(serializer);
    ccf.setDeserializer(serializer);
    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());
    Set<String> results = new HashSet<String>();
    Message<?> mOut = channel.receive(10000);
    assertNotNull(mOut);
    results.add(new String((byte[]) mOut.getPayload()));
    mOut = channel.receive(10000);
    assertNotNull(mOut);
    results.add(new String((byte[]) mOut.getPayload()));
    assertTrue(results.remove("Reply1"));
    assertTrue(results.remove("Reply2"));
    done.set(true);
    ccf.stop();
    serverSocket.get().close();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) 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) TcpNioClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory) MessagingException(org.springframework.messaging.MessagingException) SocketException(java.net.SocketException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with AbstractConnectionFactory

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

the class TcpSendingMessageHandlerTests method testNioLength.

@Test
public void testNioLength() 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) {
                byte[] b = new byte[8];
                readFully(socket.getInputStream(), b);
                if (!"\u0000\u0000\u0000\u0004Test".equals(new String(b))) {
                    throw new RuntimeException("Bad Data");
                }
                b = ("\u0000\u0000\u0000\u0006Reply" + (++i)).getBytes();
                socket.getOutputStream().write(b);
            }
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
    noopPublisher(ccf);
    ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
    ccf.setSerializer(serializer);
    ccf.setDeserializer(serializer);
    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());
    Set<String> results = new HashSet<String>();
    Message<?> mOut = channel.receive(10000);
    assertNotNull(mOut);
    results.add(new String((byte[]) mOut.getPayload()));
    mOut = channel.receive(10000);
    assertNotNull(mOut);
    results.add(new String((byte[]) mOut.getPayload()));
    assertTrue(results.remove("Reply1"));
    assertTrue(results.remove("Reply2"));
    done.set(true);
    ccf.stop();
    serverSocket.get().close();
}
Also used : AbstractConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory) QueueChannel(org.springframework.integration.channel.QueueChannel) ByteArrayLengthHeaderSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer) ServerSocket(java.net.ServerSocket) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TcpNioClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory) MessagingException(org.springframework.messaging.MessagingException) SocketException(java.net.SocketException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with AbstractConnectionFactory

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

the class TcpSendingMessageHandlerTests method testConnectionException.

@Test
public void testConnectionException() throws Exception {
    TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
    AbstractConnectionFactory mockCcf = mock(AbstractClientConnectionFactory.class);
    Mockito.doAnswer(invocation -> {
        throw new SocketException("Failed to connect");
    }).when(mockCcf).getConnection();
    handler.setConnectionFactory(mockCcf);
    try {
        handler.handleMessage(new GenericMessage<String>("foo"));
        fail("Expected exception");
    } catch (Exception e) {
        assertTrue(e instanceof MessagingException);
        assertTrue(e.getCause() != null);
        assertTrue(e.getCause() instanceof SocketException);
        assertEquals("Failed to connect", e.getCause().getMessage());
    }
}
Also used : SocketException(java.net.SocketException) AbstractConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory) MessagingException(org.springframework.messaging.MessagingException) MessagingException(org.springframework.messaging.MessagingException) SocketException(java.net.SocketException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

AbstractConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory)21 MessagingException (org.springframework.messaging.MessagingException)20 IOException (java.io.IOException)19 SocketException (java.net.SocketException)19 Test (org.junit.Test)19 ServerSocket (java.net.ServerSocket)18 Socket (java.net.Socket)18 CountDownLatch (java.util.concurrent.CountDownLatch)18 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)18 QueueChannel (org.springframework.integration.channel.QueueChannel)14 TcpNetClientConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory)9 TcpNioClientConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory)9 ByteArrayCrLfSerializer (org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer)8 HashSet (java.util.HashSet)7 ObjectInputStream (java.io.ObjectInputStream)6 ObjectOutputStream (java.io.ObjectOutputStream)6 DefaultDeserializer (org.springframework.core.serializer.DefaultDeserializer)6 DefaultSerializer (org.springframework.core.serializer.DefaultSerializer)6 Semaphore (java.util.concurrent.Semaphore)5