簡體   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