Package org.springframework.retry.support

Examples of org.springframework.retry.support.RetryTemplate


  @Test
  public void testFatalExceptionWithoutState() throws Throwable {
    MockRetryCallback callback = new MockRetryCallback();
    callback.setExceptionToThrow(new IllegalArgumentException());

    RetryTemplate retryTemplate = new RetryTemplate();

    // Make sure certain exceptions are fatal...
    Map<Class<? extends Throwable>, Boolean> map = new HashMap<Class<? extends Throwable>, Boolean>();
    map.put(IllegalArgumentException.class, false);
    map.put(IllegalStateException.class, false);

    // ... and allow multiple attempts
    SimpleRetryPolicy policy = new SimpleRetryPolicy(3, map);
    retryTemplate.setRetryPolicy(policy);
    RecoveryCallback<String> recoveryCallback = new RecoveryCallback<String>() {
      public String recover(RetryContext context) throws Exception {
        return "bar";
      }
    };

    Object result = null;
    try {
      result = retryTemplate.execute(callback, recoveryCallback);
    }
    catch (IllegalArgumentException e) {
      // We should swallow the exception when recovery is possible
      fail("Did not expect IllegalArgumentException");
    }
View Full Code Here


  @Test
  public void testFatalExceptionWithState() throws Throwable {
    MockRetryCallback callback = new MockRetryCallback();
    callback.setExceptionToThrow(new IllegalArgumentException());

    RetryTemplate retryTemplate = new RetryTemplate();

    Map<Class<? extends Throwable>, Boolean> map = new HashMap<Class<? extends Throwable>, Boolean>();
    map.put(IllegalArgumentException.class, false);
    map.put(IllegalStateException.class, false);

    SimpleRetryPolicy policy = new SimpleRetryPolicy(3, map);
    retryTemplate.setRetryPolicy(policy);

    RecoveryCallback<String> recoveryCallback = new RecoveryCallback<String>() {
      public String recover(RetryContext context) throws Exception {
        return "bar";
      }
    };

    Object result = null;
    try {
      retryTemplate.execute(callback, recoveryCallback, new DefaultRetryState("foo"));
      fail("Expected IllegalArgumentException");
    }
    catch (IllegalArgumentException e) {
      // If stateful we have to always rethrow. Clients who want special
      // cases have to implement them in the callback
    }
    result = retryTemplate.execute(callback, recoveryCallback, new DefaultRetryState("foo"));
    // Callback is called once: the recovery path should also be called
    assertEquals(1, callback.attempts);
    assertEquals("bar", result);
  }
View Full Code Here

        /**
         * Do not have Spring AMQP re-try messages upon failure, leave it to Camel
         * @return An advice chain populated with a NeverRetryPolicy
         */
        public final Advice[] getAdviceChain() {
            RetryTemplate retryRule = new RetryTemplate();
            retryRule.setRetryPolicy(new NeverRetryPolicy());
           
            StatefulRetryOperationsInterceptorFactoryBean retryOperation = new StatefulRetryOperationsInterceptorFactoryBean();
            retryOperation.setRetryOperations(retryRule);
            retryOperation.setMessageKeyGenerator(new DefaultKeyGenerator());
           
View Full Code Here

        String text = (String) jmsTemplate.receiveAndConvert("queue");
        list.add(text);
        return text;
      }
    };
    retryTemplate = new RetryTemplate();
  }
View Full Code Here

        String text = (String) jmsTemplate.receiveAndConvert("queue");
        list.add(text);
        return text;
      }
    };
    retryTemplate = new RetryTemplate();
  }
View Full Code Here

    assertNotNull(text);
  }

  @Before
  public void onSetUpInTransaction() throws Exception {
    retryTemplate = new RetryTemplate();
  }
View Full Code Here

    }
  }

  @Test
  public void testFailureAndRecovery() throws Exception {
    final RetryTemplate retryTemplate = new RetryTemplate();
    retryTemplate.setRetryPolicy(new NeverRetryPolicy());
    container.setMessageListener(new MessageListener() {
      @Override
      public void onMessage(final Message msg) {
        try {
          RetryCallback<Message, Exception> callback = new RetryCallback<Message, Exception>() {
            @Override
            public Message doWithRetry(RetryContext context) throws Exception {
              try {
                processed.add(((TextMessage) msg).getText());
              }
              catch (JMSException e) {
                throw new IllegalStateException(e);
              }
              throw new RuntimeException("planned failure: " + msg);
            }
          };
          RecoveryCallback<Message> recoveryCallback = new RecoveryCallback<Message>() {
            @Override
            public Message recover(RetryContext context) {
              try {
                recovered.add(((TextMessage) msg).getText());
              }
              catch (JMSException e) {
                throw new IllegalStateException(e);
              }
              return msg;
            }
          };
          retryTemplate.execute(callback, recoveryCallback, new DefaultRetryState(msg.getJMSMessageID()));
        }
        catch (Exception e) {
          throw (RuntimeException) e;
        }
      }
View Full Code Here

   * @return The retry template, or null if retry is not enabled.
   */
  protected RetryTemplate buildRetryTemplateIfRetryEnabled(AbstractBusPropertiesAccessor properties) {
    int maxAttempts = properties.getMaxAttempts(this.defaultMaxAttempts);
    if (maxAttempts > 1) {
      RetryTemplate template = new RetryTemplate();
      SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
      retryPolicy.setMaxAttempts(maxAttempts);
      ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
      backOffPolicy.setInitialInterval(properties.getBackOffInitialInterval(this.defaultBackOffInitialInterval));
      backOffPolicy.setMultiplier(properties.getBackOffMultiplier(this.defaultBackOffMultiplier));
      backOffPolicy.setMaxInterval(properties.getBackOffMaxInterval(this.defaultBackOffMaxInterval));
      template.setRetryPolicy(retryPolicy);
      template.setBackOffPolicy(backOffPolicy);
      return template;
    }
    else {
      return null;
    }
View Full Code Here

   * @param properties The properties.
   * @return The channel, or a wrapper.
   */
  private MessageChannel addRetryIfNeeded(final String name, final DirectChannel bridgeToModuleChannel,
      RedisPropertiesAccessor properties) {
    final RetryTemplate retryTemplate = buildRetryTemplateIfRetryEnabled(properties);
    if (retryTemplate == null) {
      return bridgeToModuleChannel;
    }
    else {
      DirectChannel channel = new DirectChannel() {

        @Override
        protected boolean doSend(final Message<?> message, final long timeout) {
          try {
            return retryTemplate.execute(new RetryCallback<Boolean, Exception>() {

              @Override
              public Boolean doWithRetry(RetryContext context) throws Exception {
                return bridgeToModuleChannel.send(message, timeout);
              }
View Full Code Here

    assertThat(
        channel.getClass().getName(), containsString("RedisMessageBus$")); // retry wrapper
    assertThat(
        TestUtils.getPropertyValue(TestUtils.getPropertyValue(endpoint, "consumers", List.class).get(1),
            "outputChannel").getClass().getName(), containsString("RedisMessageBus$")); // retry wrapper
    RetryTemplate retry = TestUtils.getPropertyValue(channel, "val$retryTemplate", RetryTemplate.class);
    assertEquals(23, TestUtils.getPropertyValue(retry, "retryPolicy.maxAttempts"));
    assertEquals(2000L, TestUtils.getPropertyValue(retry, "backOffPolicy.initialInterval"));
    assertEquals(20000L, TestUtils.getPropertyValue(retry, "backOffPolicy.maxInterval"));
    assertEquals(5.0, TestUtils.getPropertyValue(retry, "backOffPolicy.multiplier"));
  }
View Full Code Here

TOP

Related Classes of org.springframework.retry.support.RetryTemplate

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.