Search in sources :

Example 6 with OptimisticLockingRetryException

use of uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException in project microservice_framework by CJSCommonPlatform.

the class RetryInterceptorTest method shouldRetryIfExceptionThrownByDispatcher.

@Test
public void shouldRetryIfExceptionThrownByDispatcher() throws Exception {
    final InterceptorContext currentContext = interceptorContextWithInput(envelope().with(metadataWithRandomUUID("nameABC")).build());
    final InterceptorContext nextInChain = interceptorContextWithInput(mock(JsonEnvelope.class));
    when(interceptorChain.processNext(currentContext)).thenThrow(new OptimisticLockingRetryException("Locking Error")).thenReturn(nextInChain);
    retryInterceptor.maxRetry = "2";
    retryInterceptor.waitTime = "500";
    retryInterceptor.immediateRetries = "0";
    assertThat(retryInterceptor.process(currentContext, interceptorChain), is(nextInChain));
}
Also used : InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) OptimisticLockingRetryException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException) Test(org.junit.Test)

Example 7 with OptimisticLockingRetryException

use of uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException in project microservice_framework by CJSCommonPlatform.

the class RetryInterceptor method process.

@Override
public InterceptorContext process(final InterceptorContext interceptorContext, final InterceptorChain interceptorChain) {
    final int maxRetryCount = parseInt(maxRetry);
    final int retryWaitTime = parseInt(waitTime);
    final int maxImmediateRetries = parseInt(immediateRetries);
    final JsonObject metadata = interceptorContext.inputEnvelope().metadata().asJsonObject();
    int retries = 0;
    while (maxRetryCount == 0 || retries < maxRetryCount) {
        try {
            return interceptorChain.processNext(interceptorContext);
        } catch (OptimisticLockingRetryException e) {
            logger.debug(format("Optimistic locking failed on command %s at retry attempt %d", metadata, retries));
            retries++;
            if (retries > maxImmediateRetries) {
                waitFor(retryWaitTime);
            }
        }
    }
    throw new OptimisticLockingRetryFailedException(format("Retry count of %d exceeded for command %s", maxRetryCount, metadata));
}
Also used : JsonObject(javax.json.JsonObject) OptimisticLockingRetryException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException)

Aggregations

OptimisticLockingRetryException (uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException)7 Test (org.junit.Test)5 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)5 InterceptorContext (uk.gov.justice.services.core.interceptor.InterceptorContext)3 UUID (java.util.UUID)1 JsonObject (javax.json.JsonObject)1 Transactional (javax.transaction.Transactional)1 MetadataBuilderFactory.metadataWithRandomUUID (uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID)1