Shreyas arya

Generation

code to explanationThu, 09 Feb 2023

private Classifier<Throwable, RetryPolicy> configureStatusCodeBasedRetryPolicy(Integer maxAttempts) { SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy(maxAttempts: maxAttempts) NeverRetryPolicy neverRetryPolicy = new NeverRetryPolicy() new Classifier<Throwable, RetryPolicy>() { @Override RetryPolicy classify(Throwable classifiable) { if (classifiable instanceof HttpStatusCodeException) { HttpStatusCodeException exception = (HttpStatusCodeException) classifiable return getRetryPolicyForStatus(exception.statusCode, simpleRetryPolicy, neverRetryPolicy) } simpleRetryPolicy } } }

The function configureStatusCodeBasedRetryPolicy take an argument maxAttempts, create an instance of SimpleRetryPolicy with maxAttempts as argument, create an instance of NeverRetryPolicy, and return an anonymous class with a method classify that takes an argument classifiable. If classifiable is an instance of HttpStatusCodeException, we cast it to a HttpStatusCodeException and call the method getRetryPolicyForStatus with the statusCode attribute of the classifiable and the simpleRetryPolicy and the neverRetryPolicy as arguments (which returns simpleRetryPolicy if the statusCode is in the list of retriable status codes and neverRetryPolicy otherwise). If classifiable is not an instance of HttpStatusCodeException, we return the simpleRetryPolicy.

Questions about programming?Chat with your personal AI assistant