Search in sources :

Example 41 with AbstractReplyProducingMessageHandler

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

the class NestedGatewayTests method errorChannelRetained.

@Test
public void errorChannelRetained() {
    DirectChannel requestChannel = new DirectChannel();
    DirectChannel errorChannel = 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").setErrorChannel(errorChannel).build();
    Message<?> reply = gateway.sendAndReceiveMessage(message);
    assertEquals("test-reply", reply.getPayload());
    assertEquals(errorChannel, reply.getHeaders().getErrorChannel());
}
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 42 with AbstractReplyProducingMessageHandler

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

the class AdvisedMessageHandlerTests method defaultStatefulRetrySucceedOnThirdTry.

@Test
public void defaultStatefulRetrySucceedOnThirdTry() {
    final AtomicInteger counter = new AtomicInteger(2);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            if (counter.getAndDecrement() > 0) {
                throw new RuntimeException("foo");
            }
            return "bar";
        }
    };
    QueueChannel replies = new QueueChannel();
    handler.setOutputChannel(replies);
    RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();
    advice.setRetryStateGenerator(message -> new DefaultRetryState(message.getHeaders().getId()));
    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    handler.setAdviceChain(adviceChain);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    Message<String> message = new GenericMessage<String>("Hello, world!");
    for (int i = 0; i < 3; i++) {
        try {
            handler.handleMessage(message);
        } catch (Exception e) {
            assertTrue(i < 2);
        }
    }
    assertTrue(counter.get() == -1);
    Message<?> reply = replies.receive(10000);
    assertNotNull(reply);
    assertEquals("bar", reply.getPayload());
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) DefaultRetryState(org.springframework.retry.support.DefaultRetryState) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) GenericMessage(org.springframework.messaging.support.GenericMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Advice(org.aopalliance.aop.Advice) Test(org.junit.Test)

Example 43 with AbstractReplyProducingMessageHandler

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

the class AdvisedMessageHandlerTests method testINT2858ExpressionAdviceWithSendFailureOnEachRetry.

@Test
public void testINT2858ExpressionAdviceWithSendFailureOnEachRetry() {
    final AtomicInteger counter = new AtomicInteger(0);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return "foo";
        }
    };
    QueueChannel errors = new QueueChannel();
    List<Advice> adviceChain = new ArrayList<Advice>();
    ExpressionEvaluatingRequestHandlerAdvice expressionAdvice = new ExpressionEvaluatingRequestHandlerAdvice();
    expressionAdvice.setBeanFactory(mock(BeanFactory.class));
    expressionAdvice.setOnFailureExpressionString("#exception.message");
    expressionAdvice.setFailureChannel(errors);
    adviceChain.add(new RequestHandlerRetryAdvice());
    adviceChain.add(expressionAdvice);
    adviceChain.add((MethodInterceptor) invocation -> {
        throw new RuntimeException("intentional: " + counter.incrementAndGet());
    });
    handler.setAdviceChain(adviceChain);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    try {
        handler.handleMessage(new GenericMessage<String>("test"));
    } catch (Exception e) {
        assertEquals("intentional: 3", e.getCause().getMessage());
    }
    for (int i = 1; i <= 3; i++) {
        Message<?> receive = errors.receive(10000);
        assertNotNull(receive);
        assertEquals("intentional: " + i, ((MessageHandlingExpressionEvaluatingAdviceException) receive.getPayload()).getEvaluationResult());
    }
    assertNull(errors.receive(1));
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) DefaultRetryState(org.springframework.retry.support.DefaultRetryState) AopUtils(org.springframework.aop.support.AopUtils) Autowired(org.springframework.beans.factory.annotation.Autowired) ErrorMessage(org.springframework.messaging.support.ErrorMessage) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) MethodInvocation(org.aopalliance.intercept.MethodInvocation) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) PollableChannel(org.springframework.messaging.PollableChannel) Method(java.lang.reflect.Method) AdviceMessage(org.springframework.integration.message.AdviceMessage) TaskScheduler(org.springframework.scheduling.TaskScheduler) MessageChannel(org.springframework.messaging.MessageChannel) Executors(java.util.concurrent.Executors) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) RunWith(org.junit.runner.RunWith) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Mockito.spy(org.mockito.Mockito.spy) TestUtils(org.springframework.integration.test.util.TestUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) MessageFilter(org.springframework.integration.filter.MessageFilter) Advice(org.aopalliance.aop.Advice) Message(org.springframework.messaging.Message) ExecutorService(java.util.concurrent.ExecutorService) ErrorHandlingTaskExecutor(org.springframework.integration.util.ErrorHandlingTaskExecutor) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Mockito(org.mockito.Mockito) Assert.assertNull(org.junit.Assert.assertNull) RetryContext(org.springframework.retry.RetryContext) BeanFactory(org.springframework.beans.factory.BeanFactory) ContextConfiguration(org.springframework.test.context.ContextConfiguration) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Log(org.apache.commons.logging.Log) GenericMessage(org.springframework.messaging.support.GenericMessage) RetryTemplate(org.springframework.retry.support.RetryTemplate) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Advice(org.aopalliance.aop.Advice) Test(org.junit.Test)

Example 44 with AbstractReplyProducingMessageHandler

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

the class AdvisedMessageHandlerTests method defaultStatefulRetryRecoverAfterThirdTrySpelState.

@Test
public void defaultStatefulRetryRecoverAfterThirdTrySpelState() {
    final AtomicInteger counter = new AtomicInteger(3);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            if (counter.getAndDecrement() > 0) {
                throw new RuntimeException("foo");
            }
            return "bar";
        }
    };
    QueueChannel replies = new QueueChannel();
    handler.setOutputChannel(replies);
    RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();
    advice.setRetryStateGenerator(new SpelExpressionRetryStateGenerator("headers['id']"));
    defaultStatefulRetryRecoverAfterThirdTryGuts(counter, handler, replies, advice);
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Test(org.junit.Test)

Example 45 with AbstractReplyProducingMessageHandler

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

the class AdvisedMessageHandlerTests method defaultStatefulRetryRecoverAfterThirdTry.

@Test
public void defaultStatefulRetryRecoverAfterThirdTry() {
    final AtomicInteger counter = new AtomicInteger(3);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            if (counter.getAndDecrement() > 0) {
                throw new RuntimeException("foo");
            }
            return "bar";
        }
    };
    QueueChannel replies = new QueueChannel();
    handler.setOutputChannel(replies);
    RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();
    advice.setRetryStateGenerator(message -> new DefaultRetryState(message.getHeaders().getId()));
    defaultStatefulRetryRecoverAfterThirdTryGuts(counter, handler, replies, advice);
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultRetryState(org.springframework.retry.support.DefaultRetryState) 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