|
|
@@ -4,6 +4,9 @@ import com.alibaba.fastjson.JSON;
|
|
|
import com.ydtech.modules.order.entity.vo.sendProject.SendProjectInfo;
|
|
|
import com.ydtech.modules.sendProject.dao.SendProjectMapper;
|
|
|
import com.ydtech.modules.sendProject.entity.SendProjectEntity;
|
|
|
+import com.ydtech.utils.StringUtils;
|
|
|
+import com.ydtech.utils.scheduledFutureUtil.ScheduledFutureUtil;
|
|
|
+import com.ydtech.utils.sendCollectUtils.SendCollectUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.ParseException;
|
|
|
@@ -19,27 +22,31 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
-public class SendProjrctUtils {
|
|
|
+public class SendProjrctUtils implements Runnable {
|
|
|
|
|
|
@Autowired
|
|
|
private SendProjectMapper sendProjectMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ScheduledFutureUtil scheduledFutureUtil;
|
|
|
+
|
|
|
@Value("${sendMessage-zg-yggl-IP}")
|
|
|
private String sendMessageIP;
|
|
|
|
|
|
+ private Integer num = 0;
|
|
|
+
|
|
|
+ private static final String CORN = "0 */1 * * * ?";
|
|
|
private static final String STRING_0 = "0";
|
|
|
private static final String STRING_1 = "1";
|
|
|
private static final String STRING_9 = "9";
|
|
|
private static final String RESPONSE_SUCCESS = "200";
|
|
|
|
|
|
- public Map<String, String> sendProject(SendProjectInfo sendProjectInfo)
|
|
|
- throws ClientProtocolException,ParseException, IOException {
|
|
|
+ public Map<String, String> sendProject(SendProjectInfo sendProjectInfo) {
|
|
|
|
|
|
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
|
|
|
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
@@ -61,31 +68,43 @@ public class SendProjrctUtils {
|
|
|
CloseableHttpResponse response = null;
|
|
|
String responseCode = new String();
|
|
|
|
|
|
- // 由客户端执行(发送)Post请求
|
|
|
- response = httpClient.execute(httpPost);
|
|
|
- // 从响应模型中获取响应实体
|
|
|
- HttpEntity responseEntity = response.getEntity();
|
|
|
+ try {
|
|
|
|
|
|
- //从响应实体中截取状态码
|
|
|
- String string = EntityUtils.toString(responseEntity);
|
|
|
- sendProjectInfo.setHttpResponse(string);
|
|
|
- String responseString = string.substring(string.lastIndexOf(":") + 1);
|
|
|
- responseCode = responseString.substring(0, responseString.length() - 1);
|
|
|
+ // 由客户端执行(发送)Post请求
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ // 从响应模型中获取响应实体
|
|
|
+ HttpEntity responseEntity = response.getEntity();
|
|
|
|
|
|
+ //从响应实体中截取状态码
|
|
|
+ String string = EntityUtils.toString(responseEntity);
|
|
|
+ sendProjectInfo.setHttpResponse(string);
|
|
|
+ String responseString = string.substring(string.lastIndexOf(":") + 1);
|
|
|
+ responseCode = responseString.substring(0, responseString.length() - 1);
|
|
|
|
|
|
- log.info("响应状态为:{}", response.getStatusLine());
|
|
|
- try {
|
|
|
- // 释放资源
|
|
|
- if (httpClient != null) {
|
|
|
- httpClient.close();
|
|
|
- }
|
|
|
- if (response != null) {
|
|
|
- response.close();
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
+
|
|
|
+ log.info("响应状态为:{}", response.getStatusLine());
|
|
|
+ } catch (Exception e) {
|
|
|
+ sendProjectInfo.setHttpResponse(e.getMessage());
|
|
|
+ sendProjectInfo.setSendState(STRING_9);
|
|
|
+ updateSendProjectInfo(sendProjectInfo);
|
|
|
+ SendProjrctUtils sendProjrctUtils = new SendProjrctUtils();
|
|
|
+ scheduledFutureUtil.startJob(sendProjrctUtils, CORN);
|
|
|
e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ // 释放资源
|
|
|
+ if (httpClient != null) {
|
|
|
+ httpClient.close();
|
|
|
+ }
|
|
|
+ if (response != null) {
|
|
|
+ response.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
final String code = responseCode;
|
|
|
return new HashMap<String, String>() {{
|
|
|
put("responseCode", code);
|
|
|
@@ -95,53 +114,30 @@ public class SendProjrctUtils {
|
|
|
public void firstSendProject(SendProjectInfo sendProjectInfo) {
|
|
|
log.info("首次推送服务接口入参:{}", sendProjectInfo);
|
|
|
Map<String, String> responseMap = new HashMap<>();
|
|
|
- try {
|
|
|
- responseMap = sendProject(sendProjectInfo);
|
|
|
- if (RESPONSE_SUCCESS.equals(responseMap.get("responseCode"))) {
|
|
|
- sendProjectInfo.setSendState(STRING_1);
|
|
|
- updateSendProjectInfo(sendProjectInfo);
|
|
|
- } else {
|
|
|
- sendProjectInfo.setSendState(STRING_0);
|
|
|
- updateSendProjectInfo(sendProjectInfo);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- sendProjectInfo.setHttpResponse(e.getMessage());
|
|
|
- sendProjectInfo.setSendState(STRING_9);
|
|
|
+ responseMap = sendProject(sendProjectInfo);
|
|
|
+ if (RESPONSE_SUCCESS.equals(responseMap.get("responseCode"))) {
|
|
|
+ sendProjectInfo.setSendState(STRING_1);
|
|
|
+ updateSendProjectInfo(sendProjectInfo);
|
|
|
+ } else {
|
|
|
+ sendProjectInfo.setSendState(STRING_0);
|
|
|
updateSendProjectInfo(sendProjectInfo);
|
|
|
- e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void sendProjectRetry(SendProjectInfo sendProjectInfo) {
|
|
|
Map<String, String> responseMap = new HashMap<>();
|
|
|
- try {
|
|
|
- responseMap = sendProject(sendProjectInfo);
|
|
|
- if (RESPONSE_SUCCESS.equals(responseMap.get("responseCode"))) {
|
|
|
- sendProjectInfo.setSendState(STRING_1);
|
|
|
- updateSendProjectInfo(sendProjectInfo);
|
|
|
- } else {
|
|
|
- Integer failNum = sendProjectInfo.getFailNum();
|
|
|
- failNum = ++failNum;
|
|
|
- sendProjectInfo.setFailNum(failNum);
|
|
|
- sendProjectInfo.setSendState(STRING_0);
|
|
|
- updateSendProjectInfo(sendProjectInfo);
|
|
|
- }
|
|
|
- } catch (ClientProtocolException e) {
|
|
|
- sendProjectInfo.setHttpResponse(e.getMessage());
|
|
|
- sendProjectInfo.setSendState(STRING_9);
|
|
|
- updateSendProjectInfo(sendProjectInfo);
|
|
|
- e.printStackTrace();
|
|
|
- } catch (ParseException e) {
|
|
|
- sendProjectInfo.setHttpResponse(e.getMessage());
|
|
|
- sendProjectInfo.setSendState(STRING_9);
|
|
|
+ responseMap = sendProject(sendProjectInfo);
|
|
|
+ if (RESPONSE_SUCCESS.equals(responseMap.get("responseCode"))) {
|
|
|
+ sendProjectInfo.setSendState(STRING_1);
|
|
|
updateSendProjectInfo(sendProjectInfo);
|
|
|
- e.printStackTrace();
|
|
|
- } catch (IOException e) {
|
|
|
- sendProjectInfo.setHttpResponse(e.getMessage());
|
|
|
- sendProjectInfo.setSendState(STRING_9);
|
|
|
+ } else {
|
|
|
+ Integer failNum = sendProjectInfo.getFailNum();
|
|
|
+ failNum = ++failNum;
|
|
|
+ sendProjectInfo.setFailNum(failNum);
|
|
|
+ sendProjectInfo.setSendState(STRING_0);
|
|
|
updateSendProjectInfo(sendProjectInfo);
|
|
|
- e.printStackTrace();
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
private void updateSendProjectInfo(SendProjectInfo sendProjectInfo) {
|
|
|
@@ -164,4 +160,44 @@ public class SendProjrctUtils {
|
|
|
log.error("修改推送服务数据库报错:{}", e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void sendProjectRetry() {
|
|
|
+ log.info("推送服务补偿启动,启动时间:{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
+ List<SendProjectEntity> sendFailInfo = new ArrayList<SendProjectEntity>();
|
|
|
+ try {
|
|
|
+ sendFailInfo = sendProjectMapper.getSendFailInfo();
|
|
|
+ log.info("查询send_project_history数据库出参:{}", JSON.toJSON(sendFailInfo));
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("查询send_project_history数据库报错:{}", e.getMessage());
|
|
|
+ }
|
|
|
+ if (sendFailInfo != null && !sendFailInfo.isEmpty()) {
|
|
|
+ for (SendProjectEntity sendProjectEntity : sendFailInfo) {
|
|
|
+ if (sendProjectEntity == null && StringUtils.isNullOrEmpty(sendProjectEntity)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (sendProjectEntity.getFailNum() != null && sendProjectEntity.getFailNum() < 3) {
|
|
|
+ SendProjectInfo sendProjectInfo = new SendProjectInfo();
|
|
|
+ sendProjectInfo.setGiveBudget(sendProjectEntity.getTotalAmount());
|
|
|
+ sendProjectInfo.setUserName(sendProjectEntity.getUserName());
|
|
|
+ sendProjectInfo.setUserPhone(sendProjectEntity.getUserPhone());
|
|
|
+ sendProjectInfo.setUserCarNum(sendProjectEntity.getUserCarNum());
|
|
|
+ sendProjectInfo.setUuid(sendProjectEntity.getSendUuid());
|
|
|
+ sendProjectInfo.setFailNum(sendProjectEntity.getFailNum());
|
|
|
+ this.sendProjectRetry(sendProjectInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ num++;
|
|
|
+ log.info("第{}次重新推送服务消息", num);
|
|
|
+ this.sendProjectRetry();
|
|
|
+ if (num == 3) {
|
|
|
+ //停止定时任务
|
|
|
+ scheduledFutureUtil.endJob("com.ydtech.utils.sendProjectUtils");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|