Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java resilience4j重试策略在2次重试后变为无限_Java_Httpresponse_Retrypolicy_Resilience4j - Fatal编程技术网

Java resilience4j重试策略在2次重试后变为无限

Java resilience4j重试策略在2次重试后变为无限,java,httpresponse,retrypolicy,resilience4j,Java,Httpresponse,Retrypolicy,Resilience4j,我正在使用resilience4j重试策略调用HttpGet请求并用于测试目的, 我已将retryOnResult设置为在HttpGet请求返回200状态代码时重试。 当maxtures设置为2时,它将成功重试 对于maxtures>2应用程序处于无限状态 公共类应用程序{ 公共静态void main(字符串[]args){ HttpClient=HttpClients.createDefault(); HttpRequest请求=新建HttpGet(“https://jsonplacehold

我正在使用
resilience4j
重试策略调用
HttpGet
请求并用于测试目的,
我已将
retryOnResult
设置为在
HttpGet
请求返回
200
状态代码时重试。
maxtures
设置为
2
时,它将成功重试

对于
maxtures>2
应用程序处于无限状态

公共类应用程序{
公共静态void main(字符串[]args){
HttpClient=HttpClients.createDefault();
HttpRequest请求=新建HttpGet(“https://jsonplaceholder.typicode.com/todos/1");
HttpResponse响应;
试一试{
RetryConfig RetryConfig=RetryConfig.custom().waitDuration(持续时间.秒(2))
.maxAttempts(3).retryOnResult(s->{
返回s.getStatusLine().getStatusCode()==200;
}).build();
RetryRegistry registry=RetryRegistry.of(retryConfig);
Retry=registry.Retry(“Http客户端”);
retry.getEventPublisher().onRetry(e->{
System.out.println(“重试”);
});
CheckedFunction0 retryableSupplier=重试。decorateCheckedSupplier(重试,
()->client.execute((HttpUriRequest)请求));
response=Try.of(retryableSupplier.get();
HttpEntity=response.getEntity();
System.out.println(EntityUtils.toString(entity));
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
pom.xml:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.3</version>
    </dependency>
    <dependency>
        <groupId>io.github.resilience4j</groupId>
        <artifactId>resilience4j-circuitbreaker</artifactId>
        <version>1.1.0</version>
    </dependency>

    <dependency>
        <groupId>io.github.resilience4j</groupId>
        <artifactId>resilience4j-retry</artifactId>
        <version>1.1.0</version>
    </dependency>
</dependencies>

朱尼特
朱尼特
3.8.1
测试
org.apache.httpcomponents
httpclient
4.5.3
io.github.resilience4j
弹性4J断路器
1.1.0
io.github.resilience4j
弹性4j重试
1.1.0

那不是真的。如果尝试次数大于maxAttempts,则将最后一个结果返回给客户端。这不是一个无限循环

public static void main(String[] args) {
        String response;
            RetryConfig retryConfig = RetryConfig.<String>custom().waitDuration(Duration.ofMillis(100))
                    .maxAttempts(3).retryOnResult(s -> {
                        return s.contains("Hello");
                    }).build();
            RetryRegistry registry = RetryRegistry.of(retryConfig);
            Retry retry = registry.retry("Http client");
            retry.getEventPublisher().onRetry(e -> {
                System.out.println("Retrying");
            });

            CheckedFunction0<String> retryableSupplier = Retry.decorateCheckedSupplier(retry,
                    () -> "Hello World");
            response = Try.of(retryableSupplier).get();
            System.out.println(response);

    }

第三次尝试也“失败”,但结果返回给客户端。

这不是真的。如果尝试次数大于maxAttempts,则将最后一个结果返回给客户端。这不是一个无限循环

public static void main(String[] args) {
        String response;
            RetryConfig retryConfig = RetryConfig.<String>custom().waitDuration(Duration.ofMillis(100))
                    .maxAttempts(3).retryOnResult(s -> {
                        return s.contains("Hello");
                    }).build();
            RetryRegistry registry = RetryRegistry.of(retryConfig);
            Retry retry = registry.retry("Http client");
            retry.getEventPublisher().onRetry(e -> {
                System.out.println("Retrying");
            });

            CheckedFunction0<String> retryableSupplier = Retry.decorateCheckedSupplier(retry,
                    () -> "Hello World");
            response = Try.of(retryableSupplier).get();
            System.out.println(response);

    }

第三次尝试也“失败”,但结果返回给客户端。

最终找到了根本原因。问题不在于弹性4j。但在上述场景中,在重试场景中多次调用相同的HttpGet请求。默认情况下。因此,在使用2之后,它会无限期地等待,试图从池中获取第三个连接

最终工作代码:

public class App {
    public static int retryCounter = 0; 
    public static void main(String[] args) {

        int maxAttempts = 4;
        HttpClient client = HttpClients.createDefault();
        HttpRequest request = new HttpGet("https://jsonplaceholder.typicode.com/todos/1");
        HttpResponse response;
        try {

            RetryConfig retryConfig = RetryConfig.<HttpResponse>custom().waitDuration(Duration.ofSeconds(1))
                    .maxAttempts(maxAttempts).retryOnResult(s -> {
                        try {
                            if (s.getStatusLine().getStatusCode() == 200) {

                                if (retryCounter < maxAttempts -1) {
                                    s.getEntity().getContent().close();
                                }
                                return true;
                            } else {
                                return false;
                            }
                        } catch (UnsupportedOperationException e1) {
                            return true;
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            return true;
                        }

                    }).build();
            RetryRegistry registry = RetryRegistry.of(retryConfig);
            Retry retry = registry.retry("Http client");
            retry.getEventPublisher().onRetry(e -> {
                retryCounter ++;
                System.out.println("Retrying" + e.getNumberOfRetryAttempts());

            });

            CheckedFunction0<HttpResponse> retryableSupplier = Retry.decorateCheckedSupplier(retry, () -> {

                HttpResponse res = client.execute((HttpUriRequest) request);
                return res;
            });
            response = (CloseableHttpResponse) Try.of(retryableSupplier).get();
            HttpEntity entity = response.getEntity();
            System.out.println(EntityUtils.toString(entity));
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block`enter code here`
            e.printStackTrace();
        }
    }
公共类应用程序{
公共静态int-retryCounter=0;
公共静态void main(字符串[]args){
int=4;
HttpClient=HttpClients.createDefault();
HttpRequest请求=新建HttpGet(“https://jsonplaceholder.typicode.com/todos/1");
HttpResponse响应;
试一试{
RetryConfig RetryConfig=RetryConfig.custom().waitDuration(持续时间为秒(1))
.maxAttempts(maxAttempts).retryOnResult(s->{
试一试{
如果(s.getStatusLine().getStatusCode()==200){
if(retryCounter{
retryCounter++;
System.out.println(“重试”+e.getNumberOfRetryAttempts());
});
CheckedFunction0 retryableSupplier=重试。decorateCheckedSupplier(重试,()->{
HttpResponse res=client.execute((HttpUriRequest)请求);
返回res;
});
response=(CloseableHttpResponse)Try.of(retryableSupplier.get();
HttpEntity=response.getEntity();
System.out.println(EntityUtils.toString(entity));
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块`在此处输入代码`
e、 printStackTrace();
}
}

最终找到了根本原因。问题不在于resiliency4j。但在上述场景中,同一HttpGet请求在重试场景中被多次调用。默认情况下,在使用2之后,它会无限期地等待,试图从池中获取第三个连接

最终工作代码:

public class App {
    public static int retryCounter = 0; 
    public static void main(String[] args) {

        int maxAttempts = 4;
        HttpClient client = HttpClients.createDefault();
        HttpRequest request = new HttpGet("https://jsonplaceholder.typicode.com/todos/1");
        HttpResponse response;
        try {

            RetryConfig retryConfig = RetryConfig.<HttpResponse>custom().waitDuration(Duration.ofSeconds(1))
                    .maxAttempts(maxAttempts).retryOnResult(s -> {
                        try {
                            if (s.getStatusLine().getStatusCode() == 200) {

                                if (retryCounter < maxAttempts -1) {
                                    s.getEntity().getContent().close();
                                }
                                return true;
                            } else {
                                return false;
                            }
                        } catch (UnsupportedOperationException e1) {
                            return true;
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            return true;
                        }

                    }).build();
            RetryRegistry registry = RetryRegistry.of(retryConfig);
            Retry retry = registry.retry("Http client");
            retry.getEventPublisher().onRetry(e -> {
                retryCounter ++;
                System.out.println("Retrying" + e.getNumberOfRetryAttempts());

            });

            CheckedFunction0<HttpResponse> retryableSupplier = Retry.decorateCheckedSupplier(retry, () -> {

                HttpResponse res = client.execute((HttpUriRequest) request);
                return res;
            });
            response = (CloseableHttpResponse) Try.of(retryableSupplier).get();
            HttpEntity entity = response.getEntity();
            System.out.println(EntityUtils.toString(entity));
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block`enter code here`
            e.printStackTrace();
        }
    }
公共类应用程序{
公共静态int-retryCounter=0;
公共静态void main(字符串[]args){
int=4;
HttpClient=HttpClients.createDefault();
HttpRequest请求=新建HttpGet(“https://jsonplaceholder.typicode.com/todos/1");
HttpResponse响应;
试一试{
RetryConfig RetryConfig=RetryConfig.custom().waitDuration(持续时间为秒(1))
.maxAttempts(maxAttempts).retryOnResult(s->{