如何在Jersey-Client Java中实现重试机制 [英] How to implement a retry mechanism in jersey-client java

查看:208
本文介绍了如何在Jersey-Client Java中实现重试机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jersey-client进行一些http rest api调用.现在,我想重试失败请求.假设返回错误代码不是200,那么我想重试几次.如何使用Jersey客户端

I am doing some http rest api calls using jersey-client. Now I want to do a retry for a failure request. Say if the return error code is not 200 then I want to retry it again for a few times. How can do it using Jersey client

推荐答案

要在任何情况下实施重试,请查看故障保护:

For implementing retries in any situation, check out Failsafe:

RetryPolicy retryPolicy = new RetryPolicy()
  .retryIf((ClientResponse response) -> response.getStatus() != 200)
  .withDelay(1, TimeUnit.SECONDS)
  .withMaxRetries(3);

Failsafe.with(retryPolicy).get(() -> webResource.post(ClientResponse.class, input));

如果响应状态!= 200,此示例将重试,最多3次,重试之间间隔1秒.

This example retries if the response status != 200, up to 3 times, with a 1 second delay between retries.

这篇关于如何在Jersey-Client Java中实现重试机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆