update 添加回调接口重试机制、发送企业微信通知

master
yinq 2 months ago
parent 911ff84ea5
commit 6dd66a44c9

@ -19,7 +19,7 @@ public class ApiConstants {
/**
* () 6
*/
public static final Long PASS_TIME = 21600000L;
public static final Long PASS_TIME = 86400000L;
/**
* TID\EPC
@ -27,9 +27,9 @@ public class ApiConstants {
public static final Integer TE_LENGTH = 24;
/**
*
* zip
*/
public static final Long CHECK_MAX_DATA = 10000L;
public static final Long ZIP_MAX_DATA = 100000L;
/**
*
@ -37,6 +37,11 @@ public class ApiConstants {
public static final String CALLBACK_URL = "https://www.kdocs.cn/chatflow/api/v2/func/webhook/2w7TqqdGMqmLwdfQY3wFidcBF4K";//正式
// public static final String CALLBACK_URL = "https://www.kdocs.cn/chatflow/api/v2/func/webhook/2uZIRSpxLvtfaoCjRoLg4EvwIc5";//测试
/**
* webhook
*/
public static final String WECHAT_WEBHOOK_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=0853ff4a-a6a0-4f99-a897-63f8cad3d332";
/**
*
*/

@ -219,7 +219,7 @@ public class HwTagRecordServiceImpl implements IHwTagRecordService {
record.setOrderCode(lot);
record.setBatchNumber(orderInfo.getOrDefault("batchNo", ""));
record.setOperatorId(orderInfo.getOrDefault("operatorId", ""));
Date processTime = orderInfo.containsKey("processTime") ?
Date processTime = orderInfo.containsKey("processTime") && !orderInfo.get("processTime").isEmpty() ?
sdf.parse(orderInfo.get("processTime")) : nowDate;
record.setProcessingTime(processTime);
record.setTotalQuantity(Long.valueOf(orderInfo.getOrDefault("totalCount", "0")));
@ -335,7 +335,7 @@ public class HwTagRecordServiceImpl implements IHwTagRecordService {
*
*
* @param tagRecordList
* @param params
* @param params
*/
private void convertExcelDataTemplateOne(List<HwTagRecord> tagRecordList, ImportParams params) {
for (HwTagRecord tagRecord : tagRecordList) {

@ -14,10 +14,14 @@ import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import java.util.List;
import static hw.tagApi.service.constant.ApiConstants.WECHAT_WEBHOOK_URL;
public class httpClientUtils {
private static final Logger log = LoggerFactory.getLogger(httpClientUtils.class);
private static final ObjectMapper objectMapper = new ObjectMapper();
@ -31,6 +35,10 @@ public class httpClientUtils {
.setSocketTimeout(SOCKET_TIMEOUT)
.build();
// 重试配置
private static final int MAX_RETRIES = 2;
private static final int RETRY_INTERVAL = 1000; // 1秒
/**
* HttpGet
*
@ -87,9 +95,11 @@ public class httpClientUtils {
}
/**
*
*
*
* @param resList
* @param resList
* @param url URL
* @param caseCode
*/
public static void sendResultToCallback(List<ApiContent> resList, String url, String caseCode) {
if (resList == null || resList.isEmpty()) {
@ -98,48 +108,122 @@ public class httpClientUtils {
}
HttpPost httpPost = null;
int retryCount = 0;
boolean success = false;
String errorMessage = "";
String jsonBody = "";
while (!success && retryCount < MAX_RETRIES) {
try {
// 创建POST请求
httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
// 设置请求头
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Connection", "close");
// 创建响应对象
ApiResponse response = new ApiResponse();
response.setCaseCode(caseCode);
response.setResult(resList);
// 将对象转换为JSON字符串
jsonBody = objectMapper.writeValueAsString(response);
log.info("准备发送的数据: {}", jsonBody);
// 设置请求体
StringEntity entity = new StringEntity(jsonBody, "UTF-8");
httpPost.setEntity(entity);
// 发送请求
try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
HttpEntity responseEntity = httpResponse.getEntity();
String responseBody = responseEntity != null ? EntityUtils.toString(responseEntity, "UTF-8") : "";
if (statusCode == 200) {
log.info("发送处理结果到回调接口成功,响应状态码: {}", statusCode);
log.info("回调接口响应内容: {}", responseBody);
success = true;
} else {
errorMessage = String.format("回调失败 - 状态码: %d, 响应内容: %s, 请求数据: %s",
statusCode, responseBody, jsonBody);
log.error(errorMessage);
retryCount++;
if (retryCount < MAX_RETRIES) {
log.info("将在{}秒后进行第{}次重试", RETRY_INTERVAL / 1000, retryCount + 1);
Thread.sleep(RETRY_INTERVAL);
}
}
}
} catch (Exception e) {
errorMessage = String.format("回调失败 - 异常信息: %s, 请求数据: %s", e.getMessage(), jsonBody);
log.error(errorMessage);
retryCount++;
if (retryCount < MAX_RETRIES) {
log.info("catch将在{}秒后进行第{}次重试", RETRY_INTERVAL / 1000, retryCount + 1);
try {
Thread.sleep(RETRY_INTERVAL);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
} finally {
if (httpPost != null) {
httpPost.releaseConnection();
}
}
}
if (!success) {
String finalMessage = String.format("回调接口调用失败 - CASE代码: %s\n重试次数: %d\n错误信息: %s",
caseCode, MAX_RETRIES, errorMessage);
log.info(finalMessage);
// 发送企业微信通知
sendWechatNotification(finalMessage);
}
}
/**
*
*
* @param message
*/
private static void sendWechatNotification(String message) {
try {
// 创建POST请求
httpPost = new HttpPost(url);
String webhookUrl = WECHAT_WEBHOOK_URL;
HttpPost httpPost = new HttpPost(webhookUrl);
httpPost.setConfig(requestConfig);
// 设置请求头
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Connection", "close");
// 创建响应对象
ApiResponse response = new ApiResponse();
response.setCaseCode(caseCode);
response.setResult(resList);
// 构建消息体
ObjectNode rootNode = objectMapper.createObjectNode();
rootNode.put("msgtype", "text");
ObjectNode textNode = rootNode.putObject("text");
textNode.put("content", message);
// 将对象转换为JSON字符串
String jsonBody = objectMapper.writeValueAsString(response);
log.info("准备发送的数据: {}", jsonBody);
// 设置请求体
String jsonBody = objectMapper.writeValueAsString(rootNode);
StringEntity entity = new StringEntity(jsonBody, "UTF-8");
httpPost.setEntity(entity);
// 发送请求
try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
HttpEntity responseEntity = httpResponse.getEntity();
String responseBody = responseEntity != null ? EntityUtils.toString(responseEntity, "UTF-8") : "";
if (statusCode == 200) {
log.info("发送处理结果到回调接口成功,响应状态码: {}", statusCode);
log.info("回调接口响应内容: {}", responseBody);
} else {
log.error("发送处理结果到回调接口失败,响应状态码: {}, 响应内容: {}", statusCode, responseBody);
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
String responseBody = EntityUtils.toString(responseEntity, "UTF-8");
JsonNode jsonResponse = objectMapper.readTree(responseBody);
int errcode = jsonResponse.get("errcode").asInt();
String errmsg = jsonResponse.get("errmsg").asText();
if (errcode == 0 && "ok".equals(errmsg)) {
log.info("企业微信通知发送成功");
} else {
log.error("企业微信通知发送失败 - errcode: {}, errmsg: {}", errcode, errmsg);
}
}
}
} catch (Exception e) {
log.error("发送处理结果到回调接口失败: {}", e.getMessage());
} finally {
if (httpPost != null) {
httpPost.releaseConnection();
}
log.error("发送企业微信通知时发生错误: {}", e.getMessage());
}
}

@ -17,14 +17,14 @@
<result property="modelCode" column="model_code"/>
<result property="processingTime" column="processing_time"/>
<result property="operatorId" column="operator_id"/>
<result property="testResult" column="test_result" />
<result property="testValue" column="test_value" />
<result property="referenceValue" column="reference_value" />
<result property="testingTime" column="testing_time" />
<result property="fileName" column="file_name" />
<result property="destinationInfo" column="destination_info" />
<result property="libraryFlag" column="library_flag" />
<result property="intendedUse" column="intended_use" />
<result property="testResult" column="test_result"/>
<result property="testValue" column="test_value"/>
<result property="referenceValue" column="reference_value"/>
<result property="testingTime" column="testing_time"/>
<result property="fileName" column="file_name"/>
<result property="destinationInfo" column="destination_info"/>
<result property="libraryFlag" column="library_flag"/>
<result property="intendedUse" column="intended_use"/>
<result property="delFlag" column="del_flag"/>
<result property="fields1" column="fields_1"/>
<result property="fields2" column="fields_2"/>
@ -87,13 +87,13 @@
and processing_time between #{params.beginProcessingTime} and #{params.endProcessingTime}
</if>
<if test="operatorId != null and operatorId != ''">and operator_id = #{operatorId}</if>
<if test="destinationInfo != null and destinationInfo != ''"> and destination_info = #{destinationInfo}</if>
<if test="libraryFlag != null and libraryFlag != ''"> and library_flag = #{libraryFlag}</if>
<if test="intendedUse != null and intendedUse != ''"> and intended_use = #{intendedUse}</if>
<if test="remark != null and remark != ''"> and remark like concat('%', #{remark}, '%')</if>
<if test="fields1 != null and fields1 != ''"> and fields_1 = #{fields1}</if>
<if test="fields2 != null and fields2 != ''"> and fields_2 = #{fields2}</if>
<if test="fields3 != null and fields3 != ''"> and fields_3 = #{fields3}</if>
<if test="destinationInfo != null and destinationInfo != ''">and destination_info = #{destinationInfo}</if>
<if test="libraryFlag != null and libraryFlag != ''">and library_flag = #{libraryFlag}</if>
<if test="intendedUse != null and intendedUse != ''">and intended_use = #{intendedUse}</if>
<if test="remark != null and remark != ''">and remark like concat('%', #{remark}, '%')</if>
<if test="fields1 != null and fields1 != ''">and fields_1 = #{fields1}</if>
<if test="fields2 != null and fields2 != ''">and fields_2 = #{fields2}</if>
<if test="fields3 != null and fields3 != ''">and fields_3 = #{fields3}</if>
<if test="startDate != null and startDate != ''">and DATE_FORMAT(processing_time, '%Y/%m/%d') >=
#{startDate}
</if>
@ -245,35 +245,48 @@
<trim prefix="SET" suffixOverrides=",">
<if test="tId != null and tId != ''">t_id = #{tId},</if>
<if test="epc != null and epc != ''">epc = #{epc},</if>
<if test="password != null and password != '!clear!'">password = #{password},</if>
<if test="password != null and password == '!clear!'">password = NULL,</if>
<if test="orderCode != null and orderCode != '!clear!'">order_code = #{orderCode},</if>
<if test="orderCode != null and orderCode == '!clear!'">order_code = NULL,</if>
<if test="batchNumber != null and batchNumber != '!clear!'">batch_number = #{batchNumber},</if>
<if test="batchNumber != null and batchNumber == '!clear!'">batch_number = NULL,</if>
<if test="tagSequence != null and tagSequence != '!clear!'">tag_sequence = #{tagSequence},</if>
<if test="tagSequence != null and tagSequence == '!clear!'">tag_sequence = NULL,</if>
<if test="password != null and password != '' and password != '!clear!'">password = #{password},</if>
<if test="password == '!clear!'">password = NULL,</if>
<if test="orderCode != null and orderCode != '' and orderCode != '!clear!'">order_code = #{orderCode},</if>
<if test="orderCode == '!clear!'">order_code = NULL,</if>
<if test="batchNumber != null and batchNumber != '' and batchNumber != '!clear!'">batch_number =
#{batchNumber},
</if>
<if test="batchNumber == '!clear!'">batch_number = NULL,</if>
<if test="tagSequence != null and tagSequence != '' and tagSequence != '!clear!'">tag_sequence =
#{tagSequence},
</if>
<if test="tagSequence == '!clear!'">tag_sequence = NULL,</if>
<if test="totalQuantity != null">total_quantity = #{totalQuantity},</if>
<if test="tagBatch != null and tagBatch != '!clear!'">tag_batch = #{tagBatch},</if>
<if test="tagBatch != null and tagBatch == '!clear!'">tag_batch = NULL,</if>
<if test="modelCode != null">model_code = #{modelCode},</if>
<if test="tagBatch != null and tagBatch != '' and tagBatch != '!clear!'">tag_batch = #{tagBatch},</if>
<if test="tagBatch == '!clear!'">tag_batch = NULL,</if>
<if test="modelCode != null and modelCode != ''">model_code = #{modelCode},</if>
<if test="processingTime != null">processing_time = #{processingTime},</if>
<if test="operatorId != null and operatorId != '!clear!'">operator_id = #{operatorId},</if>
<if test="operatorId != null and operatorId == '!clear!'">operator_id = NULL,</if>
<if test="testResult != null and testResult != '!clear!'">test_result = #{testResult},</if>
<if test="testResult != null and testResult == '!clear!'">test_result = NULL,</if>
<if test="testValue != null and testValue != '!clear!'">test_value = #{testValue},</if>
<if test="testValue != null and testValue == '!clear!'">test_value = NULL,</if>
<if test="referenceValue != null and referenceValue != '!clear!'">reference_value = #{referenceValue},</if>
<if test="referenceValue != null and referenceValue == '!clear!'">reference_value = NULL,</if>
<if test="operatorId != null and modelCode != '' and operatorId != '!clear!'">operator_id = #{operatorId},
</if>
<if test="operatorId == '!clear!'">operator_id = NULL,</if>
<if test="testResult != null and testResult != '' and testResult != '!clear!'">test_result =
#{testResult},
</if>
<if test="testResult == '!clear!'">test_result = NULL,</if>
<if test="testValue != null and testValue != '' and testValue != '!clear!'">test_value = #{testValue},</if>
<if test="testValue == '!clear!'">test_value = NULL,</if>
<if test="referenceValue != null and referenceValue != '' and referenceValue != '!clear!'">reference_value =
#{referenceValue},
</if>
<if test="referenceValue == '!clear!'">reference_value = NULL,</if>
<if test="testingTime != null">testing_time = #{testingTime},</if>
<if test="fileName != null and fileName != '!clear!'">file_name = #{fileName},</if>
<if test="fileName != null and fileName == '!clear!'">file_name = NULL,</if>
<if test="destinationInfo != null and destinationInfo != '!clear!'">destination_info = #{destinationInfo},</if>
<if test="destinationInfo != null and destinationInfo == '!clear!'">destination_info = NULL,</if>
<if test="libraryFlag != null and libraryFlag != '!clear!'">library_flag = #{libraryFlag},</if>
<if test="libraryFlag != null and libraryFlag == '!clear!'">library_flag = NULL,</if>
<!-- <if test="intendedUse != null">intended_use = #{intendedUse},</if>-->
<if test="fileName != null and fileName != '' and fileName != '!clear!'">file_name = #{fileName},</if>
<if test="fileName == '!clear!'">file_name = NULL,</if>
<if test="destinationInfo != null and destinationInfo != '' and destinationInfo != '!clear!'">
destination_info = #{destinationInfo},
</if>
<if test="destinationInfo == '!clear!'">destination_info = NULL,</if>
<if test="libraryFlag != null and libraryFlag != '' and libraryFlag != '!clear!'">library_flag =
#{libraryFlag},
</if>
<if test="libraryFlag == '!clear!'">library_flag = NULL,</if>
<!-- <if test="intendedUse != null">intended_use = #{intendedUse},</if>-->
<if test="delFlag != null">del_flag = #{delFlag},</if>
<if test="fields1 != null">fields_1 = #{fields1},</if>
<if test="fields2 != null">fields_2 = #{fields2},</if>
@ -282,8 +295,8 @@
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="remark != null and remark != '!clear!'">remark = #{remark},</if>
<if test="remark != null and remark == '!clear!'">remark = NULL,</if>
<if test="remark != null and and remark != '' remark != '!clear!'">remark = #{remark},</if>
<if test="remark == '!clear!'">remark = NULL,</if>
</trim>
where t_id = #{tId}
</update>

Loading…
Cancel
Save