欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用spring-retry实现支付系统异步通知

程序员文章站 2022-03-07 10:28:48
...

支付系统异步通知承接上文:DelayQueue实现支付系统异步通知 

本篇介绍通过spring-retry来实现支付系统异步通知功能。

 

1、添加所需的jar

<dependency>
	<groupId>org.springframework.retry</groupId>
	<artifactId>spring-retry</artifactId>
	<version>1.1.2.RELEASE</version>
</dependency>

 2、实现任务重试服务

package com.huatech.service;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import com.huatech.common.delay.RetMessage;
import com.huatech.common.util.HttpsUtil;
/**
 * 任务重试服务
 * 
 * 	第一次成功则不再重试;
 * 	第一次失败,10分钟后重试第二次;
 * 	第二次失败,20分钟后重试第三次;
 * 	第三次失败,40分钟后重试第四次;
 * 	第四次失败,60分钟后重试第五次;
 * @author lh
 *
 */
//@Configuration
@EnableRetry
@Service
public class TaskRetryService {
	
	
	private static final Logger LOGGER = LoggerFactory.getLogger(TaskRetryService.class);
	
	//返回结果
	private static final String RES_SUCCESS = "success";
	//时间单位:10min
	private static final long TIME_UNIT = 1000 * 60 * 10;
	//重试次数
	private static final int MAX_ATTEMPTS = 5;
	
	 @Retryable(value = {RetryException.class, RuntimeException.class},
	            maxAttempts = MAX_ATTEMPTS,
	            backoff = @Backoff(delay = TIME_UNIT, maxDelay=TIME_UNIT * 6, multiplier= 2 ))
	public void notice(RetMessage msg){
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("reqData", msg.getReqData());
		String httpResult = null;
		try {
			httpResult = HttpsUtil.getInstance().doPostRetString(msg.getUrl(), null, paramMap);
			LOGGER.info("第{}次异步回调,返回结果{},返回参数:{},响应结果:{}", msg.getTimes(), httpResult,
					paramMap.get("reqData"), RES_SUCCESS.equals(httpResult));
			if (!RES_SUCCESS.equals(httpResult)) {
				msg.setTimes(msg.getTimes() + 1);
				msg.setSuccess(false);
				throw new RetryException("retry failed");
			}else{
				msg.setSuccess(true);
			}
		} catch (URISyntaxException | IOException e) {
		}		
	}

}