use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class EventOutboundChannelAdapterParserTests method testInsideChain.
// INT-2275
@Test
public void testInsideChain() {
this.receivedEvent = false;
ApplicationListener<?> listener = event -> {
Object source = event.getSource();
if (source instanceof Message) {
String payload = (String) ((Message<?>) source).getPayload();
if (payload.equals("foobar")) {
receivedEvent = true;
}
}
};
this.context.addApplicationListener(listener);
DirectChannel channel = context.getBean("inputChain", DirectChannel.class);
channel.send(new GenericMessage<String>("foo"));
Assert.assertTrue(this.receivedEvent);
}
use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class EventOutboundChannelAdapterParserTests method withAdvice.
@Test
public void withAdvice() {
this.receivedEvent = false;
ApplicationListener<?> listener = event -> {
Object source = event.getSource();
if (source instanceof Message) {
String payload = (String) ((Message<?>) source).getPayload();
if (payload.equals("hello")) {
receivedEvent = true;
}
}
};
context.addApplicationListener(listener);
DirectChannel channel = context.getBean("inputAdvice", DirectChannel.class);
channel.send(new GenericMessage<String>("hello"));
Assert.assertTrue(this.receivedEvent);
Assert.assertEquals(1, adviceCalled);
}
use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class ApplicationEventListeningMessageProducerTests method anyApplicationEventCausesExceptionWithErrorHandling.
@Test(expected = MessageHandlingException.class)
public void anyApplicationEventCausesExceptionWithErrorHandling() {
DirectChannel channel = new DirectChannel();
channel.subscribe(new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
throw new RuntimeException("Failed");
}
});
ApplicationEventListeningMessageProducer adapter = new ApplicationEventListeningMessageProducer();
adapter.setOutputChannel(channel);
QueueChannel errorChannel = new QueueChannel();
adapter.setErrorChannel(errorChannel);
adapter.start();
adapter.onApplicationEvent(new TestApplicationEvent1());
Message<?> message = errorChannel.receive(10000);
assertNotNull(message);
assertEquals("Failed", ((Exception) message.getPayload()).getCause().getMessage());
adapter.setErrorChannel(null);
adapter.onApplicationEvent(new TestApplicationEvent1());
}
use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class ImapMailReceiverTests method testIdleChannelAdapterException.
@Test
public void testIdleChannelAdapterException() throws Exception {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("ImapIdleChannelAdapterParserTests-context.xml", ImapIdleChannelAdapterParserTests.class);
ImapIdleChannelAdapter adapter = context.getBean("simpleAdapter", ImapIdleChannelAdapter.class);
// ImapMailReceiver receiver = (ImapMailReceiver) TestUtils.getPropertyValue(adapter, "mailReceiver");
DirectChannel channel = new DirectChannel();
channel.subscribe(new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(org.springframework.messaging.Message<?> requestMessage) {
throw new RuntimeException("Failed");
}
});
adapter.setOutputChannel(channel);
QueueChannel errorChannel = new QueueChannel();
adapter.setErrorChannel(errorChannel);
AbstractMailReceiver receiver = new ImapMailReceiver();
receiver = spy(receiver);
receiver.setBeanFactory(mock(BeanFactory.class));
receiver.afterPropertiesSet();
Field folderField = AbstractMailReceiver.class.getDeclaredField("folder");
folderField.setAccessible(true);
Folder folder = mock(IMAPFolder.class);
given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
folderField.set(receiver, folder);
willAnswer(invocation -> true).given(folder).isOpen();
willAnswer(invocation -> null).given(receiver).openFolder();
DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
adapterAccessor.setPropertyValue("mailReceiver", receiver);
MimeMessage mailMessage = mock(MimeMessage.class);
Flags flags = mock(Flags.class);
given(mailMessage.getFlags()).willReturn(flags);
final Message[] messages = new Message[] { mailMessage };
willAnswer(invocation -> messages).given(receiver).searchForNewMessages();
willAnswer(invocation -> null).given(receiver).fetchMessages(messages);
adapter.start();
org.springframework.messaging.Message<?> replMessage = errorChannel.receive(10000);
assertNotNull(replMessage);
assertEquals("Failed", ((Exception) replMessage.getPayload()).getCause().getMessage());
adapter.stop();
context.close();
}
use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.
the class MySqlJdbcMessageStoreTests method testWithMessageHistory.
@Test
@Transactional
public void testWithMessageHistory() throws Exception {
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);
messageStore.addMessage(message);
message = messageStore.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"));
}
Aggregations