繁体   English   中英

Web服务调用的重试机制

[英]Retry mechanism for Web Service invocation

我正在用Java调用Web服务。 我有一个要求,如果在Web服务调用期间出现错误,则需要在一定的预定义时间间隔后重新触发相同的Web服务调用以进行最大尝试,例如5。

我可以为此目的使用Java中的任何api吗?

选中一个http://aspects.jcabi.com/annotation-retryonfailure.html

它提供了注释以指定重试策略。

Spring有一个重试注释,该注释用于目的

步骤1:将以下依赖项添加到您的POM

<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
    <version>1.1.5.RELEASE</version>
</dependency>

步骤2:启用Spring重试

To enable Spring Retry in an application, we need to add the @EnableRetry annotation to our @Configuration class:

Ex:

@Configuration
@EnableRetry
public class AppConfig { ... }

步骤3:要向方法添加重试功能,可以使用@Retryable:

Ex: 

@Service
public interface MyService {
    @Retryable(
      value = { SQLException.class }, 
      maxAttempts = 2,
      backoff = @Backoff(delay = 5000))
    void retryService(String sql) throws SQLException;
    ...
}

步骤4.当@Retryable方法因指定的异常而失败时,@Recover批注用于定义单独的恢复方法:

Ex: 

@Service
public interface MyService {
    ...
    @Recover
    void recover(SQLException e, String sql);
}

有关更多详细信息,请参见网址: http : //www.baeldung.com/spring-retry

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM