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

阅读更多

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

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

 

1、添加所需的jar


	org.springframework.retry
	spring-retry
	1.1.2.RELEASE

 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 paramMap = new HashMap();
		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) {
		}		
	}

}

 

你可能感兴趣的:(java,spring-retry,异步通知)