Search in sources :

Example 1 with CompositeExecutor

use of org.springframework.integration.util.CompositeExecutor in project spring-integration by spring-projects.

the class TcpNioConnectionTests method compositeExecutor.

private CompositeExecutor compositeExecutor() {
    ThreadPoolTaskExecutor ioExec = new ThreadPoolTaskExecutor();
    ioExec.setCorePoolSize(2);
    ioExec.setMaxPoolSize(4);
    ioExec.setQueueCapacity(0);
    ioExec.setThreadNamePrefix("io-");
    ioExec.setRejectedExecutionHandler(new AbortPolicy());
    ioExec.initialize();
    ThreadPoolTaskExecutor assemblerExec = new ThreadPoolTaskExecutor();
    assemblerExec.setCorePoolSize(2);
    assemblerExec.setMaxPoolSize(5);
    assemblerExec.setQueueCapacity(0);
    assemblerExec.setThreadNamePrefix("assembler-");
    assemblerExec.setRejectedExecutionHandler(new AbortPolicy());
    assemblerExec.initialize();
    return new CompositeExecutor(ioExec, assemblerExec);
}
Also used : CompositeExecutor(org.springframework.integration.util.CompositeExecutor) AbortPolicy(java.util.concurrent.ThreadPoolExecutor.AbortPolicy) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)

Example 2 with CompositeExecutor

use of org.springframework.integration.util.CompositeExecutor 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 3 with CompositeExecutor

use of org.springframework.integration.util.CompositeExecutor in project spring-integration by spring-projects.

the class TcpNioConnection method doRead.

private void doRead() throws Exception {
    if (this.rawBuffer == null) {
        this.rawBuffer = allocate(this.maxMessageSize);
    }
    this.writingLatch = new CountDownLatch(1);
    this.writingToPipe = true;
    try {
        if (this.taskExecutor == null) {
            ExecutorService executor = Executors.newCachedThreadPool();
            this.taskExecutor = new CompositeExecutor(executor, executor);
        }
        // If there is no assembler running, start one
        checkForAssembler();
        if (logger.isTraceEnabled()) {
            logger.trace("Before read:" + this.rawBuffer.position() + "/" + this.rawBuffer.limit());
        }
        int len = this.socketChannel.read(this.rawBuffer);
        if (len < 0) {
            this.writingToPipe = false;
            this.closeConnection(true);
        }
        if (logger.isTraceEnabled()) {
            logger.trace("After read:" + this.rawBuffer.position() + "/" + this.rawBuffer.limit());
        }
        this.rawBuffer.flip();
        if (logger.isTraceEnabled()) {
            logger.trace("After flip:" + this.rawBuffer.position() + "/" + this.rawBuffer.limit());
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Read " + this.rawBuffer.limit() + " into raw buffer");
        }
        this.sendToPipe(this.rawBuffer);
    } catch (RejectedExecutionException e) {
        throw e;
    } catch (Exception e) {
        this.publishConnectionExceptionEvent(e);
        throw e;
    } finally {
        this.writingToPipe = false;
        this.writingLatch.countDown();
    }
}
Also used : CompositeExecutor(org.springframework.integration.util.CompositeExecutor) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) MessagingException(org.springframework.messaging.MessagingException) SoftEndOfStreamException(org.springframework.integration.ip.tcp.serializer.SoftEndOfStreamException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) SocketTimeoutException(java.net.SocketTimeoutException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException)

Example 4 with CompositeExecutor

use of org.springframework.integration.util.CompositeExecutor in project spring-integration by spring-projects.

the class TcpNioConnectionTests method testAssemblerUsesSecondaryExecutor.

@Test
public void testAssemblerUsesSecondaryExecutor() throws Exception {
    TcpNioServerConnectionFactory factory = new TcpNioServerConnectionFactory(0);
    factory.setApplicationEventPublisher(nullPublisher);
    CompositeExecutor compositeExec = compositeExecutor();
    factory.setSoTimeout(1000);
    factory.setTaskExecutor(compositeExec);
    final AtomicReference<String> threadName = new AtomicReference<String>();
    final CountDownLatch latch = new CountDownLatch(1);
    factory.registerListener(new TcpListener() {

        @Override
        public boolean onMessage(Message<?> message) {
            if (!(message instanceof ErrorMessage)) {
                threadName.set(Thread.currentThread().getName());
                latch.countDown();
            }
            return false;
        }
    });
    factory.start();
    TestingUtilities.waitListening(factory, null);
    int port = factory.getPort();
    Socket socket = null;
    int n = 0;
    while (n++ < 100) {
        try {
            socket = SocketFactory.getDefault().createSocket("localhost", port);
            break;
        } catch (ConnectException e) {
        }
        Thread.sleep(100);
    }
    assertTrue("Could not open socket to localhost:" + port, n < 100);
    socket.getOutputStream().write("foo\r\n".getBytes());
    socket.close();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertThat(threadName.get(), containsString("assembler"));
    factory.stop();
    cleanupCompositeExecutor(compositeExec);
}
Also used : CompositeExecutor(org.springframework.integration.util.CompositeExecutor) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) 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 5 with CompositeExecutor

use of org.springframework.integration.util.CompositeExecutor in project faf-java-server by FAForever.

the class LegacyAdapterConfig method tcpServerConnectionFactory.

/**
 * Non-blocking TCP connection factory that deserializes into byte array messages.
 */
@Bean
public TcpNioServerConnectionFactory tcpServerConnectionFactory() {
    ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
    serializer.setMaxMessageSize(100 * 1024);
    serializer.setApplicationEventPublisher(applicationEventPublisher);
    TcpNioServerConnectionFactory connectionFactory = new TcpNioServerConnectionFactory(serverProperties.getPort());
    connectionFactory.setDeserializer(serializer);
    connectionFactory.setSerializer(serializer);
    connectionFactory.setUsingDirectBuffers(true);
    connectionFactory.getMapper().setApplySequence(true);
    // See https://docs.spring.io/spring-integration/reference/html/ip.html#_thread_pool_task_executor_with_caller_runs_policy
    connectionFactory.setTaskExecutor(new CompositeExecutor(createNioTaskExecutor("legacy-io-"), createNioTaskExecutor("legacy-assembler-")));
    return connectionFactory;
}
Also used : CompositeExecutor(org.springframework.integration.util.CompositeExecutor) TcpNioServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory) ByteArrayLengthHeaderSerializer(org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer) Bean(org.springframework.context.annotation.Bean)

Aggregations

CompositeExecutor (org.springframework.integration.util.CompositeExecutor)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 ConnectException (java.net.ConnectException)2 ServerSocket (java.net.ServerSocket)2 Socket (java.net.Socket)2 Test (org.junit.Test)2 ErrorMessage (org.springframework.messaging.support.ErrorMessage)2 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 ExecutorService (java.util.concurrent.ExecutorService)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 AbortPolicy (java.util.concurrent.ThreadPoolExecutor.AbortPolicy)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Bean (org.springframework.context.annotation.Bean)1 TcpNioServerConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory)1 ByteArrayLengthHeaderSerializer (org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer)1 SoftEndOfStreamException (org.springframework.integration.ip.tcp.serializer.SoftEndOfStreamException)1 MessagingException (org.springframework.messaging.MessagingException)1