use of org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory in project spring-integration by spring-projects.
the class ConnectionFacforyTests method test.
@Test
public void test() throws Exception {
ApplicationEventPublisher publisher = e -> {
};
AbstractServerConnectionFactory server = Tcp.netServer(0).backlog(2).soTimeout(5000).get();
final AtomicReference<Message<?>> received = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
server.registerListener(m -> {
received.set(new ObjectToStringTransformer().transform(m));
latch.countDown();
return false;
});
server.setApplicationEventPublisher(publisher);
server.afterPropertiesSet();
server.start();
TestingUtilities.waitListening(server, null);
AbstractClientConnectionFactory client = Tcp.netClient("localhost", server.getPort()).get();
client.setApplicationEventPublisher(publisher);
client.afterPropertiesSet();
client.start();
client.getConnection().send(new GenericMessage<>("foo"));
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertEquals("foo", received.get().getPayload());
client.stop();
server.stop();
}
use of org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory in project spring-integration by spring-projects.
the class TcpInboundGatewayTests method testNetClientMode.
@Test
public void testNetClientMode() throws Exception {
final AtomicInteger port = new AtomicInteger();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final CountDownLatch latch3 = new CountDownLatch(1);
final AtomicBoolean done = new AtomicBoolean();
new SimpleAsyncTaskExecutor().execute(() -> {
try {
ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0, 10);
port.set(server.getLocalPort());
latch1.countDown();
Socket socket = server.accept();
socket.getOutputStream().write("Test1\r\nTest2\r\n".getBytes());
byte[] bytes = new byte[12];
readFully(socket.getInputStream(), bytes);
assertEquals("Echo:Test1\r\n", new String(bytes));
readFully(socket.getInputStream(), bytes);
assertEquals("Echo:Test2\r\n", new String(bytes));
latch2.await();
socket.close();
server.close();
done.set(true);
latch3.countDown();
} catch (Exception e) {
if (!done.get()) {
e.printStackTrace();
}
}
});
assertTrue(latch1.await(10, TimeUnit.SECONDS));
AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port.get());
ccf.setSingleUse(false);
TcpInboundGateway gateway = new TcpInboundGateway();
gateway.setConnectionFactory(ccf);
final QueueChannel channel = new QueueChannel();
gateway.setRequestChannel(channel);
gateway.setClientMode(true);
gateway.setRetryInterval(10000);
gateway.setBeanFactory(mock(BeanFactory.class));
gateway.afterPropertiesSet();
ServiceActivatingHandler handler = new ServiceActivatingHandler(new Service());
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(1);
taskScheduler.initialize();
gateway.setTaskScheduler(taskScheduler);
gateway.start();
Message<?> message = channel.receive(10000);
assertNotNull(message);
handler.handleMessage(message);
message = channel.receive(10000);
assertNotNull(message);
handler.handleMessage(message);
latch2.countDown();
assertTrue(latch3.await(10, TimeUnit.SECONDS));
assertTrue(done.get());
gateway.stop();
}
use of org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory 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.connection.AbstractClientConnectionFactory in project spring-integration by spring-projects.
the class IpIntegrationTests method testTcpAdapters.
@Test
public void testTcpAdapters() throws Exception {
ApplicationEventPublisher publisher = e -> {
};
AbstractServerConnectionFactory server = Tcp.netServer(0).backlog(2).soTimeout(5000).id("server").get();
assertEquals("server", server.getComponentName());
server.setApplicationEventPublisher(publisher);
server.afterPropertiesSet();
TcpReceivingChannelAdapter inbound = Tcp.inboundAdapter(server).get();
QueueChannel received = new QueueChannel();
inbound.setOutputChannel(received);
inbound.afterPropertiesSet();
inbound.start();
TestingUtilities.waitListening(server, null);
AbstractClientConnectionFactory client = Tcp.netClient("localhost", server.getPort()).id("client").get();
assertEquals("client", client.getComponentName());
client.setApplicationEventPublisher(publisher);
client.afterPropertiesSet();
TcpSendingMessageHandler handler = Tcp.outboundAdapter(client).get();
handler.start();
handler.handleMessage(new GenericMessage<>("foo"));
Message<?> receivedMessage = received.receive(10000);
assertNotNull(receivedMessage);
assertEquals("foo", Transformers.objectToString().transform(receivedMessage).getPayload());
client.stop();
server.stop();
}
use of org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory in project spring-integration by spring-projects.
the class TcpOutboundGatewayTests method testGoodNetGWTimeoutCached.
@Test
public void testGoodNetGWTimeoutCached() throws Exception {
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0);
final int port = serverSocket.getLocalPort();
AbstractClientConnectionFactory ccf = buildCF(port);
CachingClientConnectionFactory cccf = new CachingClientConnectionFactory(ccf, 1);
cccf.start();
testGoodNetGWTimeoutGuts(port, cccf, serverSocket);
serverSocket.close();
}
Aggregations