| | |
| | | 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; |
| | |
| | | */ |
| | | @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()); |
| | | |
| | | boolean success = false; |
| | | 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.getInteger("code"); |
| | | if (code == 200) { |
| | | // 通知成功 |
| | | redisUtil.del(key); |
| | | success = true; |
| | | 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 (!success) { |
| | | // 通知失败,更新重试次数 |
| | | if (!result.isSuccess() && updateRetryState && key != null) { |
| | | handleNotifyFailure(key, notifyDto); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |