Search in sources :

Example 1 with MethodInvokingReleaseStrategy

use of org.springframework.integration.aggregator.MethodInvokingReleaseStrategy in project spring-integration by spring-projects.

the class AggregatorAnnotationTests method testAnnotationWithCustomReleaseStrategy.

@Test
public void testAnnotationWithCustomReleaseStrategy() throws Exception {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:/org/springframework/integration/config/annotation/testAnnotatedAggregator.xml" });
    final String endpointName = "endpointWithDefaultAnnotationAndCustomReleaseStrategy";
    MessageHandler aggregator = this.getAggregator(context, endpointName);
    Object releaseStrategy = getPropertyValue(aggregator, "releaseStrategy");
    Assert.assertTrue(releaseStrategy instanceof MethodInvokingReleaseStrategy);
    MethodInvokingReleaseStrategy releaseStrategyAdapter = (MethodInvokingReleaseStrategy) releaseStrategy;
    Object handlerMethods = new DirectFieldAccessor(releaseStrategyAdapter).getPropertyValue("adapter.delegate.handlerMethods");
    assertNull(handlerMethods);
    Object handlerMethod = new DirectFieldAccessor(releaseStrategyAdapter).getPropertyValue("adapter.delegate.handlerMethod");
    assertTrue(handlerMethod.toString().contains("completionChecker"));
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) MessageHandler(org.springframework.messaging.MessageHandler) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MethodInvokingReleaseStrategy(org.springframework.integration.aggregator.MethodInvokingReleaseStrategy) Test(org.junit.Test)

Example 2 with MethodInvokingReleaseStrategy

use of org.springframework.integration.aggregator.MethodInvokingReleaseStrategy 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 3 with MethodInvokingReleaseStrategy

use of org.springframework.integration.aggregator.MethodInvokingReleaseStrategy in project spring-integration by spring-projects.

the class AggregatorParserTests method testAggregatorWithPojoReleaseStrategyAsCollection.

// see INT-2011
@Test
public void testAggregatorWithPojoReleaseStrategyAsCollection() {
    MessageChannel input = (MessageChannel) context.getBean("aggregatorWithPojoReleaseStrategyInputAsCollection");
    EventDrivenConsumer endpoint = (EventDrivenConsumer) context.getBean("aggregatorWithPojoReleaseStrategyAsCollection");
    ReleaseStrategy releaseStrategy = (ReleaseStrategy) new DirectFieldAccessor(new DirectFieldAccessor(endpoint).getPropertyValue("handler")).getPropertyValue("releaseStrategy");
    Assert.assertTrue(releaseStrategy instanceof MethodInvokingReleaseStrategy);
    DirectFieldAccessor releaseStrategyAccessor = new DirectFieldAccessor(new DirectFieldAccessor(new DirectFieldAccessor(releaseStrategy).getPropertyValue("adapter")).getPropertyValue("delegate"));
    Object handlerMethods = releaseStrategyAccessor.getPropertyValue("handlerMethods");
    assertNull(handlerMethods);
    Object handlerMethod = releaseStrategyAccessor.getPropertyValue("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) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) 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 4 with MethodInvokingReleaseStrategy

use of org.springframework.integration.aggregator.MethodInvokingReleaseStrategy in project spring-integration by spring-projects.

the class ResequencerParserTests method testReleaseStrategyRefAndMethod.

@Test
public void testReleaseStrategyRefAndMethod() throws Exception {
    EventDrivenConsumer endpoint = (EventDrivenConsumer) context.getBean("resequencerWithReleaseStrategyRefAndMethod");
    ResequencingMessageHandler resequencer = getPropertyValue(endpoint, "handler", ResequencingMessageHandler.class);
    Object releaseStrategyBean = context.getBean("testReleaseStrategyPojo");
    assertTrue("Release strategy is not of the expected type", releaseStrategyBean instanceof TestReleaseStrategyPojo);
    TestReleaseStrategyPojo expectedReleaseStrategy = (TestReleaseStrategyPojo) releaseStrategyBean;
    int currentInvocationCount = expectedReleaseStrategy.invocationCount;
    ReleaseStrategy effectiveReleaseStrategy = (ReleaseStrategy) getPropertyValue(resequencer, "releaseStrategy");
    assertTrue("The release strategy is expected to be a MethodInvokingReleaseStrategy", effectiveReleaseStrategy instanceof MethodInvokingReleaseStrategy);
    effectiveReleaseStrategy.canRelease(new SimpleMessageGroup("test"));
    assertEquals("The ResequencerEndpoint was not invoked the expected number of times;", currentInvocationCount + 1, expectedReleaseStrategy.invocationCount);
    assertTrue(TestUtils.getPropertyValue(resequencer, "expireGroupsUponTimeout", Boolean.class));
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) ResequencingMessageHandler(org.springframework.integration.aggregator.ResequencingMessageHandler) MethodInvokingReleaseStrategy(org.springframework.integration.aggregator.MethodInvokingReleaseStrategy) MethodInvokingReleaseStrategy(org.springframework.integration.aggregator.MethodInvokingReleaseStrategy) ReleaseStrategy(org.springframework.integration.aggregator.ReleaseStrategy) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) Test(org.junit.Test)

Example 5 with MethodInvokingReleaseStrategy

use of org.springframework.integration.aggregator.MethodInvokingReleaseStrategy in project spring-integration by spring-projects.

the class AggregatorAnnotationPostProcessor method createHandler.

@Override
protected MessageHandler createHandler(Object bean, Method method, List<Annotation> annotations) {
    MethodInvokingMessageGroupProcessor processor = new MethodInvokingMessageGroupProcessor(bean, method);
    processor.setBeanFactory(this.beanFactory);
    MethodInvokingReleaseStrategy releaseStrategy = null;
    Method releaseStrategyMethod = MessagingAnnotationUtils.findAnnotatedMethod(bean, ReleaseStrategy.class);
    if (releaseStrategyMethod != null) {
        releaseStrategy = new MethodInvokingReleaseStrategy(bean, releaseStrategyMethod);
    }
    MethodInvokingCorrelationStrategy correlationStrategy = null;
    Method correlationStrategyMethod = MessagingAnnotationUtils.findAnnotatedMethod(bean, CorrelationStrategy.class);
    if (correlationStrategyMethod != null) {
        correlationStrategy = new MethodInvokingCorrelationStrategy(bean, correlationStrategyMethod);
    }
    AggregatingMessageHandler handler = new AggregatingMessageHandler(processor, new SimpleMessageStore(), correlationStrategy, releaseStrategy);
    String discardChannelName = MessagingAnnotationUtils.resolveAttribute(annotations, "discardChannel", String.class);
    if (StringUtils.hasText(discardChannelName)) {
        handler.setDiscardChannelName(discardChannelName);
    }
    String outputChannelName = MessagingAnnotationUtils.resolveAttribute(annotations, "outputChannel", String.class);
    if (StringUtils.hasText(outputChannelName)) {
        handler.setOutputChannelName(outputChannelName);
    }
    String sendPartialResultsOnExpiry = MessagingAnnotationUtils.resolveAttribute(annotations, "sendPartialResultsOnExpiry", String.class);
    if (sendPartialResultsOnExpiry != null) {
        handler.setSendPartialResultOnExpiry(Boolean.parseBoolean(this.beanFactory.resolveEmbeddedValue(sendPartialResultsOnExpiry)));
    }
    handler.setBeanFactory(this.beanFactory);
    return handler;
}
Also used : AggregatingMessageHandler(org.springframework.integration.aggregator.AggregatingMessageHandler) SimpleMessageStore(org.springframework.integration.store.SimpleMessageStore) MethodInvokingMessageGroupProcessor(org.springframework.integration.aggregator.MethodInvokingMessageGroupProcessor) Method(java.lang.reflect.Method) MethodInvokingCorrelationStrategy(org.springframework.integration.aggregator.MethodInvokingCorrelationStrategy) MethodInvokingReleaseStrategy(org.springframework.integration.aggregator.MethodInvokingReleaseStrategy)

Aggregations

MethodInvokingReleaseStrategy (org.springframework.integration.aggregator.MethodInvokingReleaseStrategy)5 Test (org.junit.Test)4 ReleaseStrategy (org.springframework.integration.aggregator.ReleaseStrategy)3 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)3 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 ExpressionEvaluatingReleaseStrategy (org.springframework.integration.aggregator.ExpressionEvaluatingReleaseStrategy)2 MessageChannel (org.springframework.messaging.MessageChannel)2 PollableChannel (org.springframework.messaging.PollableChannel)2 Method (java.lang.reflect.Method)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 AggregatingMessageHandler (org.springframework.integration.aggregator.AggregatingMessageHandler)1 MethodInvokingCorrelationStrategy (org.springframework.integration.aggregator.MethodInvokingCorrelationStrategy)1 MethodInvokingMessageGroupProcessor (org.springframework.integration.aggregator.MethodInvokingMessageGroupProcessor)1 ResequencingMessageHandler (org.springframework.integration.aggregator.ResequencingMessageHandler)1 SimpleMessageGroup (org.springframework.integration.store.SimpleMessageGroup)1 SimpleMessageStore (org.springframework.integration.store.SimpleMessageStore)1 MessageHandler (org.springframework.messaging.MessageHandler)1