Examples of RetryTemplate


Examples of org.springframework.retry.support.RetryTemplate

  public void testExternalRetryWithSuccessOnRetry() throws Throwable {
    MockRetryCallback callback = new MockRetryCallback();

    RetryState retryState = new DefaultRetryState("foo");

    RetryTemplate retryTemplate = new RetryTemplate();
    MapRetryContextCache cache = new MapRetryContextCache();
    retryTemplate.setRetryContextCache(cache);
    retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2, Collections
        .<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true)));

    assertFalse(cache.containsKey("foo"));

    Object result = "start_foo";
    try {
      result = retryTemplate.execute(callback, retryState);
      // The first failed attempt we expect to retry...
      fail("Expected RuntimeException");
    }
    catch (RuntimeException e) {
      assertNull(e.getMessage());
    }

    assertTrue(cache.containsKey("foo"));

    result = retryTemplate.execute(callback, retryState);

    assertFalse(cache.containsKey("foo"));

    assertEquals(2, callback.attempts);
    assertEquals("bar", result);
View Full Code Here

Examples of org.springframework.retry.support.RetryTemplate

  @Test
  public void testExponentialBackOffIsExponential() throws Throwable {
    ExponentialBackOffPolicy policy = new ExponentialBackOffPolicy();
    policy.setInitialInterval(100);
    policy.setMultiplier(1.5);
    RetryTemplate template = new RetryTemplate();
    template.setBackOffPolicy(policy);
    final List<Long> times = new ArrayList<Long>();
    RetryState retryState = new DefaultRetryState("bar");
    for (int i = 0; i < 3; i++) {
      try {
        template.execute(new RetryCallback<String, Exception>() {
          public String doWithRetry(RetryContext context) throws Exception {
            times.add(System.currentTimeMillis());
            throw new Exception("Fail");
          }
        }, new RecoveryCallback<String>() {
View Full Code Here

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

Examples of org.springframework.retry.support.RetryTemplate

  @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

Examples of org.springframework.retry.support.RetryTemplate

        /**
         * 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

Examples of org.springframework.retry.support.RetryTemplate

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

Examples of org.springframework.retry.support.RetryTemplate

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

Examples of org.springframework.retry.support.RetryTemplate

    assertNotNull(text);
  }

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

Examples of org.springframework.retry.support.RetryTemplate

    }
  }

  @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

Examples of org.springframework.retry.support.RetryTemplate

   * @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
TOP
Copyright © 2018 www.massapi.com. 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.