Search in sources :

Example 16 with ErrorMessage

use of org.springframework.messaging.support.ErrorMessage in project spring-integration by spring-projects.

the class TcpNioConnectionTests method testAllMessagesDelivered.

@Test
public void testAllMessagesDelivered() throws Exception {
    final int numberOfSockets = 10;
    TcpNioServerConnectionFactory factory = new TcpNioServerConnectionFactory(0);
    factory.setApplicationEventPublisher(nullPublisher);
    CompositeExecutor compositeExec = compositeExecutor();
    factory.setTaskExecutor(compositeExec);
    final CountDownLatch latch = new CountDownLatch(numberOfSockets * 4);
    factory.registerListener(message -> {
        if (!(message instanceof ErrorMessage)) {
            latch.countDown();
        }
        return false;
    });
    factory.start();
    TestingUtilities.waitListening(factory, null);
    int port = factory.getPort();
    Socket[] sockets = new Socket[numberOfSockets];
    for (int i = 0; i < numberOfSockets; i++) {
        Socket socket = null;
        int n = 0;
        while (n++ < 100) {
            try {
                socket = SocketFactory.getDefault().createSocket("localhost", port);
                break;
            } catch (ConnectException e) {
            }
            Thread.sleep(1);
        }
        assertTrue("Could not open socket to localhost:" + port, n < 100);
        sockets[i] = socket;
    }
    for (int i = 0; i < numberOfSockets; i++) {
        sockets[i].getOutputStream().write("foo1 and...".getBytes());
        sockets[i].getOutputStream().flush();
    }
    Thread.sleep(1);
    for (int i = 0; i < numberOfSockets; i++) {
        sockets[i].getOutputStream().write(("...foo2\r\nbar1 and...").getBytes());
        sockets[i].getOutputStream().flush();
    }
    for (int i = 0; i < numberOfSockets; i++) {
        sockets[i].getOutputStream().write(("...bar2\r\n").getBytes());
        sockets[i].getOutputStream().flush();
    }
    for (int i = 0; i < numberOfSockets; i++) {
        sockets[i].getOutputStream().write("foo3 and...".getBytes());
        sockets[i].getOutputStream().flush();
    }
    Thread.sleep(1);
    for (int i = 0; i < numberOfSockets; i++) {
        sockets[i].getOutputStream().write(("...foo4\r\nbar3 and...").getBytes());
        sockets[i].getOutputStream().flush();
    }
    for (int i = 0; i < numberOfSockets; i++) {
        sockets[i].getOutputStream().write(("...bar4\r\n").getBytes());
        sockets[i].close();
    }
    assertTrue("latch is still " + latch.getCount(), latch.await(60, TimeUnit.SECONDS));
    factory.stop();
    cleanupCompositeExecutor(compositeExec);
}
Also used : CompositeExecutor(org.springframework.integration.util.CompositeExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorMessage(org.springframework.messaging.support.ErrorMessage) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 17 with ErrorMessage

use of org.springframework.messaging.support.ErrorMessage in project spring-integration by spring-projects.

the class ConnectionTimeoutTests method testNetReplyTimeout.

/**
 * Ensure we do timeout on the read side (client) if we sent a message within the
 * first timeout but the reply takes > 2 timeouts.
 * @throws Exception
 */
@Test
public void testNetReplyTimeout() throws Exception {
    TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory(0);
    this.setupServerCallbacks(server, 4500);
    final AtomicReference<Message<?>> reply = new AtomicReference<Message<?>>();
    server.start();
    TestingUtilities.waitListening(server, null);
    TcpNetClientConnectionFactory client = new TcpNetClientConnectionFactory("localhost", server.getPort());
    client.registerListener(message -> {
        if (!(message instanceof ErrorMessage)) {
            reply.set(message);
        }
        return false;
    });
    client.setSoTimeout(2000);
    CountDownLatch clientCloseLatch = getCloseLatch(client);
    setupClientCallback(client);
    client.start();
    TcpConnection connection = client.getConnection();
    Socket socket = TestUtils.getPropertyValue(connection, "socket", Socket.class);
    assertEquals(2000, socket.getSoTimeout());
    Thread.sleep(1000);
    connection.send(MessageBuilder.withPayload("foo").build());
    Thread.sleep(1400);
    assertTrue(connection.isOpen());
    assertTrue(clientCloseLatch.await(2000, TimeUnit.SECONDS));
    assertNull(reply.get());
    assertFalse(connection.isOpen());
    server.stop();
    client.stop();
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) ErrorMessage(org.springframework.messaging.support.ErrorMessage) CountDownLatch(java.util.concurrent.CountDownLatch) Socket(java.net.Socket) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 18 with ErrorMessage

use of org.springframework.messaging.support.ErrorMessage in project spring-integration by spring-projects.

the class ConnectionTimeoutTests method testNioReplyTimeout.

/**
 * Ensure we do timeout on the read side (client) if we sent a message within the
 * first timeout but the reply takes > 2 timeouts.
 * @throws Exception
 */
@Test
public void testNioReplyTimeout() throws Exception {
    TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory(0);
    this.setupServerCallbacks(server, 2100);
    final AtomicReference<Message<?>> reply = new AtomicReference<Message<?>>();
    server.start();
    TestingUtilities.waitListening(server, null);
    TcpNioClientConnectionFactory client = new TcpNioClientConnectionFactory("localhost", server.getPort());
    client.registerListener(message -> {
        if (!(message instanceof ErrorMessage)) {
            reply.set(message);
        }
        return false;
    });
    client.setSoTimeout(1000);
    CountDownLatch clientCloseLatch = getCloseLatch(client);
    setupClientCallback(client);
    client.start();
    TcpConnection connection = client.getConnection();
    Thread.sleep(500);
    connection.send(MessageBuilder.withPayload("foo").build());
    Thread.sleep(700);
    assertTrue(connection.isOpen());
    assertTrue(clientCloseLatch.await(2, TimeUnit.SECONDS));
    assertNull(reply.get());
    assertFalse(connection.isOpen());
    server.stop();
    client.stop();
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) ErrorMessage(org.springframework.messaging.support.ErrorMessage) CountDownLatch(java.util.concurrent.CountDownLatch) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 19 with ErrorMessage

use of org.springframework.messaging.support.ErrorMessage in project spring-integration by spring-projects.

the class ConnectionTimeoutTests method notTimeoutGuts.

private void notTimeoutGuts(AbstractServerConnectionFactory server, AbstractClientConnectionFactory client) throws Exception, InterruptedException {
    final AtomicReference<Message<?>> reply = new AtomicReference<Message<?>>();
    final CountDownLatch replyLatch = new CountDownLatch(1);
    client.registerListener(message -> {
        if (!(message instanceof ErrorMessage)) {
            reply.set(message);
            replyLatch.countDown();
        }
        return false;
    });
    client.setSoTimeout(2000);
    CountDownLatch clientClosedLatch = getCloseLatch(client);
    setupClientCallback(client);
    client.start();
    TcpConnection connection = client.getConnection();
    Thread.sleep(1000);
    connection.send(MessageBuilder.withPayload("foo").build());
    assertTrue(replyLatch.await(5, TimeUnit.SECONDS));
    assertNotNull(reply.get());
    assertTrue(clientClosedLatch.await(10, TimeUnit.SECONDS));
    assertFalse(connection.isOpen());
    server.stop();
    client.stop();
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorMessage(org.springframework.messaging.support.ErrorMessage)

Example 20 with ErrorMessage

use of org.springframework.messaging.support.ErrorMessage in project spring-integration by spring-projects.

the class TcpNioConnectionReadTests method testReadCrLfOverflow.

@Test
public void testReadCrLfOverflow() throws Exception {
    ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
    serializer.setMaxMessageSize(1024);
    final Semaphore semaphore = new Semaphore(0);
    final List<TcpConnection> added = new ArrayList<TcpConnection>();
    final List<TcpConnection> removed = new ArrayList<TcpConnection>();
    final CountDownLatch errorMessageLetch = new CountDownLatch(1);
    final AtomicReference<Throwable> errorMessageRef = new AtomicReference<Throwable>();
    AbstractServerConnectionFactory scf = getConnectionFactory(serializer, message -> {
        if (message instanceof ErrorMessage) {
            errorMessageRef.set(((ErrorMessage) message).getPayload());
            errorMessageLetch.countDown();
        }
        return false;
    }, new TcpSender() {

        @Override
        public void addNewConnection(TcpConnection connection) {
            added.add(connection);
            semaphore.release();
        }

        @Override
        public void removeDeadConnection(TcpConnection connection) {
            removed.add(connection);
            semaphore.release();
        }
    });
    // Fire up the sender.
    CountDownLatch done = SocketTestUtils.testSendCrLfOverflow(scf.getPort());
    whileOpen(semaphore, added);
    assertEquals(1, added.size());
    assertTrue(errorMessageLetch.await(10, TimeUnit.SECONDS));
    assertThat(errorMessageRef.get().getMessage(), anyOf(containsString("Connection is closed"), containsString("CRLF not found before max message length: 1024")));
    assertTrue(semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS));
    assertTrue(removed.size() > 0);
    scf.stop();
    done.countDown();
}
Also used : ByteArrayCrLfSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorMessage(org.springframework.messaging.support.ErrorMessage) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Aggregations

ErrorMessage (org.springframework.messaging.support.ErrorMessage)59 Test (org.junit.Test)47 CountDownLatch (java.util.concurrent.CountDownLatch)17 GenericMessage (org.springframework.messaging.support.GenericMessage)17 MessagingException (org.springframework.messaging.MessagingException)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 Message (org.springframework.messaging.Message)12 QueueChannel (org.springframework.integration.channel.QueueChannel)11 MessageHandlingException (org.springframework.messaging.MessageHandlingException)11 Socket (java.net.Socket)9 ArrayList (java.util.ArrayList)7 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)7 Semaphore (java.util.concurrent.Semaphore)6 Log (org.apache.commons.logging.Log)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 BeanFactory (org.springframework.beans.factory.BeanFactory)5 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 ServerSocket (java.net.ServerSocket)3