Parcourir la source

推送失败启动定时任务

huihui il y a 1 an
Parent
commit
6f6581e493

+ 17 - 0
src/main/java/com/ydtech/config/scheduledConfig/ScheduledConfig.java

@@ -0,0 +1,17 @@
+package com.ydtech.config.scheduledConfig;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
+
+@Configuration
+public class ScheduledConfig {
+    @Bean
+    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
+        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
+        threadPoolTaskScheduler.setPoolSize(5);
+        threadPoolTaskScheduler.setRemoveOnCancelPolicy(true);
+        return threadPoolTaskScheduler;
+    }
+}
+

+ 48 - 0
src/main/java/com/ydtech/utils/scheduledFutureUtil/ScheduledFutureHolder.java

@@ -0,0 +1,48 @@
+package com.ydtech.utils.scheduledFutureUtil;
+
+import java.util.concurrent.ScheduledFuture;
+
+/**
+ * 任务执行的包装类
+ */
+public class ScheduledFutureHolder {
+    private ScheduledFuture<?> scheduledFuture;
+
+    private Class<? extends Runnable> runnableClass;
+
+    private String corn;
+
+    public ScheduledFuture<?> getScheduledFuture() {
+        return scheduledFuture;
+    }
+
+    public void setScheduledFuture(ScheduledFuture<?> scheduledFuture) {
+        this.scheduledFuture = scheduledFuture;
+    }
+
+    public Class<? extends Runnable> getRunnableClass() {
+        return runnableClass;
+    }
+
+    public void setRunnableClass(Class<? extends Runnable> runnableClass) {
+        this.runnableClass = runnableClass;
+    }
+
+    public String getCorn() {
+        return corn;
+    }
+
+    public void setCorn(String corn) {
+        this.corn = corn;
+    }
+
+    @Override
+    public String toString() {
+        return "ScheduledFutureHolder{" +
+                "scheduledFuture=" + scheduledFuture +
+                ", runnableClass=" + runnableClass +
+                ", corn='" + corn + '\'' +
+                '}';
+    }
+}
+

+ 40 - 0
src/main/java/com/ydtech/utils/scheduledFutureUtil/ScheduledFutureUtil.java

@@ -0,0 +1,40 @@
+package com.ydtech.utils.scheduledFutureUtil;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
+import org.springframework.scheduling.support.CronTrigger;
+import org.springframework.stereotype.Component;
+
+import java.util.HashMap;
+import java.util.concurrent.ScheduledFuture;
+
+@Slf4j
+@Component
+public class ScheduledFutureUtil {
+    //存储任务执行的包装类
+    public static HashMap<String, ScheduledFutureHolder> scheduleMap = new HashMap<>();
+    @Autowired
+    private ThreadPoolTaskScheduler threadPoolTaskScheduler;
+
+    public  void startJob(Runnable runnable, String cron) {
+        //将任务交给任务调度器执行
+        ScheduledFuture<?> schedule = threadPoolTaskScheduler.schedule(runnable, new CronTrigger(cron));
+        //将任务包装成ScheduledFutureHolder
+        ScheduledFutureHolder scheduledFutureHolder = new ScheduledFutureHolder();
+        scheduledFutureHolder.setScheduledFuture(schedule);
+        scheduledFutureHolder.setRunnableClass(runnable.getClass());
+        scheduledFutureHolder.setCorn(cron);
+        scheduleMap.put(scheduledFutureHolder.getRunnableClass().getName(), scheduledFutureHolder);
+    }
+
+    public void endJob(String className){
+        if(ScheduledFutureUtil.scheduleMap.containsKey(className)){//如果包含这个任务
+            ScheduledFuture<?> scheduledFuture = ScheduledFutureUtil.scheduleMap.get(className).getScheduledFuture();
+            if(scheduledFuture!=null){
+                scheduledFuture.cancel(true);
+                log.info("{}任务已停止",className);
+            }
+        }
+    }
+}

+ 22 - 4
src/main/java/com/ydtech/utils/sendCollectUtils/SendCollectUtils.java

@@ -6,6 +6,7 @@ import com.ydtech.modules.order.entity.vo.sendCollect.SendCollect;
 import com.ydtech.modules.sendCollect.dao.SendCollectMapper;
 import com.ydtech.modules.sendCollect.entity.SendCollectEntity;
 import com.ydtech.utils.StringUtils;
+import com.ydtech.utils.scheduledFutureUtil.ScheduledFutureUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.http.HttpEntity;
 import org.apache.http.client.methods.CloseableHttpResponse;
@@ -17,7 +18,6 @@ import org.apache.http.util.EntityUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.Async;
-import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
 import java.io.IOException;
@@ -29,14 +29,20 @@ import java.util.UUID;
 
 @Slf4j
 @Service
-public class SendCollectUtils {
+public class SendCollectUtils implements Runnable {
 
     @Autowired
     private SendCollectMapper sendCollectMapper;
 
+    @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_2 = "2";
@@ -93,6 +99,8 @@ public class SendCollectUtils {
             }
 
         } catch (Exception e) {
+            SendCollectUtils sendCollectUtils = new SendCollectUtils();
+            scheduledFutureUtil.startJob(sendCollectUtils, CORN);
             e.printStackTrace();
         } finally {
             try {
@@ -109,7 +117,7 @@ public class SendCollectUtils {
         }
     }
 
-    public void sendCollect (SysAmountAuditingVo sysAmountAuditingVo) {
+    public void sendCollect(SysAmountAuditingVo sysAmountAuditingVo) {
 
         StringBuilder sendUuid = new StringBuilder();
         sendUuid.append(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()))
@@ -142,7 +150,6 @@ public class SendCollectUtils {
         send(sendCollectEntity,sendCollect);
     }
 
-//    @Scheduled(cron = "0 */1 * * * ?")
     public void sendCollectRetry() {
         log.info("推送提现历史补偿启动,启动时间:{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
         List<SendCollectEntity> sendCollectFailInfoList = new ArrayList<SendCollectEntity>();
@@ -175,4 +182,15 @@ public class SendCollectUtils {
             }
         }
     }
+
+    @Override
+    public void run() {
+        num++;
+        log.info("第{}次重新推送提现记录消息", num);
+        this.sendCollectRetry();
+        if (num == 3) {
+            //停止定时任务
+            scheduledFutureUtil.endJob("com.ydtech.utils.scheduledFutureUtil");
+        }
+    }
 }

+ 0 - 57
src/main/java/com/ydtech/utils/sendProjectUtils/SendProjectRetryUtils.java

@@ -1,57 +0,0 @@
-package com.ydtech.utils.sendProjectUtils;
-
-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 lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.scheduling.annotation.Scheduled;
-import org.springframework.stereotype.Component;
-
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-
-@Slf4j
-@Component
-public class SendProjectRetryUtils {
-
-    @Autowired
-    private SendProjectMapper sendProjectMapper;
-
-    @Autowired
-    private SendProjrctUtils sendProjrctUtils;
-
-//    @Scheduled(cron = "0 */1 * * * ?")
-    private 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());
-                    sendProjrctUtils.sendProjectRetry(sendProjectInfo);
-                }
-            }
-        }
-    }
-}

+ 99 - 63
src/main/java/com/ydtech/utils/sendProjectUtils/SendProjrctUtils.java

@@ -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");
+        }
+    }
 }