use of org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer in project spring-integration by spring-projects.
the class TcpNioConnectionReadTests method testReadLengthOverflow.
@Test
public void testReadLengthOverflow() throws Exception {
ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
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.testSendLengthOverflow(scf.getPort());
whileOpen(semaphore, added);
assertEquals(1, added.size());
assertTrue(errorMessageLetch.await(10, TimeUnit.SECONDS));
assertThat(errorMessageRef.get().getMessage(), anyOf(containsString("Message length 2147483647 exceeds max message length: 2048"), containsString("Connection is closed")));
assertTrue(semaphore.tryAcquire(10000, TimeUnit.MILLISECONDS));
assertTrue(removed.size() > 0);
scf.stop();
done.countDown();
}
use of org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer in project spring-integration by spring-projects.
the class TcpNioConnectionWriteTests method testWriteLengthHeaderDirect.
@Test
public void testWriteLengthHeaderDirect() throws Exception {
final String testString = "abcdef";
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
final int port = server.getLocalPort();
server.setSoTimeout(10000);
final CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(() -> {
AbstractConnectionFactory ccf = null;
try {
ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
ccf = getClientConnectionFactory(true, port, serializer);
TcpConnection connection = ccf.getConnection();
connection.send(MessageBuilder.withPayload(testString.getBytes()).build());
latch.await(10, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ccf != null) {
ccf.stop();
}
}
});
t.setDaemon(true);
t.start();
Socket socket = server.accept();
socket.setSoTimeout(5000);
InputStream is = socket.getInputStream();
byte[] buff = new byte[testString.length() + 4];
readFully(is, buff);
ByteBuffer buffer = ByteBuffer.wrap(buff);
assertEquals(testString.length(), buffer.getInt());
assertEquals(testString, new String(buff, 4, testString.length()));
server.close();
latch.countDown();
}
use of org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer in project spring-integration by spring-projects.
the class TcpNioConnectionWriteTests method testWriteLengthHeader.
@Test
public void testWriteLengthHeader() throws Exception {
final String testString = "abcdef";
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
final int port = server.getLocalPort();
server.setSoTimeout(10000);
final CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(() -> {
AbstractConnectionFactory ccf = null;
try {
ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
ccf = getClientConnectionFactory(false, port, serializer);
TcpConnection connection = ccf.getConnection();
connection.send(MessageBuilder.withPayload(testString.getBytes()).build());
latch.await(10, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ccf != null) {
ccf.stop();
}
}
});
t.setDaemon(true);
t.start();
Socket socket = server.accept();
socket.setSoTimeout(5000);
InputStream is = socket.getInputStream();
byte[] buff = new byte[testString.length() + 4];
readFully(is, buff);
ByteBuffer buffer = ByteBuffer.wrap(buff);
assertEquals(testString.length(), buffer.getInt());
assertEquals(testString, new String(buff, 4, testString.length()));
server.close();
latch.countDown();
}
use of org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer 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