Search in sources :

Example 66 with DirectChannel

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);
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) CyclicBarrier(java.util.concurrent.CyclicBarrier) AbstractRequestHandlerAdvice(org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Test(org.junit.Test) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ApplicationListener(org.springframework.context.ApplicationListener) TestUtils(org.springframework.integration.test.util.TestUtils) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) MessageHandler(org.springframework.messaging.MessageHandler) ApplicationEventPublishingMessageHandler(org.springframework.integration.event.outbound.ApplicationEventPublishingMessageHandler) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ContextConfiguration(org.springframework.test.context.ContextConfiguration) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert(org.junit.Assert) DirectChannel(org.springframework.integration.channel.DirectChannel) PayloadApplicationEvent(org.springframework.context.PayloadApplicationEvent) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) Test(org.junit.Test)

Example 67 with DirectChannel

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);
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) CyclicBarrier(java.util.concurrent.CyclicBarrier) AbstractRequestHandlerAdvice(org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Test(org.junit.Test) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ApplicationListener(org.springframework.context.ApplicationListener) TestUtils(org.springframework.integration.test.util.TestUtils) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) MessageHandler(org.springframework.messaging.MessageHandler) ApplicationEventPublishingMessageHandler(org.springframework.integration.event.outbound.ApplicationEventPublishingMessageHandler) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ContextConfiguration(org.springframework.test.context.ContextConfiguration) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) Assert(org.junit.Assert) DirectChannel(org.springframework.integration.channel.DirectChannel) PayloadApplicationEvent(org.springframework.context.PayloadApplicationEvent) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) Test(org.junit.Test)

Example 68 with DirectChannel

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());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 69 with DirectChannel

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();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) Flags(javax.mail.Flags) Folder(javax.mail.Folder) IMAPFolder(com.sun.mail.imap.IMAPFolder) MessagingException(javax.mail.MessagingException) FolderClosedException(javax.mail.FolderClosedException) AddressException(javax.mail.internet.AddressException) IOException(java.io.IOException) Field(java.lang.reflect.Field) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MimeMessage(javax.mail.internet.MimeMessage) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 70 with DirectChannel

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"));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHistory(org.springframework.integration.history.MessageHistory) DirectChannel(org.springframework.integration.channel.DirectChannel) Properties(java.util.Properties) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

DirectChannel (org.springframework.integration.channel.DirectChannel)215 Test (org.junit.Test)182 MessageChannel (org.springframework.messaging.MessageChannel)71 Message (org.springframework.messaging.Message)68 QueueChannel (org.springframework.integration.channel.QueueChannel)63 BeanFactory (org.springframework.beans.factory.BeanFactory)45 GenericMessage (org.springframework.messaging.support.GenericMessage)38 MessageHandler (org.springframework.messaging.MessageHandler)32 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)28 CountDownLatch (java.util.concurrent.CountDownLatch)27 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)26 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)25 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)23 HashMap (java.util.HashMap)22 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)22 MessagingException (org.springframework.messaging.MessagingException)18 Properties (java.util.Properties)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 SubscribableChannel (org.springframework.messaging.SubscribableChannel)15 Expression (org.springframework.expression.Expression)14