Search in sources :

Example 1 with RetryContext

use of org.springframework.retry.RetryContext in project dhis2-core by dhis2.

the class MetadataRetryContextTest method testShouldSetRetryContextCorrectly.

@Test
public void testShouldSetRetryContextCorrectly() throws Exception {
    RetryContext newMock = mock(RetryContext.class);
    metadataRetryContext.setRetryContext(newMock);
    assertEquals(newMock, metadataRetryContext.getRetryContext());
}
Also used : RetryContext(org.springframework.retry.RetryContext) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 2 with RetryContext

use of org.springframework.retry.RetryContext in project spring-integration by spring-projects.

the class AdvisedMessageHandlerTests method errorMessageSendingRecovererTestsNoThrowable.

@Test
public void errorMessageSendingRecovererTestsNoThrowable() {
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            throw new RuntimeException("fooException");
        }
    };
    QueueChannel errors = new QueueChannel();
    RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();
    ErrorMessageSendingRecoverer recoverer = new ErrorMessageSendingRecoverer(errors);
    advice.setRecoveryCallback(recoverer);
    RetryTemplate retryTemplate = new RetryTemplate();
    retryTemplate.setRetryPolicy(new SimpleRetryPolicy() {

        static final long serialVersionUID = -1;

        @Override
        public boolean canRetry(RetryContext context) {
            return false;
        }
    });
    advice.setRetryTemplate(retryTemplate);
    advice.setBeanFactory(mock(BeanFactory.class));
    advice.afterPropertiesSet();
    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    handler.setAdviceChain(adviceChain);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    Message<String> message = new GenericMessage<String>("Hello, world!");
    handler.handleMessage(message);
    Message<?> error = errors.receive(10000);
    assertNotNull(error);
    assertTrue(error.getPayload() instanceof ErrorMessageSendingRecoverer.RetryExceptionNotAvailableException);
    assertNotNull(((MessagingException) error.getPayload()).getFailedMessage());
    assertSame(message, ((MessagingException) error.getPayload()).getFailedMessage());
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) RetryContext(org.springframework.retry.RetryContext) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) GenericMessage(org.springframework.messaging.support.GenericMessage) RetryTemplate(org.springframework.retry.support.RetryTemplate) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Advice(org.aopalliance.aop.Advice) Test(org.junit.Test)

Example 3 with RetryContext

use of org.springframework.retry.RetryContext in project spring-cloud-netflix by spring-cloud.

the class RetryableOkHttpLoadBalancingClient method execute.

@Override
public OkHttpRibbonResponse execute(final OkHttpRibbonRequest ribbonRequest, final IClientConfig configOverride) throws Exception {
    final LoadBalancedRetryPolicy retryPolicy = loadBalancedRetryFactory.createRetryPolicy(this.getClientName(), this);
    RetryCallback<OkHttpRibbonResponse, Exception> retryCallback = new RetryCallback<OkHttpRibbonResponse, Exception>() {

        @Override
        public OkHttpRibbonResponse doWithRetry(RetryContext context) throws Exception {
            // on retries the policy will choose the server and set it in the context
            // extract the server and update the request being made
            OkHttpRibbonRequest newRequest = ribbonRequest;
            if (context instanceof LoadBalancedRetryContext) {
                ServiceInstance service = ((LoadBalancedRetryContext) context).getServiceInstance();
                validateServiceInstance(service);
                // Reconstruct the request URI using the host and port set in the retry context
                newRequest = newRequest.withNewUri(new URI(service.getUri().getScheme(), newRequest.getURI().getUserInfo(), service.getHost(), service.getPort(), newRequest.getURI().getPath(), newRequest.getURI().getQuery(), newRequest.getURI().getFragment()));
            }
            if (isSecure(configOverride)) {
                final URI secureUri = UriComponentsBuilder.fromUri(newRequest.getUri()).scheme("https").build().toUri();
                newRequest = newRequest.withNewUri(secureUri);
            }
            OkHttpClient httpClient = getOkHttpClient(configOverride, secure);
            final Request request = newRequest.toRequest();
            Response response = httpClient.newCall(request).execute();
            if (retryPolicy.retryableStatusCode(response.code())) {
                ResponseBody responseBody = response.peekBody(Integer.MAX_VALUE);
                response.close();
                throw new OkHttpStatusCodeException(RetryableOkHttpLoadBalancingClient.this.clientName, response, responseBody, newRequest.getURI());
            }
            return new OkHttpRibbonResponse(response, newRequest.getUri());
        }
    };
    return this.executeWithRetry(ribbonRequest, retryPolicy, retryCallback, new LoadBalancedRecoveryCallback<OkHttpRibbonResponse, Response>() {

        @Override
        protected OkHttpRibbonResponse createResponse(Response response, URI uri) {
            return new OkHttpRibbonResponse(response, uri);
        }
    });
}
Also used : LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) OkHttpClient(okhttp3.OkHttpClient) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) RetryContext(org.springframework.retry.RetryContext) HttpRequest(org.springframework.http.HttpRequest) Request(okhttp3.Request) ContextAwareRequest(org.springframework.cloud.netflix.ribbon.support.ContextAwareRequest) ServiceInstance(org.springframework.cloud.client.ServiceInstance) URI(java.net.URI) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) RetryCallback(org.springframework.retry.RetryCallback)

Example 4 with RetryContext

use of org.springframework.retry.RetryContext in project molgenis by molgenis.

the class ConnectionRetryConfigTest method testRetryPolicyInterrupted.

@Test
public void testRetryPolicyInterrupted() {
    RetryContext context = retryPolicy.open(null);
    retryPolicy.registerThrowable(context, new InterruptedException("Going down"));
    assertFalse(retryPolicy.canRetry(context));
}
Also used : RetryContext(org.springframework.retry.RetryContext) Test(org.testng.annotations.Test)

Example 5 with RetryContext

use of org.springframework.retry.RetryContext in project molgenis by molgenis.

the class ConnectionRetryConfigTest method testRetryPolicyMolgenisDataException.

@Test
public void testRetryPolicyMolgenisDataException() {
    RetryContext context = retryPolicy.open(null);
    retryPolicy.registerThrowable(context, new MolgenisDataException("Failed to connect"));
    assertTrue(retryPolicy.canRetry(context));
}
Also used : MolgenisDataException(org.molgenis.data.MolgenisDataException) RetryContext(org.springframework.retry.RetryContext) Test(org.testng.annotations.Test)

Aggregations

RetryContext (org.springframework.retry.RetryContext)7 DhisSpringTest (org.hisp.dhis.DhisSpringTest)2 Test (org.junit.Test)2 Test (org.junit.jupiter.api.Test)2 Test (org.testng.annotations.Test)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1 Advice (org.aopalliance.aop.Advice)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)1 MetadataRetryContext (org.hisp.dhis.dxf2.metadata.jobs.MetadataRetryContext)1 MolgenisDataException (org.molgenis.data.MolgenisDataException)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 ServiceInstance (org.springframework.cloud.client.ServiceInstance)1 LoadBalancedRetryContext (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext)1 LoadBalancedRetryPolicy (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy)1