use of org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory in project spring-integration by spring-projects.
the class TcpSyslogReceivingChannelAdapter method onInit.
@Override
protected void onInit() {
super.onInit();
if (this.connectionFactory == null) {
this.connectionFactory = new TcpNioServerConnectionFactory(getPort());
this.connectionFactory.setDeserializer(new ByteArrayLfSerializer());
this.connectionFactory.setBeanFactory(getBeanFactory());
if (this.applicationEventPublisher != null) {
this.connectionFactory.setApplicationEventPublisher(this.applicationEventPublisher);
}
this.connectionFactory.afterPropertiesSet();
}
this.connectionFactory.registerListener(this);
}
use of org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory in project spring-integration by spring-projects.
the class TcpInboundGatewayTests method testNioNotSingle.
@Test
public void testNioNotSingle() throws Exception {
AbstractServerConnectionFactory scf = new TcpNioServerConnectionFactory(0);
scf.setSingleUse(false);
TcpInboundGateway gateway = new TcpInboundGateway();
gateway.setConnectionFactory(scf);
scf.start();
TestingUtilities.waitListening(scf, 20000L);
int port = scf.getPort();
final QueueChannel channel = new QueueChannel();
gateway.setRequestChannel(channel);
gateway.setBeanFactory(mock(BeanFactory.class));
ServiceActivatingHandler handler = new ServiceActivatingHandler(new Service());
Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
socket.getOutputStream().write("Test1\r\n".getBytes());
socket.getOutputStream().write("Test2\r\n".getBytes());
handler.handleMessage(channel.receive(10000));
handler.handleMessage(channel.receive(10000));
Set<String> results = new HashSet<String>();
byte[] bytes = new byte[12];
readFully(socket.getInputStream(), bytes);
results.add(new String(bytes));
readFully(socket.getInputStream(), bytes);
results.add(new String(bytes));
assertTrue(results.remove("Echo:Test1\r\n"));
assertTrue(results.remove("Echo:Test2\r\n"));
}
use of org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory 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();
}
use of org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory in project spring-integration by spring-projects.
the class TcpReceivingChannelAdapterTests method testNioSingleSharedInterceptors.
@Test
public void testNioSingleSharedInterceptors() throws Exception {
AbstractServerConnectionFactory scf = new TcpNioServerConnectionFactory(0);
noopPublisher(scf);
singleSharedInterceptorsGuts(scf);
scf.stop();
}
use of org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory in project spring-integration by spring-projects.
the class DeserializationTests method testTimeoutWhileDecoding.
public void testTimeoutWhileDecoding(AbstractByteArraySerializer deserializer, String reply) throws Exception {
ByteArrayRawSerializer serializer = new ByteArrayRawSerializer();
TcpNioServerConnectionFactory serverNio = new TcpNioServerConnectionFactory(0);
ByteArrayLengthHeaderSerializer lengthHeaderSerializer = new ByteArrayLengthHeaderSerializer(1);
serverNio.setDeserializer(lengthHeaderSerializer);
serverNio.setSerializer(serializer);
serverNio.afterPropertiesSet();
TcpInboundGateway in = new TcpInboundGateway();
in.setConnectionFactory(serverNio);
QueueChannel serverSideChannel = new QueueChannel();
in.setRequestChannel(serverSideChannel);
in.setBeanFactory(mock(BeanFactory.class));
in.afterPropertiesSet();
in.start();
TestingUtilities.waitListening(serverNio, null);
TcpNioClientConnectionFactory clientNio = new TcpNioClientConnectionFactory("localhost", serverNio.getPort());
clientNio.setSerializer(serializer);
clientNio.setDeserializer(deserializer);
clientNio.setSoTimeout(1000);
clientNio.afterPropertiesSet();
final TcpOutboundGateway out = new TcpOutboundGateway();
out.setConnectionFactory(clientNio);
QueueChannel outputChannel = new QueueChannel();
out.setOutputChannel(outputChannel);
out.setRemoteTimeout(60000);
out.setBeanFactory(mock(BeanFactory.class));
out.afterPropertiesSet();
out.start();
Runnable command = () -> {
try {
out.handleMessage(MessageBuilder.withPayload("\u0004Test").build());
} catch (Exception e) {
// eat SocketTimeoutException. Doesn't matter for this test
}
};
Executor exec = new SimpleAsyncTaskExecutor();
Message<?> message;
// short reply should not be received.
exec.execute(command);
message = serverSideChannel.receive(10000);
assertNotNull(message);
assertEquals("Test", new String((byte[]) message.getPayload()));
String shortReply = reply.substring(0, reply.length() - 1);
((MessageChannel) message.getHeaders().getReplyChannel()).send(new GenericMessage<String>(shortReply));
message = outputChannel.receive(6000);
assertNull(message);
// good message should be received
if ((deserializer instanceof ByteArrayRawSerializer)) {
// restore old behavior
clientNio.setDeserializer(new ByteArrayRawSerializer(true));
}
exec.execute(command);
message = serverSideChannel.receive(10000);
assertNotNull(message);
assertEquals("Test", new String((byte[]) message.getPayload()));
((MessageChannel) message.getHeaders().getReplyChannel()).send(new GenericMessage<String>(reply));
message = outputChannel.receive(10000);
assertNotNull(message);
assertEquals(reply, new String(((byte[]) message.getPayload())));
}
Aggregations