Search in sources :

Example 36 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class ContentEnricherTests method replyChannelReplyTimingOut.

/**
 * In this test a {@link Target} message is passed into a {@link ContentEnricher}.
 * The Enricher passes the message to a "request-channel" that is backed by a
 * {@link QueueChannel}. The consumer of the "request-channel" takes a long
 * time to execute, longer actually than the specified "replyTimeout" set on
 * the {@link ContentEnricher}.
 *
 * Due to the occurring replyTimeout, a Null replyMessage is returned and because
 * "requiresReply" is set to "true" on the {@link ContentEnricher}, a
 * {@link ReplyRequiredException} is raised.
 */
@Test
public void replyChannelReplyTimingOut() throws Exception {
    final long requestTimeout = 500L;
    final long replyTimeout = 100L;
    final DirectChannel replyChannel = new DirectChannel();
    final QueueChannel requestChannel = new QueueChannel(1);
    final ContentEnricher enricher = new ContentEnricher();
    enricher.setRequestChannel(requestChannel);
    enricher.setReplyChannel(replyChannel);
    enricher.setOutputChannel(new NullChannel());
    enricher.setRequestTimeout(requestTimeout);
    enricher.setReplyTimeout(replyTimeout);
    final ExpressionFactoryBean expressionFactoryBean = new ExpressionFactoryBean("payload");
    expressionFactoryBean.setSingleton(false);
    expressionFactoryBean.afterPropertiesSet();
    final Map<String, Expression> expressions = new HashMap<String, Expression>();
    expressions.put("name", new LiteralExpression("cartman"));
    expressions.put("child.name", expressionFactoryBean.getObject());
    enricher.setPropertyExpressions(expressions);
    enricher.setRequiresReply(true);
    enricher.setBeanName("Enricher");
    enricher.setBeanFactory(mock(BeanFactory.class));
    enricher.afterPropertiesSet();
    final AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                fail(e.getMessage());
            }
            return new Target("child");
        }
    };
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    final PollingConsumer consumer = new PollingConsumer(requestChannel, handler);
    final TestErrorHandler errorHandler = new TestErrorHandler();
    consumer.setTrigger(new PeriodicTrigger(0));
    consumer.setErrorHandler(errorHandler);
    consumer.setTaskScheduler(taskScheduler);
    consumer.setBeanFactory(mock(BeanFactory.class));
    consumer.afterPropertiesSet();
    consumer.start();
    final Target target = new Target("replace me");
    Message<?> requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build();
    try {
        enricher.handleMessage(requestMessage);
    } catch (ReplyRequiredException e) {
        assertEquals("No reply produced by handler 'Enricher', and its 'requiresReply' property is set to true.", e.getMessage());
        return;
    }
    fail("ReplyRequiredException expected.");
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) ReplyRequiredException(org.springframework.integration.handler.ReplyRequiredException) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) HashMap(java.util.HashMap) TestErrorHandler(org.springframework.integration.config.TestErrorHandler) LiteralExpression(org.springframework.expression.common.LiteralExpression) Matchers.containsString(org.hamcrest.Matchers.containsString) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) ExpressionFactoryBean(org.springframework.integration.config.ExpressionFactoryBean) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) NullChannel(org.springframework.integration.channel.NullChannel) Test(org.junit.Test)

Example 37 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class ApplicationContextMessageBusTests method exactlyOneConsumerReceivesPointToPointMessage.

@Test
public void exactlyOneConsumerReceivesPointToPointMessage() {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    QueueChannel inputChannel = new QueueChannel();
    QueueChannel outputChannel1 = new QueueChannel();
    QueueChannel outputChannel2 = new QueueChannel();
    AbstractReplyProducingMessageHandler handler1 = new AbstractReplyProducingMessageHandler() {

        @Override
        public Object handleRequestMessage(Message<?> message) {
            return message;
        }
    };
    AbstractReplyProducingMessageHandler handler2 = new AbstractReplyProducingMessageHandler() {

        @Override
        public Object handleRequestMessage(Message<?> message) {
            return message;
        }
    };
    context.registerChannel("input", inputChannel);
    context.registerChannel("output1", outputChannel1);
    context.registerChannel("output2", outputChannel2);
    handler1.setOutputChannel(outputChannel1);
    handler2.setOutputChannel(outputChannel2);
    PollingConsumer endpoint1 = new PollingConsumer(inputChannel, handler1);
    endpoint1.setBeanFactory(mock(BeanFactory.class));
    PollingConsumer endpoint2 = new PollingConsumer(inputChannel, handler2);
    endpoint2.setBeanFactory(mock(BeanFactory.class));
    context.registerEndpoint("testEndpoint1", endpoint1);
    context.registerEndpoint("testEndpoint2", endpoint2);
    context.refresh();
    inputChannel.send(new GenericMessage<String>("testing"));
    Message<?> message1 = outputChannel1.receive(10000);
    Message<?> message2 = outputChannel2.receive(0);
    context.stop();
    assertTrue("exactly one message should be null", message1 == null ^ message2 == null);
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) Test(org.junit.Test)

Example 38 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class ApplicationContextMessageBusTests method endpointRegistrationWithInputChannelReference.

@Test
public void endpointRegistrationWithInputChannelReference() {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    QueueChannel sourceChannel = new QueueChannel();
    QueueChannel targetChannel = new QueueChannel();
    context.registerChannel("sourceChannel", sourceChannel);
    context.registerChannel("targetChannel", targetChannel);
    Message<String> message = MessageBuilder.withPayload("test").setReplyChannelName("targetChannel").build();
    sourceChannel.send(message);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        public Object handleRequestMessage(Message<?> message) {
            return message;
        }
    };
    handler.setBeanFactory(context);
    handler.afterPropertiesSet();
    PollingConsumer endpoint = new PollingConsumer(sourceChannel, handler);
    endpoint.setBeanFactory(mock(BeanFactory.class));
    context.registerEndpoint("testEndpoint", endpoint);
    context.refresh();
    Message<?> result = targetChannel.receive(10000);
    assertEquals("test", result.getPayload());
    context.stop();
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) Test(org.junit.Test)

Example 39 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class ApplicationContextMessageBusTests method bothConsumersReceivePublishSubscribeMessage.

@Test
public void bothConsumersReceivePublishSubscribeMessage() throws InterruptedException {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    PublishSubscribeChannel inputChannel = new PublishSubscribeChannel();
    QueueChannel outputChannel1 = new QueueChannel();
    QueueChannel outputChannel2 = new QueueChannel();
    final CountDownLatch latch = new CountDownLatch(2);
    AbstractReplyProducingMessageHandler handler1 = new AbstractReplyProducingMessageHandler() {

        @Override
        public Object handleRequestMessage(Message<?> message) {
            latch.countDown();
            return message;
        }
    };
    AbstractReplyProducingMessageHandler handler2 = new AbstractReplyProducingMessageHandler() {

        @Override
        public Object handleRequestMessage(Message<?> message) {
            latch.countDown();
            return message;
        }
    };
    context.registerChannel("input", inputChannel);
    context.registerChannel("output1", outputChannel1);
    context.registerChannel("output2", outputChannel2);
    handler1.setOutputChannel(outputChannel1);
    handler2.setOutputChannel(outputChannel2);
    EventDrivenConsumer endpoint1 = new EventDrivenConsumer(inputChannel, handler1);
    EventDrivenConsumer endpoint2 = new EventDrivenConsumer(inputChannel, handler2);
    context.registerEndpoint("testEndpoint1", endpoint1);
    context.registerEndpoint("testEndpoint2", endpoint2);
    context.refresh();
    inputChannel.send(new GenericMessage<String>("testing"));
    latch.await(500, TimeUnit.MILLISECONDS);
    assertEquals("both handlers should have been invoked", 0, latch.getCount());
    Message<?> message1 = outputChannel1.receive(500);
    Message<?> message2 = outputChannel2.receive(500);
    context.stop();
    assertNotNull("both handlers should have replied to the message", message1);
    assertNotNull("both handlers should have replied to the message", message2);
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) PublishSubscribeChannel(org.springframework.integration.channel.PublishSubscribeChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) CountDownLatch(java.util.concurrent.CountDownLatch) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) Test(org.junit.Test)

Example 40 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class NestedGatewayTests method replyChannelRetained.

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

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return requestMessage.getPayload() + "-reply";
        }
    });
    MessagingGatewaySupport gateway = new MessagingGatewaySupport() {
    };
    gateway.setRequestChannel(requestChannel);
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.afterPropertiesSet();
    Message<?> message = MessageBuilder.withPayload("test").setReplyChannel(replyChannel).build();
    Message<?> reply = gateway.sendAndReceiveMessage(message);
    assertEquals("test-reply", reply.getPayload());
    assertEquals(replyChannel, reply.getHeaders().getReplyChannel());
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Test(org.junit.Test)

Aggregations

AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)52 Test (org.junit.Test)46 BeanFactory (org.springframework.beans.factory.BeanFactory)37 QueueChannel (org.springframework.integration.channel.QueueChannel)29 Message (org.springframework.messaging.Message)28 DirectChannel (org.springframework.integration.channel.DirectChannel)23 GenericMessage (org.springframework.messaging.support.GenericMessage)20 Matchers.containsString (org.hamcrest.Matchers.containsString)19 ErrorMessage (org.springframework.messaging.support.ErrorMessage)18 ArrayList (java.util.ArrayList)14 AdviceMessage (org.springframework.integration.message.AdviceMessage)14 Advice (org.aopalliance.aop.Advice)13 HashMap (java.util.HashMap)12 MessageHandlingException (org.springframework.messaging.MessageHandlingException)12 Expression (org.springframework.expression.Expression)10 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 MessageHandlingExpressionEvaluatingAdviceException (org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException)9 MessagingException (org.springframework.messaging.MessagingException)9