use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class AbstractMongoDbMessageGroupStoreTests method testWithMessageHistory.
@Test
@MongoDbAvailable
public void testWithMessageHistory() throws Exception {
this.cleanupCollections(new SimpleMongoDbFactory(new MongoClient(), "test"));
MessageGroupStore store = this.getMessageGroupStore();
store.getMessageGroup(1);
Message<?> message = new GenericMessage<String>("Hello");
DirectChannel fooChannel = new DirectChannel();
fooChannel.setBeanName("fooChannel");
DirectChannel barChannel = new DirectChannel();
barChannel.setBeanName("barChannel");
message = MessageHistory.write(message, fooChannel);
message = MessageHistory.write(message, barChannel);
store.addMessagesToGroup(1, message);
MessageGroup group = store.getMessageGroup(1);
assertNotNull(group);
Collection<Message<?>> messages = group.getMessages();
assertTrue(!messages.isEmpty());
message = messages.iterator().next();
MessageHistory messageHistory = MessageHistory.read(message);
assertNotNull(messageHistory);
assertEquals(2, messageHistory.size());
Properties fooChannelHistory = messageHistory.get(0);
assertEquals("fooChannel", fooChannelHistory.get("name"));
assertEquals("channel", fooChannelHistory.get("type"));
}
use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class RedisMessageStoreTests method testWithMessageHistory.
@Test
@RedisAvailable
public void testWithMessageHistory() throws Exception {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMessageStore store = new RedisMessageStore(jcf);
Message<?> message = new GenericMessage<String>("Hello");
DirectChannel fooChannel = new DirectChannel();
fooChannel.setBeanName("fooChannel");
DirectChannel barChannel = new DirectChannel();
barChannel.setBeanName("barChannel");
message = MessageHistory.write(message, fooChannel);
message = MessageHistory.write(message, barChannel);
store.addMessage(message);
message = store.getMessage(message.getHeaders().getId());
MessageHistory messageHistory = MessageHistory.read(message);
assertNotNull(messageHistory);
assertEquals(2, messageHistory.size());
Properties fooChannelHistory = messageHistory.get(0);
assertEquals("fooChannel", fooChannelHistory.get("name"));
assertEquals("channel", fooChannelHistory.get("type"));
}
use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class TestServerConfig method clientOutboundChannel.
@Bean
public AbstractSubscribableChannel clientOutboundChannel() {
DirectChannel directChannel = new DirectChannel();
directChannel.addInterceptor(new ChannelInterceptorAdapter() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
headers.setLeaveMutable(true);
return MessageBuilder.createMessage(message.getPayload(), headers.getMessageHeaders());
}
});
return directChannel;
}
use of org.springframework.integration.channel.DirectChannel 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.channel.DirectChannel in project spring-integration by spring-projects.
the class TcpInboundGatewayTests method testErrorFlow.
@Test
public void testErrorFlow() throws Exception {
AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0);
scf.setSingleUse(true);
TcpInboundGateway gateway = new TcpInboundGateway();
gateway.setConnectionFactory(scf);
SubscribableChannel errorChannel = new DirectChannel();
final String errorMessage = "An error occurred";
errorChannel.subscribe(message -> {
MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
replyChannel.send(new GenericMessage<String>(errorMessage));
});
gateway.setErrorChannel(errorChannel);
scf.start();
TestingUtilities.waitListening(scf, 20000L);
int port = scf.getPort();
final SubscribableChannel channel = new DirectChannel();
gateway.setRequestChannel(channel);
gateway.setBeanFactory(mock(BeanFactory.class));
ServiceActivatingHandler handler = new ServiceActivatingHandler(new FailingService());
channel.subscribe(handler);
Socket socket1 = SocketFactory.getDefault().createSocket("localhost", port);
socket1.getOutputStream().write("Test1\r\n".getBytes());
Socket socket2 = SocketFactory.getDefault().createSocket("localhost", port);
socket2.getOutputStream().write("Test2\r\n".getBytes());
byte[] bytes = new byte[errorMessage.length() + 2];
readFully(socket1.getInputStream(), bytes);
assertEquals(errorMessage + "\r\n", new String(bytes));
readFully(socket2.getInputStream(), bytes);
assertEquals(errorMessage + "\r\n", new String(bytes));
}
Aggregations