Search in sources :

Example 11 with EventDrivenConsumer

use of org.springframework.integration.endpoint.EventDrivenConsumer in project spring-integration by spring-projects.

the class ScatterGatherHandler method doInit.

@Override
protected void doInit() {
    if (this.gatherChannel == null) {
        this.gatherChannel = new FixedSubscriberChannel(this.gatherer);
    } else {
        if (this.gatherChannel instanceof SubscribableChannel) {
            this.gatherEndpoint = new EventDrivenConsumer((SubscribableChannel) this.gatherChannel, this.gatherer);
        } else if (this.gatherChannel instanceof PollableChannel) {
            this.gatherEndpoint = new PollingConsumer((PollableChannel) this.gatherChannel, this.gatherer);
            ((PollingConsumer) this.gatherEndpoint).setReceiveTimeout(this.gatherTimeout);
        } else {
            throw new MessagingException("Unsupported 'replyChannel' type [" + this.gatherChannel.getClass() + "]." + "SubscribableChannel or PollableChannel type are supported.");
        }
        this.gatherEndpoint.setBeanFactory(this.getBeanFactory());
        this.gatherEndpoint.afterPropertiesSet();
    }
    ((MessageProducer) this.gatherer).setOutputChannel(new FixedSubscriberChannel(message -> {
        MessageHeaders headers = message.getHeaders();
        if (headers.containsKey(GATHER_RESULT_CHANNEL)) {
            Object gatherResultChannel = headers.get(GATHER_RESULT_CHANNEL);
            if (gatherResultChannel instanceof MessageChannel) {
                messagingTemplate.send((MessageChannel) gatherResultChannel, message);
            } else if (gatherResultChannel instanceof String) {
                messagingTemplate.send((String) gatherResultChannel, message);
            }
        } else {
            throw new MessageDeliveryException(message, "The 'gatherResultChannel' header is required to delivery gather result.");
        }
    }));
    this.replyChannelRegistry = getBeanFactory().getBean(IntegrationContextUtils.INTEGRATION_HEADER_CHANNEL_REGISTRY_BEAN_NAME, HeaderChannelRegistry.class);
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) HeaderChannelRegistry(org.springframework.integration.support.channel.HeaderChannelRegistry) ClassUtils(org.springframework.util.ClassUtils) AopUtils(org.springframework.aop.support.AopUtils) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) SubscribableChannel(org.springframework.messaging.SubscribableChannel) MessageProducer(org.springframework.integration.core.MessageProducer) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) MessageChannel(org.springframework.messaging.MessageChannel) MessageHeaders(org.springframework.messaging.MessageHeaders) IntegrationContextUtils(org.springframework.integration.context.IntegrationContextUtils) Lifecycle(org.springframework.context.Lifecycle) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) MessageHandler(org.springframework.messaging.MessageHandler) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel) Message(org.springframework.messaging.Message) PollableChannel(org.springframework.messaging.PollableChannel) Assert(org.springframework.util.Assert) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) MessagingException(org.springframework.messaging.MessagingException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) HeaderChannelRegistry(org.springframework.integration.support.channel.HeaderChannelRegistry) MessageChannel(org.springframework.messaging.MessageChannel) PollableChannel(org.springframework.messaging.PollableChannel) MessageProducer(org.springframework.integration.core.MessageProducer) MessageHeaders(org.springframework.messaging.MessageHeaders) SubscribableChannel(org.springframework.messaging.SubscribableChannel) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel)

Example 12 with EventDrivenConsumer

use of org.springframework.integration.endpoint.EventDrivenConsumer in project spring-integration by spring-projects.

the class DirectChannelTests method testChannelCreationWithBeanDefinitionOverrideTrue.

// See INT-2434
@Test
public void testChannelCreationWithBeanDefinitionOverrideTrue() throws Exception {
    ClassPathXmlApplicationContext parentContext = new ClassPathXmlApplicationContext("parent-config.xml", this.getClass());
    MessageChannel parentChannelA = parentContext.getBean("parentChannelA", MessageChannel.class);
    MessageChannel parentChannelB = parentContext.getBean("parentChannelB", MessageChannel.class);
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
    context.setAllowBeanDefinitionOverriding(false);
    context.setConfigLocations(new String[] { "classpath:org/springframework/integration/channel/channel-override-config.xml" });
    context.setParent(parentContext);
    Method method = ReflectionUtils.findMethod(ClassPathXmlApplicationContext.class, "obtainFreshBeanFactory");
    method.setAccessible(true);
    method.invoke(context);
    assertFalse(context.containsBean("channelA"));
    assertFalse(context.containsBean("channelB"));
    assertTrue(context.containsBean("channelC"));
    assertTrue(context.containsBean("channelD"));
    context.refresh();
    PublishSubscribeChannel channelEarly = context.getBean("channelEarly", PublishSubscribeChannel.class);
    assertTrue(context.containsBean("channelA"));
    assertTrue(context.containsBean("channelB"));
    assertTrue(context.containsBean("channelC"));
    assertTrue(context.containsBean("channelD"));
    EventDrivenConsumer consumerA = context.getBean("serviceA", EventDrivenConsumer.class);
    assertEquals(context.getBean("channelA"), TestUtils.getPropertyValue(consumerA, "inputChannel"));
    assertEquals(context.getBean("channelB"), TestUtils.getPropertyValue(consumerA, "handler.outputChannel"));
    EventDrivenConsumer consumerB = context.getBean("serviceB", EventDrivenConsumer.class);
    assertEquals(context.getBean("channelB"), TestUtils.getPropertyValue(consumerB, "inputChannel"));
    assertEquals(context.getBean("channelC"), TestUtils.getPropertyValue(consumerB, "handler.outputChannel"));
    EventDrivenConsumer consumerC = context.getBean("serviceC", EventDrivenConsumer.class);
    assertEquals(context.getBean("channelC"), TestUtils.getPropertyValue(consumerC, "inputChannel"));
    assertEquals(context.getBean("channelD"), TestUtils.getPropertyValue(consumerC, "handler.outputChannel"));
    EventDrivenConsumer consumerD = context.getBean("serviceD", EventDrivenConsumer.class);
    assertEquals(parentChannelA, TestUtils.getPropertyValue(consumerD, "inputChannel"));
    assertEquals(parentChannelB, TestUtils.getPropertyValue(consumerD, "handler.outputChannel"));
    EventDrivenConsumer consumerE = context.getBean("serviceE", EventDrivenConsumer.class);
    assertEquals(parentChannelB, TestUtils.getPropertyValue(consumerE, "inputChannel"));
    EventDrivenConsumer consumerF = context.getBean("serviceF", EventDrivenConsumer.class);
    assertEquals(channelEarly, TestUtils.getPropertyValue(consumerF, "inputChannel"));
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 13 with EventDrivenConsumer

use of org.springframework.integration.endpoint.EventDrivenConsumer in project spring-integration by spring-projects.

the class AggregatorParserTests method testAggregatorWithPojoReleaseStrategy.

@Test
@SuppressWarnings("unchecked")
public void testAggregatorWithPojoReleaseStrategy() {
    MessageChannel input = this.context.getBean("aggregatorWithPojoReleaseStrategyInput", MessageChannel.class);
    EventDrivenConsumer endpoint = this.context.getBean("aggregatorWithPojoReleaseStrategy", EventDrivenConsumer.class);
    ReleaseStrategy releaseStrategy = TestUtils.getPropertyValue(endpoint, "handler.releaseStrategy", ReleaseStrategy.class);
    Assert.assertTrue(releaseStrategy instanceof MethodInvokingReleaseStrategy);
    MessagingMethodInvokerHelper<Long> methodInvokerHelper = TestUtils.getPropertyValue(releaseStrategy, "adapter.delegate", MessagingMethodInvokerHelper.class);
    Object handlerMethods = TestUtils.getPropertyValue(methodInvokerHelper, "handlerMethods");
    assertNull(handlerMethods);
    Object handlerMethod = TestUtils.getPropertyValue(methodInvokerHelper, "handlerMethod");
    assertTrue(handlerMethod.toString().contains("checkCompleteness"));
    input.send(createMessage(1L, "correlationId", 4, 0, null));
    input.send(createMessage(2L, "correlationId", 4, 1, null));
    input.send(createMessage(3L, "correlationId", 4, 2, null));
    PollableChannel outputChannel = (PollableChannel) context.getBean("outputChannel");
    Message<?> reply = outputChannel.receive(0);
    Assert.assertNull(reply);
    input.send(createMessage(5L, "correlationId", 4, 3, null));
    reply = outputChannel.receive(0);
    Assert.assertNotNull(reply);
    assertEquals(11L, reply.getPayload());
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) MessageChannel(org.springframework.messaging.MessageChannel) PollableChannel(org.springframework.messaging.PollableChannel) MethodInvokingReleaseStrategy(org.springframework.integration.aggregator.MethodInvokingReleaseStrategy) MethodInvokingReleaseStrategy(org.springframework.integration.aggregator.MethodInvokingReleaseStrategy) ReleaseStrategy(org.springframework.integration.aggregator.ReleaseStrategy) ExpressionEvaluatingReleaseStrategy(org.springframework.integration.aggregator.ExpressionEvaluatingReleaseStrategy) Test(org.junit.Test)

Example 14 with EventDrivenConsumer

use of org.springframework.integration.endpoint.EventDrivenConsumer in project spring-integration by spring-projects.

the class AggregatorParserTests method testPropertyAssignment.

@Test
public void testPropertyAssignment() throws Exception {
    EventDrivenConsumer endpoint = (EventDrivenConsumer) context.getBean("completelyDefinedAggregator");
    ReleaseStrategy releaseStrategy = (ReleaseStrategy) context.getBean("releaseStrategy");
    CorrelationStrategy correlationStrategy = (CorrelationStrategy) context.getBean("correlationStrategy");
    MessageChannel outputChannel = (MessageChannel) context.getBean("outputChannel");
    MessageChannel discardChannel = (MessageChannel) context.getBean("discardChannel");
    Object consumer = new DirectFieldAccessor(endpoint).getPropertyValue("handler");
    assertThat(consumer, is(instanceOf(AggregatingMessageHandler.class)));
    DirectFieldAccessor accessor = new DirectFieldAccessor(consumer);
    Object handlerMethods = new DirectFieldAccessor(new DirectFieldAccessor(new DirectFieldAccessor(accessor.getPropertyValue("outputProcessor")).getPropertyValue("processor")).getPropertyValue("delegate")).getPropertyValue("handlerMethods");
    assertNull(handlerMethods);
    Object handlerMethod = new DirectFieldAccessor(new DirectFieldAccessor(new DirectFieldAccessor(accessor.getPropertyValue("outputProcessor")).getPropertyValue("processor")).getPropertyValue("delegate")).getPropertyValue("handlerMethod");
    assertTrue(handlerMethod.toString().contains("createSingleMessageFromGroup"));
    assertEquals("The AggregatorEndpoint is not injected with the appropriate ReleaseStrategy instance", releaseStrategy, accessor.getPropertyValue("releaseStrategy"));
    assertEquals("The AggregatorEndpoint is not injected with the appropriate CorrelationStrategy instance", correlationStrategy, accessor.getPropertyValue("correlationStrategy"));
    assertEquals("The AggregatorEndpoint is not injected with the appropriate output channel", outputChannel, accessor.getPropertyValue("outputChannel"));
    assertEquals("The AggregatorEndpoint is not injected with the appropriate discard channel", discardChannel, accessor.getPropertyValue("discardChannel"));
    assertEquals("The AggregatorEndpoint is not set with the appropriate timeout value", 86420000L, TestUtils.getPropertyValue(consumer, "messagingTemplate.sendTimeout"));
    assertEquals("The AggregatorEndpoint is not configured with the appropriate 'send partial results on timeout' flag", true, accessor.getPropertyValue("sendPartialResultOnExpiry"));
    assertFalse(TestUtils.getPropertyValue(consumer, "expireGroupsUponTimeout", Boolean.class));
    assertTrue(TestUtils.getPropertyValue(consumer, "expireGroupsUponCompletion", Boolean.class));
    assertEquals(123L, TestUtils.getPropertyValue(consumer, "minimumTimeoutForEmptyGroups"));
    assertEquals("456", TestUtils.getPropertyValue(consumer, "groupTimeoutExpression", Expression.class).getExpressionString());
    assertSame(this.context.getBean(LockRegistry.class), TestUtils.getPropertyValue(consumer, "lockRegistry"));
    assertSame(this.context.getBean("scheduler"), TestUtils.getPropertyValue(consumer, "taskScheduler"));
    assertSame(this.context.getBean("store"), TestUtils.getPropertyValue(consumer, "messageStore"));
    assertEquals(5, TestUtils.getPropertyValue(consumer, "order"));
    assertNotNull(TestUtils.getPropertyValue(consumer, "forceReleaseAdviceChain"));
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) MessageChannel(org.springframework.messaging.MessageChannel) LockRegistry(org.springframework.integration.support.locks.LockRegistry) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) CorrelationStrategy(org.springframework.integration.aggregator.CorrelationStrategy) ExpressionEvaluatingCorrelationStrategy(org.springframework.integration.aggregator.ExpressionEvaluatingCorrelationStrategy) MethodInvokingReleaseStrategy(org.springframework.integration.aggregator.MethodInvokingReleaseStrategy) ReleaseStrategy(org.springframework.integration.aggregator.ReleaseStrategy) ExpressionEvaluatingReleaseStrategy(org.springframework.integration.aggregator.ExpressionEvaluatingReleaseStrategy) Test(org.junit.Test)

Example 15 with EventDrivenConsumer

use of org.springframework.integration.endpoint.EventDrivenConsumer 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)

Aggregations

EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)106 Test (org.junit.Test)96 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)29 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)27 DirectChannel (org.springframework.integration.channel.DirectChannel)19 PollableChannel (org.springframework.messaging.PollableChannel)19 GenericMessage (org.springframework.messaging.support.GenericMessage)18 MessageChannel (org.springframework.messaging.MessageChannel)16 QueueChannel (org.springframework.integration.channel.QueueChannel)14 MessageHandler (org.springframework.messaging.MessageHandler)12 List (java.util.List)9 ResequencingMessageHandler (org.springframework.integration.aggregator.ResequencingMessageHandler)9 Message (org.springframework.messaging.Message)8 SmartLifecycle (org.springframework.context.SmartLifecycle)6 JmsOutboundGateway (org.springframework.integration.jms.JmsOutboundGateway)6 MongoDbAvailable (org.springframework.integration.mongodb.rules.MongoDbAvailable)6 SmartLifecycleRoleController (org.springframework.integration.support.SmartLifecycleRoleController)6 MethodInvokingReleaseStrategy (org.springframework.integration.aggregator.MethodInvokingReleaseStrategy)5 ReleaseStrategy (org.springframework.integration.aggregator.ReleaseStrategy)5 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)5