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);
}
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);
}
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();
}
}
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);
}
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;
}
Aggregations