use of org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory in project spring-integration by spring-projects.
the class TcpConnectionFactoryFactoryBean method createInstance.
@Override
protected AbstractConnectionFactory createInstance() throws Exception {
if (!this.mapperSet) {
this.mapper.setBeanFactory(this.beanFactory);
}
if (this.usingNio) {
if (isServer()) {
TcpNioServerConnectionFactory connectionFactory = new TcpNioServerConnectionFactory(this.port);
this.setCommonAttributes(connectionFactory);
this.setServerAttributes(connectionFactory);
connectionFactory.setUsingDirectBuffers(this.usingDirectBuffers);
connectionFactory.setTcpNioConnectionSupport(this.obtainNioConnectionSupport());
this.connectionFactory = connectionFactory;
} else {
TcpNioClientConnectionFactory connectionFactory = new TcpNioClientConnectionFactory(this.host, this.port);
this.setCommonAttributes(connectionFactory);
connectionFactory.setUsingDirectBuffers(this.usingDirectBuffers);
connectionFactory.setTcpNioConnectionSupport(this.obtainNioConnectionSupport());
this.connectionFactory = connectionFactory;
}
if (this.sslHandshakeTimeout != null) {
this.connectionFactory.setSslHandshakeTimeout(this.sslHandshakeTimeout);
}
} else {
if (isServer()) {
TcpNetServerConnectionFactory connectionFactory = new TcpNetServerConnectionFactory(this.port);
this.setCommonAttributes(connectionFactory);
this.setServerAttributes(connectionFactory);
connectionFactory.setTcpSocketFactorySupport(this.obtainSocketFactorySupport());
connectionFactory.setTcpNetConnectionSupport(this.obtainNetConnectionSupport());
this.connectionFactory = connectionFactory;
} else {
TcpNetClientConnectionFactory connectionFactory = new TcpNetClientConnectionFactory(this.host, this.port);
this.setCommonAttributes(connectionFactory);
connectionFactory.setTcpSocketFactorySupport(this.obtainSocketFactorySupport());
connectionFactory.setTcpNetConnectionSupport(this.obtainNetConnectionSupport());
this.connectionFactory = connectionFactory;
}
}
return this.connectionFactory;
}
use of org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory in project spring-integration by spring-projects.
the class TcpOutboundGatewayTests method testNioGWPropagatesSocketTimeout.
@Test
public void testNioGWPropagatesSocketTimeout() throws Exception {
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0);
final int port = serverSocket.getLocalPort();
AbstractClientConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", port);
ccf.setSerializer(new DefaultSerializer());
ccf.setDeserializer(new DefaultDeserializer());
ccf.setSoTimeout(100);
ccf.setSingleUse(false);
ccf.start();
testGWPropagatesSocketTimeoutGuts(port, ccf, serverSocket);
serverSocket.close();
}
use of org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory in project spring-integration by spring-projects.
the class TcpSendingMessageHandlerTests method testNioSingleUseWithInbound.
@Test
public void testNioSingleUseWithInbound() 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 = 1; i < 3; i++) {
Socket socket = server.accept();
semaphore.release();
byte[] b = new byte[6];
readFully(socket.getInputStream(), b);
b = ("Reply" + i + "\r\n").getBytes();
socket.getOutputStream().write(b);
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(10000);
ccf.start();
ccf.setSingleUse(true);
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
handler.setConnectionFactory(ccf);
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setConnectionFactory(ccf);
QueueChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
handler.handleMessage(MessageBuilder.withPayload("Test").build());
handler.handleMessage(MessageBuilder.withPayload("Test").build());
assertTrue(semaphore.tryAcquire(2, 10000, TimeUnit.MILLISECONDS));
Set<String> replies = new HashSet<String>();
for (int i = 0; i < 2; i++) {
Message<?> mOut = channel.receive(10000);
assertNotNull(mOut);
replies.add(new String((byte[]) mOut.getPayload()));
}
assertTrue(replies.remove("Reply1"));
assertTrue(replies.remove("Reply2"));
done.set(true);
ccf.stop();
serverSocket.get().close();
}
use of org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory in project spring-integration by spring-projects.
the class TcpSendingMessageHandlerTests method testNioCrLf.
@Test
public void testNioCrLf() throws Exception {
final AtomicReference<ServerSocket> serverSocket = new AtomicReference<ServerSocket>();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
this.executor.execute(() -> {
try {
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
serverSocket.set(server);
latch.countDown();
Socket socket = server.accept();
int i = 0;
while (true) {
byte[] b = new byte[6];
readFully(socket.getInputStream(), b);
b = ("Reply" + (++i) + "\r\n").getBytes();
socket.getOutputStream().write(b);
}
} 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.start();
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
handler.setConnectionFactory(ccf);
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setConnectionFactory(ccf);
QueueChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
handler.handleMessage(MessageBuilder.withPayload("Test").build());
handler.handleMessage(MessageBuilder.withPayload("Test").build());
Set<String> results = new HashSet<String>();
Message<?> mOut = channel.receive(10000);
assertNotNull(mOut);
results.add(new String((byte[]) mOut.getPayload()));
mOut = channel.receive(10000);
assertNotNull(mOut);
results.add(new String((byte[]) mOut.getPayload()));
assertTrue(results.remove("Reply1"));
assertTrue(results.remove("Reply2"));
done.set(true);
ccf.stop();
serverSocket.get().close();
}
use of org.springframework.integration.ip.tcp.connection.TcpNioClientConnectionFactory in project spring-integration by spring-projects.
the class TcpSendingMessageHandlerTests method testNioLength.
@Test
public void testNioLength() throws Exception {
final AtomicReference<ServerSocket> serverSocket = new AtomicReference<ServerSocket>();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
this.executor.execute(() -> {
try {
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0);
serverSocket.set(server);
latch.countDown();
Socket socket = server.accept();
int i = 0;
while (true) {
byte[] b = new byte[8];
readFully(socket.getInputStream(), b);
if (!"\u0000\u0000\u0000\u0004Test".equals(new String(b))) {
throw new RuntimeException("Bad Data");
}
b = ("\u0000\u0000\u0000\u0006Reply" + (++i)).getBytes();
socket.getOutputStream().write(b);
}
} catch (Exception e) {
if (!done.get()) {
e.printStackTrace();
}
}
});
assertTrue(latch.await(10, TimeUnit.SECONDS));
AbstractConnectionFactory ccf = new TcpNioClientConnectionFactory("localhost", serverSocket.get().getLocalPort());
noopPublisher(ccf);
ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer();
ccf.setSerializer(serializer);
ccf.setDeserializer(serializer);
ccf.setSoTimeout(10000);
ccf.start();
TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
handler.setConnectionFactory(ccf);
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
adapter.setConnectionFactory(ccf);
QueueChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
handler.handleMessage(MessageBuilder.withPayload("Test").build());
handler.handleMessage(MessageBuilder.withPayload("Test").build());
Set<String> results = new HashSet<String>();
Message<?> mOut = channel.receive(10000);
assertNotNull(mOut);
results.add(new String((byte[]) mOut.getPayload()));
mOut = channel.receive(10000);
assertNotNull(mOut);
results.add(new String((byte[]) mOut.getPayload()));
assertTrue(results.remove("Reply1"));
assertTrue(results.remove("Reply2"));
done.set(true);
ccf.stop();
serverSocket.get().close();
}
Aggregations