spring retry开源项目例子

https://github.com/spring-projects/spring-retry,
实现的功能是比如对于耗费资源的可以进行多次重试

例子如下:
比如一个实际模拟获得汇率的转换的例子
public interface ExchangeRateCalculator {
	public abstract Double getCurrentRate();
}


模拟出错,重新试验10次,然后最终还是不行的话,调用recover方法

@Service
public class AdvancedDummyExchangeRateCalculator implements ExchangeRateCalculator {
	
	private static final double BASE_EXCHANGE_RATE = 1.09;
	private int attempts = 0;
	private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

	@Retryable(value=RuntimeException.class,maxAttempts=10,backoff = @Backoff(delay = 5000,multiplier=1.5))
	public Double getCurrentRate(){
		System.out.println("Calculating - Attempt " + attempts + " at " + sdf.format(new Date()));
		attempts++;
		.
		throw new RuntimeException("Error");
	}
	
	@Recover
	public Double recover(RuntimeException e){
		System.out.println("Recovering - returning safe value");
		return BASE_EXCHANGE_RATE;
	}

你可能感兴趣的:(spring retry开源项目例子)