use of org.springframework.integration.ip.tcp.TcpOutboundGateway in project spring-integration by spring-projects.
the class ConnectionEventTests method testOutboundGatewayNoConnectionEvents.
@Test
public void testOutboundGatewayNoConnectionEvents() {
TcpOutboundGateway gw = new TcpOutboundGateway();
AbstractClientConnectionFactory ccf = new AbstractClientConnectionFactory("localhost", 0) {
};
final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<ApplicationEvent>();
ccf.setApplicationEventPublisher(new ApplicationEventPublisher() {
@Override
public void publishEvent(Object event) {
}
@Override
public void publishEvent(ApplicationEvent event) {
theEvent.set(event);
}
});
gw.setConnectionFactory(ccf);
DirectChannel requestChannel = new DirectChannel();
requestChannel.subscribe(message -> ((MessageChannel) message.getHeaders().getReplyChannel()).send(message));
gw.start();
Message<String> message = MessageBuilder.withPayload("foo").setHeader(IpHeaders.CONNECTION_ID, "bar").build();
gw.onMessage(message);
assertNotNull(theEvent.get());
TcpConnectionFailedCorrelationEvent event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
assertEquals("bar", event.getConnectionId());
MessagingException messagingException = (MessagingException) event.getCause();
assertSame(message, messagingException.getFailedMessage());
assertEquals("Cannot correlate response - no pending reply for bar", messagingException.getMessage());
message = new GenericMessage<String>("foo");
gw.onMessage(message);
assertNotNull(theEvent.get());
event = (TcpConnectionFailedCorrelationEvent) theEvent.get();
assertNull(event.getConnectionId());
messagingException = (MessagingException) event.getCause();
assertSame(message, messagingException.getFailedMessage());
assertEquals("Cannot correlate response - no connection id", messagingException.getMessage());
gw.stop();
ccf.stop();
}
use of org.springframework.integration.ip.tcp.TcpOutboundGateway in project spring-integration by spring-projects.
the class FailoverClientConnectionFactoryTests method testRealGuts.
private void testRealGuts(AbstractClientConnectionFactory client1, AbstractClientConnectionFactory client2, Holder holder) throws Exception {
int port1 = 0;
int port2 = 0;
client1.setTaskExecutor(holder.exec);
client2.setTaskExecutor(holder.exec);
client1.setBeanName("client1");
client2.setBeanName("client2");
client1.setApplicationEventPublisher(NULL_PUBLISHER);
client2.setApplicationEventPublisher(NULL_PUBLISHER);
List<AbstractClientConnectionFactory> factories = new ArrayList<AbstractClientConnectionFactory>();
factories.add(client1);
factories.add(client2);
FailoverClientConnectionFactory failFactory = new FailoverClientConnectionFactory(factories);
boolean singleUse = client1.isSingleUse();
failFactory.setSingleUse(singleUse);
failFactory.setBeanFactory(mock(BeanFactory.class));
failFactory.afterPropertiesSet();
TcpOutboundGateway outGateway = new TcpOutboundGateway();
outGateway.setConnectionFactory(failFactory);
outGateway.start();
QueueChannel replyChannel = new QueueChannel();
outGateway.setReplyChannel(replyChannel);
Message<String> message = new GenericMessage<String>("foo");
outGateway.setRemoteTimeout(120000);
outGateway.handleMessage(message);
Socket socket = null;
if (!singleUse) {
socket = getSocket(client1);
port1 = socket.getLocalPort();
}
assertTrue(singleUse | holder.connectionId.get().contains(Integer.toString(port1)));
Message<?> replyMessage = replyChannel.receive(10000);
assertNotNull(replyMessage);
holder.server1.stop();
TestingUtilities.waitStopListening(holder.server1, 10000L);
TestingUtilities.waitUntilFactoryHasThisNumberOfConnections(client1, 0);
outGateway.handleMessage(message);
if (!singleUse) {
socket = getSocket(client2);
port2 = socket.getLocalPort();
}
assertTrue(singleUse | holder.connectionId.get().contains(Integer.toString(port2)));
replyMessage = replyChannel.receive(10000);
assertNotNull(replyMessage);
holder.gateway2.stop();
outGateway.stop();
}
use of org.springframework.integration.ip.tcp.TcpOutboundGateway in project spring-integration by spring-projects.
the class FailoverClientConnectionFactoryTests method testFailoverCachedWithGateway.
@SuppressWarnings("unchecked")
@Test
public void testFailoverCachedWithGateway() throws Exception {
final TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory(0);
server.setBeanName("server");
server.afterPropertiesSet();
DirectChannel inChannel = new DirectChannel();
inChannel.setBeanName("inChannel");
TcpInboundGateway inbound = new TcpInboundGateway();
inbound.setConnectionFactory(server);
inbound.setRequestChannel(inChannel);
inbound.afterPropertiesSet();
inChannel.subscribe(new BridgeHandler());
inbound.start();
TestingUtilities.waitListening(server, 10000L);
int port = server.getPort();
AbstractClientConnectionFactory client = new TcpNetClientConnectionFactory("localhost", port);
client.setBeanName("client");
// Cache
CachingClientConnectionFactory cachingClient = new CachingClientConnectionFactory(client, 2);
cachingClient.setBeanName("cache");
cachingClient.afterPropertiesSet();
// Failover
List<AbstractClientConnectionFactory> clientFactories = new ArrayList<AbstractClientConnectionFactory>();
clientFactories.add(cachingClient);
FailoverClientConnectionFactory failoverClient = new FailoverClientConnectionFactory(clientFactories);
failoverClient.setSingleUse(true);
failoverClient.afterPropertiesSet();
TcpOutboundGateway outbound = new TcpOutboundGateway();
outbound.setConnectionFactory(failoverClient);
QueueChannel replyChannel = new QueueChannel();
replyChannel.setBeanName("replyChannel");
outbound.setReplyChannel(replyChannel);
outbound.setBeanFactory(mock(BeanFactory.class));
outbound.afterPropertiesSet();
outbound.start();
outbound.handleMessage(new GenericMessage<String>("foo"));
Message<byte[]> result = (Message<byte[]>) replyChannel.receive(10000);
assertNotNull(result);
assertEquals("foo", new String(result.getPayload()));
// INT-4024 - second reply had bad connection id
outbound.handleMessage(new GenericMessage<String>("foo"));
result = (Message<byte[]>) replyChannel.receive(10000);
assertNotNull(result);
assertEquals("foo", new String(result.getPayload()));
inbound.stop();
outbound.stop();
}
use of org.springframework.integration.ip.tcp.TcpOutboundGateway in project spring-integration by spring-projects.
the class CachingClientConnectionFactoryTests method testGatewayRelease.
@SuppressWarnings("unchecked")
// INT-3722
@Test
public void testGatewayRelease() throws Exception {
TcpNetServerConnectionFactory in = new TcpNetServerConnectionFactory(0);
in.setApplicationEventPublisher(mock(ApplicationEventPublisher.class));
final TcpSendingMessageHandler handler = new TcpSendingMessageHandler();
handler.setConnectionFactory(in);
final AtomicInteger count = new AtomicInteger(2);
in.registerListener(message -> {
if (!(message instanceof ErrorMessage)) {
if (count.decrementAndGet() < 1) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
handler.handleMessage(message);
}
return false;
});
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
handler.start();
TestingUtilities.waitListening(in, null);
int port = in.getPort();
TcpNetClientConnectionFactory out = new TcpNetClientConnectionFactory("localhost", port);
out.setApplicationEventPublisher(mock(ApplicationEventPublisher.class));
CachingClientConnectionFactory cache = new CachingClientConnectionFactory(out, 2);
final TcpOutboundGateway gate = new TcpOutboundGateway();
gate.setConnectionFactory(cache);
QueueChannel outputChannel = new QueueChannel();
gate.setOutputChannel(outputChannel);
gate.setBeanFactory(mock(BeanFactory.class));
gate.afterPropertiesSet();
Log logger = spy(TestUtils.getPropertyValue(gate, "logger", Log.class));
new DirectFieldAccessor(gate).setPropertyValue("logger", logger);
when(logger.isDebugEnabled()).thenReturn(true);
doAnswer(new Answer<Void>() {
private final CountDownLatch latch = new CountDownLatch(2);
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
invocation.callRealMethod();
String log = invocation.getArgument(0);
if (log.startsWith("Response")) {
new SimpleAsyncTaskExecutor().execute(() -> gate.handleMessage(new GenericMessage<>("bar")));
// hold up the first thread until the second has added its pending reply
latch.await(10, TimeUnit.SECONDS);
} else if (log.startsWith("Added")) {
latch.countDown();
}
return null;
}
}).when(logger).debug(anyString());
gate.start();
gate.handleMessage(new GenericMessage<String>("foo"));
Message<byte[]> result = (Message<byte[]>) outputChannel.receive(10000);
assertNotNull(result);
assertEquals("foo", new String(result.getPayload()));
result = (Message<byte[]>) outputChannel.receive(10000);
assertNotNull(result);
assertEquals("bar", new String(result.getPayload()));
handler.stop();
gate.stop();
verify(logger, never()).error(anyString());
}
use of org.springframework.integration.ip.tcp.TcpOutboundGateway 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