Search in sources :

Example 1 with DefaultRetryState

use of org.springframework.retry.support.DefaultRetryState in project spring-integration by spring-projects.

the class SpelExpressionRetryStateGeneratorTests method testBasic.

@Test
public void testBasic() {
    SpelExpressionRetryStateGenerator generator = new SpelExpressionRetryStateGenerator("headers['foo']");
    RetryState state = generator.determineRetryState(message);
    assertEquals("bar", state.getKey());
    assertFalse(((DefaultRetryState) state).isForceRefresh());
    assertTrue(state.rollbackFor(new RuntimeException()));
}
Also used : DefaultRetryState(org.springframework.retry.support.DefaultRetryState) RetryState(org.springframework.retry.RetryState) Test(org.junit.Test)

Example 2 with DefaultRetryState

use of org.springframework.retry.support.DefaultRetryState in project spring-integration by spring-projects.

the class SpelExpressionRetryStateGeneratorTests method testForceRefreshTrue.

@Test
public void testForceRefreshTrue() {
    SpelExpressionRetryStateGenerator generator = new SpelExpressionRetryStateGenerator("headers['foo']", "headers['trueHeader']");
    RetryState state = generator.determineRetryState(message);
    assertEquals("bar", state.getKey());
    assertTrue(((DefaultRetryState) state).isForceRefresh());
    assertTrue(state.rollbackFor(new RuntimeException()));
}
Also used : DefaultRetryState(org.springframework.retry.support.DefaultRetryState) RetryState(org.springframework.retry.RetryState) Test(org.junit.Test)

Example 3 with DefaultRetryState

use of org.springframework.retry.support.DefaultRetryState in project spring-integration by spring-projects.

the class SpelExpressionRetryStateGeneratorTests method testForceRefreshFalse.

@Test
public void testForceRefreshFalse() {
    SpelExpressionRetryStateGenerator generator = new SpelExpressionRetryStateGenerator("headers['foo']", "headers['falseHeader']");
    RetryState state = generator.determineRetryState(message);
    assertEquals("bar", state.getKey());
    assertFalse(((DefaultRetryState) state).isForceRefresh());
    assertTrue(state.rollbackFor(new RuntimeException()));
}
Also used : DefaultRetryState(org.springframework.retry.support.DefaultRetryState) RetryState(org.springframework.retry.RetryState) Test(org.junit.Test)

Example 4 with DefaultRetryState

use of org.springframework.retry.support.DefaultRetryState 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 5 with DefaultRetryState

use of org.springframework.retry.support.DefaultRetryState 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

Test (org.junit.Test)8 DefaultRetryState (org.springframework.retry.support.DefaultRetryState)8 RetryState (org.springframework.retry.RetryState)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)2 AdviceMessage (org.springframework.integration.message.AdviceMessage)2 Message (org.springframework.messaging.Message)2 ErrorMessage (org.springframework.messaging.support.ErrorMessage)2 GenericMessage (org.springframework.messaging.support.GenericMessage)2 ArrayList (java.util.ArrayList)1 Advice (org.aopalliance.aop.Advice)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 MessageHandlingExpressionEvaluatingAdviceException (org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException)1 MessageHandlingException (org.springframework.messaging.MessageHandlingException)1 MessagingException (org.springframework.messaging.MessagingException)1