Search in sources :

Example 36 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class ChannelAdapterParserTests method methodInvokingConsumer.

@Test
public void methodInvokingConsumer() {
    String beanName = "methodInvokingConsumer";
    Object channel = this.applicationContext.getBean(beanName);
    assertTrue(channel instanceof DirectChannel);
    BeanFactoryChannelResolver channelResolver = new BeanFactoryChannelResolver(this.applicationContext);
    assertNotNull(channelResolver.resolveDestination(beanName));
    Object adapter = this.applicationContext.getBean(beanName + ".adapter");
    assertNotNull(adapter);
    assertTrue(adapter instanceof EventDrivenConsumer);
    TestBean testBean = (TestBean) this.applicationContext.getBean("testBean");
    assertNull(testBean.getMessage());
    Message<?> message = new GenericMessage<String>("consumer test");
    assertTrue(((MessageChannel) channel).send(message));
    assertNotNull(testBean.getMessage());
    assertEquals("consumer test", testBean.getMessage());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) DirectChannel(org.springframework.integration.channel.DirectChannel) BeanFactoryChannelResolver(org.springframework.integration.support.channel.BeanFactoryChannelResolver) Test(org.junit.Test)

Example 37 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class ChannelAdapterParserTests method targetOnly.

@Test
public void targetOnly() {
    String beanName = "outboundWithImplicitChannel";
    Object channel = this.applicationContext.getBean(beanName);
    assertTrue(channel instanceof DirectChannel);
    BeanFactoryChannelResolver channelResolver = new BeanFactoryChannelResolver(this.applicationContext);
    assertNotNull(channelResolver.resolveDestination(beanName));
    Object adapter = this.applicationContext.getBean(beanName + ".adapter");
    assertNotNull(adapter);
    assertTrue(adapter instanceof EventDrivenConsumer);
    assertFalse(((EventDrivenConsumer) adapter).isAutoStartup());
    assertEquals(-1, ((EventDrivenConsumer) adapter).getPhase());
    TestConsumer consumer = (TestConsumer) this.applicationContext.getBean("consumer");
    assertNull(consumer.getLastMessage());
    Message<?> message = new GenericMessage<String>("test");
    try {
        ((MessageChannel) channel).send(message);
        fail("MessageDispatchingException is expected.");
    } catch (Exception e) {
        assertThat(e, Matchers.instanceOf(MessageDeliveryException.class));
        assertThat(e.getCause(), Matchers.instanceOf(MessageDispatchingException.class));
    }
    ((EventDrivenConsumer) adapter).start();
    ((MessageChannel) channel).send(message);
    assertNotNull(consumer.getLastMessage());
    assertEquals(message, consumer.getLastMessage());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) MessageChannel(org.springframework.messaging.MessageChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) BeanFactoryChannelResolver(org.springframework.integration.support.channel.BeanFactoryChannelResolver) DestinationResolutionException(org.springframework.messaging.core.DestinationResolutionException) MessageDispatchingException(org.springframework.integration.MessageDispatchingException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) BeanDefinitionParsingException(org.springframework.beans.factory.parsing.BeanDefinitionParsingException) Test(org.junit.Test)

Example 38 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class ContentEnricherTests method clonePayloadIgnored.

@Test
public void clonePayloadIgnored() {
    QueueChannel replyChannel = new QueueChannel();
    DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return new Source("John", "Doe");
        }
    });
    ContentEnricher enricher = new ContentEnricher();
    enricher.setRequestChannel(requestChannel);
    enricher.setShouldClonePayload(true);
    SpelExpressionParser parser = new SpelExpressionParser();
    Map<String, Expression> propertyExpressions = new HashMap<String, Expression>();
    propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName"));
    enricher.setPropertyExpressions(propertyExpressions);
    enricher.setBeanFactory(mock(BeanFactory.class));
    enricher.afterPropertiesSet();
    TargetUser target = new TargetUser();
    target.setName("replace me");
    Message<?> requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build();
    enricher.handleMessage(requestMessage);
    Message<?> reply = replyChannel.receive(0);
    TargetUser result = (TargetUser) reply.getPayload();
    assertEquals("Doe, John", result.getName());
    assertSame(target, result);
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Test(org.junit.Test)

Example 39 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class ContentEnricherTests method testLifeCycleMethodsWithRequestChannel.

@Test
public void testLifeCycleMethodsWithRequestChannel() {
    DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return new Source("John", "Doe");
        }
    });
    ContentEnricher enricher = new ContentEnricher();
    enricher.setRequestChannel(requestChannel);
    enricher.setBeanFactory(mock(BeanFactory.class));
    enricher.afterPropertiesSet();
    enricher.start();
    assertTrue(enricher.isRunning());
    enricher.stop();
    assertFalse(enricher.isRunning());
    enricher.start();
    assertTrue(enricher.isRunning());
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Test(org.junit.Test)

Example 40 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class ContentEnricherTests method nestedProperty.

@Test
public void nestedProperty() {
    QueueChannel replyChannel = new QueueChannel();
    DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return new Source("John", "Doe");
        }
    });
    ContentEnricher enricher = new ContentEnricher();
    enricher.setRequestChannel(requestChannel);
    SpelExpressionParser parser = new SpelExpressionParser();
    Map<String, Expression> propertyExpressions = new HashMap<String, Expression>();
    propertyExpressions.put("child.name", parser.parseExpression("payload.lastName + ', ' + payload.firstName"));
    enricher.setPropertyExpressions(propertyExpressions);
    enricher.setBeanFactory(mock(BeanFactory.class));
    enricher.afterPropertiesSet();
    Target target = new Target("test");
    Message<?> requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build();
    enricher.handleMessage(requestMessage);
    Message<?> reply = replyChannel.receive(0);
    Target result = (Target) reply.getPayload();
    assertEquals("test", result.getName());
    assertEquals("Doe, John", result.getChild().getName());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Test(org.junit.Test)

Aggregations

DirectChannel (org.springframework.integration.channel.DirectChannel)207 Test (org.junit.Test)179 Message (org.springframework.messaging.Message)62 MessageChannel (org.springframework.messaging.MessageChannel)60 QueueChannel (org.springframework.integration.channel.QueueChannel)58 BeanFactory (org.springframework.beans.factory.BeanFactory)45 GenericMessage (org.springframework.messaging.support.GenericMessage)37 MessageHandler (org.springframework.messaging.MessageHandler)32 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)28 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)26 CountDownLatch (java.util.concurrent.CountDownLatch)25 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)23 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)22 HashMap (java.util.HashMap)20 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)19 MessagingException (org.springframework.messaging.MessagingException)18 SubscribableChannel (org.springframework.messaging.SubscribableChannel)16 Properties (java.util.Properties)15 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)15 Expression (org.springframework.expression.Expression)14