Skip to content

Commit

Permalink
implement retry template
Browse files Browse the repository at this point in the history
implement exponential backoff strategy
  • Loading branch information
deepcloudlabs committed Jun 24, 2020
1 parent f588e1f commit 7bad1ed
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.lottery.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;

/**
*
* @author Binnur Kurt <binnur.kurt@gmail.com>
*
*/
@Configuration
public class RetryTemplateConfig {
@Bean
public RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate();

ExponentialBackOffPolicy exponentialBackOffPolicy = new ExponentialBackOffPolicy();
exponentialBackOffPolicy.setInitialInterval(1000L);
retryTemplate.setBackOffPolicy(exponentialBackOffPolicy);

SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(3);
retryTemplate.setRetryPolicy(retryPolicy);
return retryTemplate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.lottery.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.RecoveryCallback;
import org.springframework.retry.RetryCallback;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.example.lottery.dto.LotteryResponse;

@Service
public class LotteryConsumerServiceWithRetryTemplate {
@Autowired
private RetryTemplate retryTemplate;

@Scheduled(fixedRate = 12_000)
public void doSomething() {
RetryCallback<LotteryResponse, Exception> retryCallback = context -> {
System.err.println("Calling lottery service from LotteryConsumerServiceWithRetryTemplate...");
var rt = new RestTemplate();
var response = rt.getForObject("http://localhost:8001/lottery/api/v1/numbers?column=10",
LotteryResponse.class);
System.err.println(response);
return response;
};
RecoveryCallback<LotteryResponse> recoveryCallback = context -> {
System.err.println("Calling recoveryCallback from LotteryConsumerServiceWithRetryTemplate...");
return new LotteryResponse(List.of(List.of(5, 10, 15, 20, 25, 30)));
};
try {
var response = retryTemplate.execute(retryCallback, recoveryCallback);
System.out.println(response);
} catch (Exception e) {
System.err.println(e.getMessage());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
public class SimpleDemoService {
@Autowired
private LotteryConsumerServiceWithCircuitBreaker srv;
@Autowired
private StudyRetryStrategy retryStrategy;

@Scheduled(fixedRate = 2_000)
public void doSomething() {
Expand All @@ -23,4 +25,14 @@ public void doSomething() {
String.format("Exception (%s) in doSomething(): %s", e.getClass().getName(), e.getMessage()));
}
}

@Scheduled(fixedRate = 5_000)
public void doSomethingElse() {
try {
System.out.println(retryStrategy.getNumbers());
} catch (Exception e) {
System.err.println(
String.format("Exception (%s) in doSomething(): %s", e.getClass().getName(), e.getMessage()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.List;

import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.example.lottery.dto.LotteryResponse;
import com.google.inject.Exposed;

/**
*
Expand All @@ -17,12 +20,18 @@
*/
@Service
public class StudyRetryStrategy {
@Retryable(value= {SocketTimeoutException.class, SocketException.class},
maxAttempts = 3, backoff=@Backoff(delay = 3_000))

@Retryable(value = { SocketTimeoutException.class, // exponential backoff: 2^{n} * 3 seconds
SocketException.class }, maxAttempts = 3, backoff = @Backoff(multiplier = 2, delay = 3_000))
public LotteryResponse getNumbers() {
System.err.println("Calling lottery service from StudyRetryStrategy...");
RestTemplate rt = new RestTemplate();
return rt.getForObject("http://localhost:8001/lottery/api/v1/numbers?column=10", LotteryResponse.class);
}

@Recover
public LotteryResponse getNumbersFallback(Exception e) {
return new LotteryResponse(List.of(List.of(1, 3, 5, 7, 9, 11), List.of(2, 4, 6, 8, 10, 12)));
}

}

0 comments on commit 7bad1ed

Please sign in to comment.