Search in sources :

Example 1 with TestErrorHandler

use of org.springframework.integration.config.TestErrorHandler 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)

Aggregations

HashMap (java.util.HashMap)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.Test)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 Expression (org.springframework.expression.Expression)1 LiteralExpression (org.springframework.expression.common.LiteralExpression)1 DirectChannel (org.springframework.integration.channel.DirectChannel)1 NullChannel (org.springframework.integration.channel.NullChannel)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 ExpressionFactoryBean (org.springframework.integration.config.ExpressionFactoryBean)1 TestErrorHandler (org.springframework.integration.config.TestErrorHandler)1 PollingConsumer (org.springframework.integration.endpoint.PollingConsumer)1 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)1 ReplyRequiredException (org.springframework.integration.handler.ReplyRequiredException)1 Message (org.springframework.messaging.Message)1 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)1