我正在使用RestAssured来触发API调用,并希望在REST调用失败时实现重试逻辑。
这个要求是因为我们正在获得某些API的间歇性500响应代码,这些代码通常会在重试时运行。
我已经实现了以下代码,但似乎没有用 -
HttpClientConfig.HttpClientFactory htc = new HttpClientConfig.HttpClientFactory() {
@Override
public HttpClient createHttpClient() {
DefaultHttpClient d = new DefaultHttpClient();
d.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(restRetryCount, true));
return d;
}
};
RestAssuredConfig rac = RestAssured.config().httpClient(HttpClientConfig.httpClientConfig().httpClientFactory(htc));
此外,由于RestAssured不支持HTTPClient级别日志记录,我尝试使用java.util.logging启用apache httpclient的日志记录,但这似乎也没有工作。
使用 - 创建custom.logging.properties文件 -
# Enables the header wire and context logging
org.apache.http.level = FINEST
org.apache.http.wire.level = SEVERE
# Enables the context logging for connection management & request execution
org.apache.http.impl.conn.level = FINEST
org.apache.http.impl.client.level = FINEST
org.apache.http.client.level = FINEST
并为启动日志记录写了以下内容 -
public static Logger initiateHttpTrafficLogging(){
Logger log = Logger.getLogger("httptraffic.log");
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream is = loader.getResourceAsStream("custom.logging.properties");
try{
LogManager.getLogManager().readConfiguration(is);
log.setLevel(Level.FINEST);
log.addHandler(new ConsoleHandler());
log.setUseParentHandlers(false);
log.info("Initiating HTTP Traffic logging ::\n\n");
} catch (IOException ie){
Assert.fail("ERROR: Could not load the loggin properties !!!", ie.fillInStackTrace());
}
return log;
}
任何人都可以请我指出我出错的地方或解决这个问题的任何指示。
感谢您的回复。谢谢。
有一个Spring-retry jar你可以将它与你的应用程序集成。你可以在你的方法中使用这样的东西
@Retryable(maxAttempts = 4, backoff = @Backoff(delay = 200),
include = {RetryableException.class})
ResponseEntity<String> request(
String url, HttpMethod method, HttpEntity<?> entity, Class<?> type,
Map<String, String> parameters
) throws Exception{}
你需要在配置类中执行@EnableAspectJAutoProxy和@EnableRetry