Search in sources :

Example 1 with FixedBackOffPolicy

use of org.springframework.retry.backoff.FixedBackOffPolicy in project spring-kafka by spring-projects.

the class DestinationTopicPropertiesFactoryTests method shouldCreateOneRetryPropertyForFixedBackoffWithSingleTopicStrategy.

@Test
void shouldCreateOneRetryPropertyForFixedBackoffWithSingleTopicStrategy() {
    // when
    FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
    backOffPolicy.setBackOffPeriod(1000);
    int maxAttempts = 5;
    List<Long> backOffValues = new BackOffValuesGenerator(maxAttempts, backOffPolicy).generateValues();
    List<DestinationTopic.Properties> propertiesList = new DestinationTopicPropertiesFactory(retryTopicSuffix, dltSuffix, backOffValues, classifier, numPartitions, kafkaOperations, FixedDelayStrategy.SINGLE_TOPIC, dltStrategy, defaultTopicSuffixingStrategy, -1).createProperties();
    List<DestinationTopic> destinationTopicList = propertiesList.stream().map(properties -> new DestinationTopic("mainTopic" + properties.suffix(), properties)).collect(Collectors.toList());
    // then
    assertThat(propertiesList.size() == 3).isTrue();
    DestinationTopic.Properties mainTopicProperties = propertiesList.get(0);
    DestinationTopic mainDestinationTopic = destinationTopicList.get(0);
    assertThat(mainDestinationTopic.isMainTopic()).isTrue();
    DestinationTopic.Properties firstRetryProperties = propertiesList.get(1);
    assertThat(firstRetryProperties.suffix()).isEqualTo(retryTopicSuffix);
    DestinationTopic retryDestinationTopic = destinationTopicList.get(1);
    assertThat(retryDestinationTopic.isSingleTopicRetry()).isTrue();
    assertThat(retryDestinationTopic.getDestinationDelay()).isEqualTo(1000);
    DestinationTopic.Properties dltProperties = propertiesList.get(2);
    assertThat(dltProperties.suffix()).isEqualTo(dltSuffix);
    assertThat(dltProperties.isDltTopic()).isTrue();
    DestinationTopic dltTopic = destinationTopicList.get(2);
    assertThat(dltTopic.getDestinationDelay()).isEqualTo(0);
    assertThat(dltTopic.getDestinationPartitions()).isEqualTo(numPartitions);
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Mock(org.mockito.Mock) ExponentialBackOffPolicy(org.springframework.retry.backoff.ExponentialBackOffPolicy) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BinaryExceptionClassifier(org.springframework.classify.BinaryExceptionClassifier) Collectors(java.util.stream.Collectors) KafkaOperations(org.springframework.kafka.core.KafkaOperations) BinaryExceptionClassifierBuilder(org.springframework.classify.BinaryExceptionClassifierBuilder) Test(org.junit.jupiter.api.Test) BackOffPolicy(org.springframework.retry.backoff.BackOffPolicy) List(java.util.List) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) FixedBackOffPolicy(org.springframework.retry.backoff.FixedBackOffPolicy) FixedBackOffPolicy(org.springframework.retry.backoff.FixedBackOffPolicy) Test(org.junit.jupiter.api.Test)

Example 2 with FixedBackOffPolicy

use of org.springframework.retry.backoff.FixedBackOffPolicy in project spring-kafka by spring-projects.

the class DestinationTopicPropertiesFactoryTests method shouldCreateRetryPropertiesForFixedBackoffWithMultiTopicStrategy.

@Test
void shouldCreateRetryPropertiesForFixedBackoffWithMultiTopicStrategy() {
    // when
    FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
    backOffPolicy.setBackOffPeriod(5000);
    int maxAttempts = 3;
    List<Long> backOffValues = new BackOffValuesGenerator(maxAttempts, backOffPolicy).generateValues();
    List<DestinationTopic.Properties> propertiesList = new DestinationTopicPropertiesFactory(retryTopicSuffix, dltSuffix, backOffValues, classifier, numPartitions, kafkaOperations, FixedDelayStrategy.MULTIPLE_TOPICS, dltStrategy, defaultTopicSuffixingStrategy, -1).createProperties();
    List<DestinationTopic> destinationTopicList = propertiesList.stream().map(properties -> new DestinationTopic("mainTopic" + properties.suffix(), properties)).collect(Collectors.toList());
    // then
    assertThat(propertiesList.size() == 4).isTrue();
    DestinationTopic.Properties mainTopicProperties = propertiesList.get(0);
    DestinationTopic mainDestinationTopic = destinationTopicList.get(0);
    assertThat(mainDestinationTopic.isMainTopic()).isTrue();
    DestinationTopic.Properties firstRetryProperties = propertiesList.get(1);
    assertThat(firstRetryProperties.suffix()).isEqualTo(retryTopicSuffix + "-0");
    DestinationTopic retryDestinationTopic = destinationTopicList.get(1);
    assertThat(retryDestinationTopic.isSingleTopicRetry()).isFalse();
    assertThat(retryDestinationTopic.getDestinationDelay()).isEqualTo(5000);
    DestinationTopic.Properties secondRetryProperties = propertiesList.get(2);
    assertThat(secondRetryProperties.suffix()).isEqualTo(retryTopicSuffix + "-1");
    DestinationTopic secondRetryDestinationTopic = destinationTopicList.get(2);
    assertThat(secondRetryDestinationTopic.isSingleTopicRetry()).isFalse();
    assertThat(secondRetryDestinationTopic.getDestinationDelay()).isEqualTo(5000);
    DestinationTopic.Properties dltProperties = propertiesList.get(3);
    assertThat(dltProperties.suffix()).isEqualTo(dltSuffix);
    assertThat(dltProperties.isDltTopic()).isTrue();
    DestinationTopic dltTopic = destinationTopicList.get(3);
    assertThat(dltTopic.getDestinationDelay()).isEqualTo(0);
    assertThat(dltTopic.getDestinationPartitions()).isEqualTo(numPartitions);
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Mock(org.mockito.Mock) ExponentialBackOffPolicy(org.springframework.retry.backoff.ExponentialBackOffPolicy) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BinaryExceptionClassifier(org.springframework.classify.BinaryExceptionClassifier) Collectors(java.util.stream.Collectors) KafkaOperations(org.springframework.kafka.core.KafkaOperations) BinaryExceptionClassifierBuilder(org.springframework.classify.BinaryExceptionClassifierBuilder) Test(org.junit.jupiter.api.Test) BackOffPolicy(org.springframework.retry.backoff.BackOffPolicy) List(java.util.List) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) FixedBackOffPolicy(org.springframework.retry.backoff.FixedBackOffPolicy) FixedBackOffPolicy(org.springframework.retry.backoff.FixedBackOffPolicy) Test(org.junit.jupiter.api.Test)

Example 3 with FixedBackOffPolicy

use of org.springframework.retry.backoff.FixedBackOffPolicy in project rhsm-subscriptions by RedHatInsights.

the class TallyWorkerConfiguration method cloudigradeRetryTemplate.

@Bean(name = "cloudigradeRetryTemplate")
public RetryTemplate cloudigradeRetryTemplate(ApplicationProperties applicationProperties) {
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
    retryPolicy.setMaxAttempts(applicationProperties.getCloudigradeMaxAttempts());
    FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
    backOffPolicy.setBackOffPeriod(2000L);
    RetryTemplate retryTemplate = new RetryTemplate();
    retryTemplate.setRetryPolicy(retryPolicy);
    retryTemplate.setBackOffPolicy(backOffPolicy);
    return retryTemplate;
}
Also used : RetryTemplate(org.springframework.retry.support.RetryTemplate) FixedBackOffPolicy(org.springframework.retry.backoff.FixedBackOffPolicy) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) Bean(org.springframework.context.annotation.Bean)

Example 4 with FixedBackOffPolicy

use of org.springframework.retry.backoff.FixedBackOffPolicy in project powop by RBGKew.

the class GetResourceClient method getWithRetry.

public ExitStatus getWithRetry(RetryCallback<ExitStatus, Exception> callback) {
    RetryTemplate retryTemplate = new RetryTemplate();
    FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
    Map<Class<? extends Throwable>, Boolean> retryableExceptions = new HashMap<Class<? extends Throwable>, Boolean>();
    retryableExceptions.put(ClientProtocolException.class, Boolean.TRUE);
    retryableExceptions.put(IOException.class, Boolean.TRUE);
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(retryAttempts, retryableExceptions);
    backOffPolicy.setBackOffPeriod(backoffPeriod);
    retryTemplate.setListeners(retryListeners);
    retryTemplate.setBackOffPolicy(backOffPolicy);
    retryTemplate.setRetryPolicy(retryPolicy);
    try {
        return retryTemplate.execute(callback);
    } catch (Exception e) {
        logger.error("Retry processing failed " + e.getMessage());
        return ExitStatus.FAILED;
    }
}
Also used : RetryTemplate(org.springframework.retry.support.RetryTemplate) FixedBackOffPolicy(org.springframework.retry.backoff.FixedBackOffPolicy) HashMap(java.util.HashMap) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException)

Example 5 with FixedBackOffPolicy

use of org.springframework.retry.backoff.FixedBackOffPolicy in project spring-boot-examples by qct.

the class RetryServiceMain method main.

public static void main(String[] args) throws Throwable {
    final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(RetryServiceMain.class);
    final RetryService retryService = applicationContext.getBean(RetryService.class);
    RetryTemplate template = new RetryTemplate();
    template.setRetryPolicy(new SimpleRetryPolicy());
    template.setBackOffPolicy(new FixedBackOffPolicy());
    template.registerListener(new StatisticsListener(new DefaultStatisticsRepository()));
    Object execute = template.execute(new RetryCallback<Integer, Exception>() {

        int i = 5;

        @Override
        public Integer doWithRetry(RetryContext context) throws Exception {
            throw new Exception();
        }
    });
// String result = retryService.someService();
// System.out.println(result);
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) FixedBackOffPolicy(org.springframework.retry.backoff.FixedBackOffPolicy) StatisticsListener(org.springframework.retry.stats.StatisticsListener) RetryContext(org.springframework.retry.RetryContext) SimpleRetryPolicy(org.springframework.retry.policy.SimpleRetryPolicy) RetryTemplate(org.springframework.retry.support.RetryTemplate) DefaultStatisticsRepository(org.springframework.retry.stats.DefaultStatisticsRepository)

Aggregations

FixedBackOffPolicy (org.springframework.retry.backoff.FixedBackOffPolicy)38 RetryTemplate (org.springframework.retry.support.RetryTemplate)26 SimpleRetryPolicy (org.springframework.retry.policy.SimpleRetryPolicy)23 Bean (org.springframework.context.annotation.Bean)14 Test (org.junit.jupiter.api.Test)5 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 AmazonClientException (com.amazonaws.AmazonClientException)2 AmazonServiceException (com.amazonaws.AmazonServiceException)2 HttpMethod (com.amazonaws.HttpMethod)2 STSAssumeRoleSessionCredentialsProvider (com.amazonaws.auth.STSAssumeRoleSessionCredentialsProvider)2 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)2 Headers (com.amazonaws.services.s3.Headers)2 AbortMultipartUploadRequest (com.amazonaws.services.s3.model.AbortMultipartUploadRequest)2 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)2 CopyObjectRequest (com.amazonaws.services.s3.model.CopyObjectRequest)2 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)2 GeneratePresignedUrlRequest (com.amazonaws.services.s3.model.GeneratePresignedUrlRequest)2 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)2 GetObjectTaggingRequest (com.amazonaws.services.s3.model.GetObjectTaggingRequest)2