use of org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer in project spring-integration by spring-projects.
the class TcpReceivingChannelAdapterTests method testNetShared.
@Test
public void testNetShared() throws Exception {
AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0);
noopPublisher(scf);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
scf.setSerializer(serializer);
scf.setDeserializer(serializer);
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 socket = SocketFactory.getDefault().createSocket("localhost", port);
socket.setSoTimeout(2000);
socket.getOutputStream().write("Test\r\n".getBytes());
socket.getOutputStream().write("Test\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[6];
readFully(socket.getInputStream(), b);
assertEquals("Test\r\n", new String(b));
readFully(socket.getInputStream(), b);
assertEquals("Test\r\n", new String(b));
scf.stop();
}
use of org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer in project spring-integration by spring-projects.
the class TcpReceivingChannelAdapterTests method testNetClientMode.
@Test
public void testNetClientMode() throws Exception {
final AtomicReference<ServerSocket> serverSocket = new AtomicReference<>();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
new SimpleAsyncTaskExecutor().execute(() -> {
try {
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0, 10);
serverSocket.set(server);
latch1.countDown();
Socket socket = server.accept();
socket.getOutputStream().write("Test1\r\nTest2\r\n".getBytes());
latch2.await();
socket.close();
server.close();
} catch (Exception e) {
if (!done.get()) {
e.printStackTrace();
}
}
});
assertTrue(latch1.await(10, TimeUnit.SECONDS));
AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
noopPublisher(ccf);
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer();
ccf.setSerializer(serializer);
ccf.setDeserializer(serializer);
ccf.setSoTimeout(Integer.MAX_VALUE);
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setConnectionFactory(ccf);
adapter.setClientMode(true);
QueueChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
adapter.setBeanFactory(mock(BeanFactory.class));
adapter.afterPropertiesSet();
adapter.setRetryInterval(10000);
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(1);
taskScheduler.initialize();
adapter.setTaskScheduler(taskScheduler);
adapter.start();
Message<?> message = channel.receive(10000);
assertNotNull(message);
assertEquals("Test1", new String((byte[]) message.getPayload()));
message = channel.receive(10000);
assertNotNull(message);
assertEquals("Test2", new String((byte[]) message.getPayload()));
adapter.stop();
adapter.start();
adapter.stop();
latch2.countDown();
ccf.stop();
serverSocket.get().close();
}
use of org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer in project spring-integration by spring-projects.
the class CachingClientConnectionFactoryTests method mockedTcpNioConnection.
private TcpConnectionSupport mockedTcpNioConnection() throws Exception {
SocketChannel socketChannel = mock(SocketChannel.class);
new DirectFieldAccessor(socketChannel).setPropertyValue("open", false);
doThrow(new IOException("Foo")).when(socketChannel).write(Mockito.any(ByteBuffer.class));
when(socketChannel.socket()).thenReturn(mock(Socket.class));
TcpNioConnection conn = new TcpNioConnection(socketChannel, false, false, new ApplicationEventPublisher() {
@Override
public void publishEvent(ApplicationEvent event) {
}
@Override
public void publishEvent(Object event) {
}
}, "foo");
conn.setMapper(new TcpMessageMapper());
conn.setSerializer(new ByteArrayCrLfSerializer());
return conn;
}
use of org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer in project spring-integration by spring-projects.
the class CachingClientConnectionFactoryTests method mockedTcpNetConnection.
private TcpConnectionSupport mockedTcpNetConnection() throws IOException {
Socket socket = mock(Socket.class);
// closed when next retrieved
when(socket.isClosed()).thenReturn(true);
OutputStream stream = mock(OutputStream.class);
doThrow(new IOException("Foo")).when(stream).write(any(byte[].class), anyInt(), anyInt());
when(socket.getOutputStream()).thenReturn(stream);
TcpNetConnection conn = new TcpNetConnection(socket, false, false, new ApplicationEventPublisher() {
@Override
public void publishEvent(ApplicationEvent event) {
}
@Override
public void publishEvent(Object event) {
}
}, "foo");
conn.setMapper(new TcpMessageMapper());
conn.setSerializer(new ByteArrayCrLfSerializer());
return conn;
}
use of org.springframework.integration.ip.tcp.serializer.ByteArrayCrLfSerializer in project spring-integration by spring-projects.
the class TcpSendingMessageHandlerTests method testNioSingleUseWithInboundMany.
@Test
public void testNioSingleUseWithInboundMany() 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();
final List<Socket> serverSockets = new ArrayList<Socket>();
this.executor.execute(() -> {
try {
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0, 100);
serverSocket.set(server);
latch.countDown();
for (int i = 0; i < 100; i++) {
final Socket socket = server.accept();
serverSockets.add(socket);
final int j = i;
this.executor.execute(() -> {
semaphore.release();
byte[] b = new byte[9];
try {
readFully(socket.getInputStream(), b);
b = ("Reply" + j + "\r\n").getBytes();
socket.getOutputStream().write(b);
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e2) {
}
}
});
}
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(10000);
ccf.setSingleUse(true);
ccf.setTaskExecutor(this.executor);
ccf.start();
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
handler.setConnectionFactory(ccf);
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setConnectionFactory(ccf);
QueueChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
int i = 0;
try {
for (i = 100; i < 200; i++) {
handler.handleMessage(MessageBuilder.withPayload("Test" + i).build());
}
} catch (Exception e) {
e.printStackTrace();
fail("Exception at " + i);
}
assertTrue(semaphore.tryAcquire(100, 20000, TimeUnit.MILLISECONDS));
Set<String> replies = new HashSet<String>();
for (i = 100; i < 200; i++) {
Message<?> mOut = channel.receive(20000);
assertNotNull(mOut);
replies.add(new String((byte[]) mOut.getPayload()));
}
for (i = 0; i < 100; i++) {
assertTrue("Reply" + i + " missing", replies.remove("Reply" + i));
}
done.set(true);
ccf.stop();
serverSocket.get().close();
}
Aggregations