zoukankan      html  css  js  c++  java
  • Retrying Operations using Spring's RetryTemplate

     

    If your application is using Spring then it is easier to use the Spring Framework's RetryTemplate.

    The example below shows how you can use a RetryTemplate to lookup a remote object. If the remote call fails, it will be retried five times with exponential backoff.

    // import the necessary classes
    import org.springframework.batch.retry.RetryCallback;
    import org.springframework.batch.retry.RetryContext;
    import org.springframework.batch.retry.backoff.ExponentialBackOffPolicy;
    import org.springframework.batch.retry.policy.SimpleRetryPolicy;
    import org.springframework.batch.retry.support.RetryTemplate;
    ...
     
    // create the retry template
    final RetryTemplate template = new RetryTemplate();
    template.setRetryPolicy(new SimpleRetryPolicy(5));
    final ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
    backOffPolicy.setInitialInterval(1000L);
    template.setBackOffPolicy(backOffPolicy);
     
    // execute the operation using the retry template
    template.execute(new RetryCallback<Remote>() {
      @Override
      public Remote doWithRetry(final RetryContext context) throws Exception {
        return (Remote) Naming.lookup("rmi://somehost:2106/MyApp");
      }
    });

  • 相关阅读:
    常见的无损压缩算法
    多媒体基本概念
    电子商务
    Java正则表达式
    Java 注解
    java泛型
    Java的反射机制
    Java 动态代理
    函数调用约定_stdcall[转]
    要研究的东东啊
  • 原文地址:https://www.cnblogs.com/feika/p/4585930.html
Copyright © 2011-2022 走看看