use of org.springframework.integration.ip.tcp.TcpInboundGateway 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.TcpInboundGateway in project spring-integration-samples by spring-projects.
the class Application method tcpGate.
@Bean
TcpInboundGateway tcpGate() {
TcpInboundGateway gateway = new TcpInboundGateway();
gateway.setConnectionFactory(cf());
gateway.setRequestChannel(requestChannel());
return gateway;
}
use of org.springframework.integration.ip.tcp.TcpInboundGateway in project spring-integration-samples by spring-projects.
the class Application method tcpGate.
@Bean
TcpInboundGateway tcpGate() {
TcpInboundGateway gateway = new TcpInboundGateway();
gateway.setConnectionFactory(cf());
gateway.setRequestChannel(requestChannel());
return gateway;
}
use of org.springframework.integration.ip.tcp.TcpInboundGateway in project spring-integration by spring-projects.
the class ConnectionEventTests method testInboundGatewayNoConnectionEvents.
@Test
public void testInboundGatewayNoConnectionEvents() {
TcpInboundGateway gw = new TcpInboundGateway();
AbstractServerConnectionFactory scf = new AbstractServerConnectionFactory(0) {
@Override
public void run() {
}
};
final AtomicReference<ApplicationEvent> theEvent = new AtomicReference<ApplicationEvent>();
scf.setApplicationEventPublisher(new ApplicationEventPublisher() {
@Override
public void publishEvent(Object event) {
}
@Override
public void publishEvent(ApplicationEvent event) {
theEvent.set(event);
}
});
gw.setConnectionFactory(scf);
DirectChannel requestChannel = new DirectChannel();
requestChannel.subscribe(message -> ((MessageChannel) message.getHeaders().getReplyChannel()).send(message));
gw.setRequestChannel(requestChannel);
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());
assertSame(message, ((MessagingException) event.getCause()).getFailedMessage());
gw.stop();
scf.stop();
}
use of org.springframework.integration.ip.tcp.TcpInboundGateway in project spring-integration by spring-projects.
the class FailoverClientConnectionFactoryTests method setupAndStartServers.
private Holder setupAndStartServers(AbstractServerConnectionFactory server1, AbstractServerConnectionFactory server2) {
Executor exec = new SimpleAsyncTaskExecutor();
server1.setTaskExecutor(exec);
server2.setTaskExecutor(exec);
server1.setBeanName("server1");
server2.setBeanName("server2");
server1.setApplicationEventPublisher(NULL_PUBLISHER);
server2.setApplicationEventPublisher(NULL_PUBLISHER);
TcpInboundGateway gateway1 = new TcpInboundGateway();
gateway1.setConnectionFactory(server1);
SubscribableChannel channel = new DirectChannel();
final AtomicReference<String> connectionId = new AtomicReference<String>();
channel.subscribe(message -> {
connectionId.set((String) message.getHeaders().get(IpHeaders.CONNECTION_ID));
((MessageChannel) message.getHeaders().getReplyChannel()).send(message);
});
gateway1.setRequestChannel(channel);
gateway1.setBeanFactory(mock(BeanFactory.class));
gateway1.afterPropertiesSet();
gateway1.start();
TcpInboundGateway gateway2 = new TcpInboundGateway();
gateway2.setConnectionFactory(server2);
gateway2.setRequestChannel(channel);
gateway2.setBeanFactory(mock(BeanFactory.class));
gateway2.afterPropertiesSet();
gateway2.start();
TestingUtilities.waitListening(server1, null);
TestingUtilities.waitListening(server2, null);
Holder holder = new Holder();
holder.exec = exec;
holder.connectionId = connectionId;
holder.server1 = server1;
holder.gateway2 = gateway2;
return holder;
}
Aggregations