use of org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer in project spring-integration by spring-projects.
the class TcpNioConnectionReadTests method testReadCrLf.
@SuppressWarnings("unchecked")
@Test
public void testReadCrLf() throws Exception {
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
final List<Message<?>> responses = new ArrayList<Message<?>>();
final Semaphore semaphore = new Semaphore(0);
AbstractServerConnectionFactory scf = getConnectionFactory(serializer, message -> {
responses.add(message);
semaphore.release();
return false;
});
// Fire up the sender.
CountDownLatch done = SocketTestUtils.testSendCrLf(scf.getPort(), latch);
latch.countDown();
assertTrue(semaphore.tryAcquire(1, 10000, TimeUnit.MILLISECONDS));
assertTrue(semaphore.tryAcquire(1, 10000, TimeUnit.MILLISECONDS));
assertEquals("Did not receive data", 2, responses.size());
assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, new String(((Message<byte[]>) responses.get(0)).getPayload()));
assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING, new String(((Message<byte[]>) responses.get(1)).getPayload()));
scf.stop();
done.countDown();
}
use of org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer in project spring-integration by spring-projects.
the class TcpNioConnectionWriteTests method testWriteCrLf.
@Test
public void testWriteCrLf() 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 {
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
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() + 2];
readFully(is, buff);
assertEquals(testString, new String(buff, 0, testString.length()));
assertEquals('\r', buff[testString.length()]);
assertEquals('\n', buff[testString.length() + 1]);
server.close();
latch.countDown();
}
use of org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer in project spring-integration by spring-projects.
the class SocketSupportTests method testNioClientAndServerSSLDifferentContextsLargeDataWithReply.
@Test
public void testNioClientAndServerSSLDifferentContextsLargeDataWithReply() throws Exception {
// SSL activity in the console
System.setProperty("javax.net.debug", "all");
TcpNioServerConnectionFactory server = new TcpNioServerConnectionFactory(0);
TcpSSLContextSupport serverSslContextSupport = new DefaultTcpSSLContextSupport("server.ks", "server.truststore.ks", "secret", "secret");
DefaultTcpNioSSLConnectionSupport serverTcpNioConnectionSupport = new DefaultTcpNioSSLConnectionSupport(serverSslContextSupport);
server.setTcpNioConnectionSupport(serverTcpNioConnectionSupport);
final List<Message<?>> messages = new ArrayList<Message<?>>();
final CountDownLatch latch = new CountDownLatch(2);
final Replier replier = new Replier();
server.registerSender(replier);
server.registerListener(message -> {
messages.add(message);
try {
replier.send(message);
} catch (Exception e) {
throw new RuntimeException(e);
}
latch.countDown();
return false;
});
ByteArrayCrLfSerializer deserializer = new ByteArrayCrLfSerializer();
deserializer.setMaxMessageSize(120000);
server.setDeserializer(deserializer);
final AtomicReference<String> serverConnectionId = new AtomicReference<>();
server.setApplicationEventPublisher(e -> {
if (e instanceof TcpConnectionOpenEvent) {
serverConnectionId.set(((TcpConnectionEvent) e).getConnectionId());
}
});
server.start();
TestingUtilities.waitListening(server, null);
TcpNioClientConnectionFactory client = new TcpNioClientConnectionFactory("localhost", server.getPort());
TcpSSLContextSupport clientSslContextSupport = new DefaultTcpSSLContextSupport("client.ks", "client.truststore.ks", "secret", "secret");
DefaultTcpNioSSLConnectionSupport clientTcpNioConnectionSupport = new DefaultTcpNioSSLConnectionSupport(clientSslContextSupport);
client.setTcpNioConnectionSupport(clientTcpNioConnectionSupport);
client.registerListener(message -> {
messages.add(message);
latch.countDown();
return false;
});
client.setDeserializer(deserializer);
client.setApplicationEventPublisher(e -> {
});
client.start();
TcpConnection connection = client.getConnection();
assertEquals(30, TestUtils.getPropertyValue(connection, "handshakeTimeout"));
byte[] bytes = new byte[100000];
connection.send(new GenericMessage<String>("Hello, world!" + new String(bytes)));
assertTrue(latch.await(60, TimeUnit.SECONDS));
byte[] payload = (byte[]) messages.get(0).getPayload();
assertEquals(13 + bytes.length, payload.length);
assertEquals("Hello, world!", new String(payload).substring(0, 13));
payload = (byte[]) messages.get(1).getPayload();
assertEquals(13 + bytes.length, payload.length);
assertEquals("Hello, world!", new String(payload).substring(0, 13));
Map<?, ?> connections = TestUtils.getPropertyValue(server, "connections", Map.class);
Object serverConnection = connections.get(serverConnectionId.get());
assertNotNull(serverConnection);
assertEquals(30, TestUtils.getPropertyValue(serverConnection, "handshakeTimeout"));
client.stop();
server.stop();
}
use of org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer in project spring-integration by spring-projects.
the class TcpSendingMessageHandlerTests method testNioSingleUseNoInbound.
@Test
public void testNioSingleUseNoInbound() 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 = 0; i < 2; i++) {
Socket socket = server.accept();
semaphore.release();
byte[] b = new byte[8];
readFully(socket.getInputStream(), b);
semaphore.release();
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(5000);
ccf.start();
ccf.setSingleUse(true);
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
handler.setConnectionFactory(ccf);
handler.handleMessage(MessageBuilder.withPayload("Test.1").build());
handler.handleMessage(MessageBuilder.withPayload("Test.2").build());
assertTrue(semaphore.tryAcquire(4, 10000, TimeUnit.MILLISECONDS));
done.set(true);
ccf.stop();
serverSocket.get().close();
}
use of org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer in project spring-integration by spring-projects.
the class TcpReceivingChannelAdapterTests method testNioSingleShared.
@Test
public void testNioSingleShared() throws Exception {
TcpNioServerConnectionFactory scf = new TcpNioServerConnectionFactory(0);
noopPublisher(scf);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
scf.setSerializer(serializer);
scf.setDeserializer(serializer);
scf.setSingleUse(true);
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
handler.setConnectionFactory(scf);
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setConnectionFactory(scf);
scf.start();
QueueChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
TestingUtilities.waitListening(scf, null);
int port = scf.getPort();
Socket socket1 = SocketFactory.getDefault().createSocket("localhost", port);
socket1.setSoTimeout(2000);
socket1.getOutputStream().write("Test1\r\n".getBytes());
Socket socket2 = SocketFactory.getDefault().createSocket("localhost", port);
socket2.setSoTimeout(2000);
socket2.getOutputStream().write("Test2\r\n".getBytes());
Message<?> message = channel.receive(10000);
assertNotNull(message);
handler.handleMessage(message);
message = channel.receive(10000);
assertNotNull(message);
handler.handleMessage(message);
byte[] b = new byte[7];
readFully(socket1.getInputStream(), b);
assertEquals("Test1\r\n", new String(b));
readFully(socket2.getInputStream(), b);
assertEquals("Test2\r\n", new String(b));
scf.stop();
}
Aggregations