#
Junjie
18 小时以前 86e79681da9c98bd08bd1f2be0c6dbd3f3dd3159
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.zy.asrs.service;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zy.asrs.domain.NotifyDto;
import com.zy.asrs.domain.NotifySendResult;
import com.zy.asrs.entity.HttpRequestLog;
import com.zy.common.utils.HttpHandler;
import com.zy.common.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import java.util.Date;
 
/**
 * 异步通知服务
 * 用于异步发送HTTP通知请求,避免阻塞定时器线程
 */
@Service
@Slf4j
public class NotifyAsyncService {
 
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private HttpRequestLogService httpRequestLogService;
 
    /**
     * 异步发送通知
     * 
     * @param notifyUri     通知URI
     * @param notifyUriPath 通知路径
     * @param key           Redis键
     * @param notifyDto     通知数据
     */
    @Async
    public void sendNotifyAsync(String notifyUri, String notifyUriPath, String key, NotifyDto notifyDto) {
        sendNotifyNow(notifyUri, notifyUriPath, key, notifyDto, true, true);
    }
 
    public NotifySendResult sendNotifyNow(String notifyUri, String notifyUriPath, String key, NotifyDto notifyDto,
                                          boolean deleteOnSuccess, boolean updateRetryState) {
        HttpRequestLog httpRequestLog = new HttpRequestLog();
        httpRequestLog.setName(notifyUri + notifyUriPath);
        httpRequestLog.setRequest(JSON.toJSONString(notifyDto));
        httpRequestLog.setCreateTime(new Date());
 
        NotifySendResult result = new NotifySendResult();
        result.setSuccess(false);
        try {
            String response = new HttpHandler.Builder()
                    .setUri(notifyUri)
                    .setPath(notifyUriPath)
                    .setJson(JSON.toJSONString(notifyDto))
                    .build()
                    .doPost();
            result.setResponse(response);
            httpRequestLog.setResponse(response);
 
            JSONObject jsonObject = JSON.parseObject(response);
            Integer code = jsonObject == null ? null : jsonObject.getInteger("code");
            if (code != null && code == 200) {
                if (deleteOnSuccess && key != null) {
                    redisUtil.del(key);
                }
                result.setSuccess(true);
                result.setMessage("通知成功");
            } else {
                result.setMessage("通知接口返回失败");
            }
        } catch (Exception e) {
            log.error("异步通知失败, key={}", key, e);
            result.setMessage("通知异常: " + e.getMessage());
        } finally {
            httpRequestLog.setResult(result.isSuccess() ? 1 : 0);
            httpRequestLogService.insert(httpRequestLog);
        }
 
        if (!result.isSuccess() && updateRetryState && key != null) {
            handleNotifyFailure(key, notifyDto);
        }
        return result;
    }
 
    /**
     * 处理通知失败的情况
     */
    private void handleNotifyFailure(String key, NotifyDto notifyDto) {
        try {
            int times = notifyDto.getRetryTimes() + 1;
            if (times >= notifyDto.getFailTimes()) {
                // 超过次数
                redisUtil.del(key);
                return;
            }
 
            notifyDto.setLastRetryTime(System.currentTimeMillis());
            notifyDto.setRetryTimes(times);
            redisUtil.set(key, notifyDto);
        } catch (Exception e) {
            log.error("处理通知失败逻辑异常, key={}", key, e);
        }
    }
}