浏览代码

优化http请求,修改定时任务

xusonglin 3 年之前
父节点
当前提交
c1a52892d4

+ 3 - 3
pom.xml

@@ -65,9 +65,9 @@
             <version>4.5.6</version>
         </dependency>
         <dependency>
-            <groupId>com.xuxueli</groupId>
-            <artifactId>xxl-job-core</artifactId>
-            <version>2.1.2</version>
+            <groupId>org.quartz-scheduler</groupId>
+            <artifactId>quartz</artifactId>
+            <version>2.3.0</version>
         </dependency>
     </dependencies>
 

+ 0 - 74
src/main/java/com/jkcredit/illegal/info/config/XxlJobConfig.java

@@ -1,74 +0,0 @@
-package com.jkcredit.illegal.info.config;
-
-import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-/**
- * xxl-job config
- *
- * @author xuxueli 2017-04-28
- */
-@Configuration
-public class XxlJobConfig {
-    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
-
-    @Value("${xxl.job.admin.addresses}")
-    private String adminAddresses;
-
-    @Value("${xxl.job.executor.appname}")
-    private String appName;
-
-    @Value("${xxl.job.executor.ip}")
-    private String ip;
-
-    @Value("${xxl.job.executor.port}")
-    private int port;
-
-    @Value("${xxl.job.accessToken}")
-    private String accessToken;
-
-    @Value("${xxl.job.executor.logpath}")
-    private String logPath;
-
-    @Value("${xxl.job.executor.logretentiondays}")
-    private int logRetentionDays;
-
-
-    @Bean
-    public XxlJobSpringExecutor xxlJobExecutor() {
-        logger.info(">>>>>>>>>>> xxl-job config init.");
-        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
-        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
-        xxlJobSpringExecutor.setAppName(appName);
-        xxlJobSpringExecutor.setIp(ip);
-        xxlJobSpringExecutor.setPort(port);
-        xxlJobSpringExecutor.setAccessToken(accessToken);
-        xxlJobSpringExecutor.setLogPath(logPath);
-        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
-
-        return xxlJobSpringExecutor;
-    }
-
-    /**
-     * 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
-     *
-     *      1、引入依赖:
-     *          <dependency>
-     *             <groupId>org.springframework.cloud</groupId>
-     *             <artifactId>spring-cloud-commons</artifactId>
-     *             <version>${version}</version>
-     *         </dependency>
-     *
-     *      2、配置文件,或者容器启动变量
-     *          spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
-     *
-     *      3、获取IP
-     *          String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
-     */
-
-
-}

+ 8 - 19
src/main/java/com/jkcredit/illegal/info/task/ExceptionListenerTask.java

@@ -3,15 +3,12 @@ package com.jkcredit.illegal.info.task;
 import com.jkcredit.illegal.info.constant.CommonConstant;
 import com.jkcredit.illegal.info.model.SendDingMsgResponseObject;
 import com.jkcredit.illegal.info.util.DingRobotUtil;
-import com.xxl.job.core.biz.model.ReturnT;
-import com.xxl.job.core.handler.annotation.XxlJob;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
-import java.util.Objects;
-
 /**
  * @author xusonglin
  * @version V1.0
@@ -22,8 +19,9 @@ public class ExceptionListenerTask {
     @Autowired
     StringRedisTemplate stringRedisTemplate;
 
-    @XxlJob("personIllegalInfoTask")
-    public ReturnT<String> personIllegalInfoTask(String param) {
+
+    @Scheduled(cron = "0 0/5 * * * ?", zone = "Asia/Shanghai")
+    public void personIllegalInfoTask() {
         int timeOutCount = 0;
         int queryFailedCount = 0;
         String timeOut = stringRedisTemplate.boundValueOps(CommonConstant.PERSON_ILLEGAL_INFO_TIME_OUT).get();
@@ -44,7 +42,7 @@ public class ExceptionListenerTask {
             }
         }
 
-        if (queryFailedCount >10) {
+        if (queryFailedCount > 10) {
             errorMessage += "数据源返回结果异常";
             SendDingMsgResponseObject sendDingMsgResponseObject = DingRobotUtil.sendDingRobotMsg(errorMessage);
             if (sendDingMsgResponseObject.getCode() != 1) {
@@ -53,11 +51,10 @@ public class ExceptionListenerTask {
         }
         stringRedisTemplate.delete(CommonConstant.PERSON_ILLEGAL_INFO_TIME_OUT);
         stringRedisTemplate.delete(CommonConstant.PERSON_ILLEGAL_INFO_QUERY_FAILED);
-        return ReturnT.SUCCESS;
     }
 
-    @XxlJob("vehicleIllegalInfoTask")
-    public ReturnT<String> vehicleIllegalInfoTask(String param) {
+    @Scheduled(cron = "0 0/5 * * * ?", zone = "Asia/Shanghai")
+    public void vehicleIllegalInfoTask() {
         int timeOutCount = 0;
         int queryFailedCount = 0;
         String timeOut = stringRedisTemplate.boundValueOps(CommonConstant.VEHICLE_ILLEGAL_INFO_TIME_OUT).get();
@@ -78,7 +75,7 @@ public class ExceptionListenerTask {
             }
         }
 
-        if (queryFailedCount >10) {
+        if (queryFailedCount > 10) {
             errorMessage += "数据源返回结果异常";
             SendDingMsgResponseObject sendDingMsgResponseObject = DingRobotUtil.sendDingRobotMsg(errorMessage);
             if (sendDingMsgResponseObject.getCode() != 1) {
@@ -87,13 +84,5 @@ public class ExceptionListenerTask {
         }
         stringRedisTemplate.delete(CommonConstant.VEHICLE_ILLEGAL_INFO_TIME_OUT);
         stringRedisTemplate.delete(CommonConstant.VEHICLE_ILLEGAL_INFO_QUERY_FAILED);
-        return ReturnT.SUCCESS;
-    }
-
-    @XxlJob("dingdingTestTask")
-    public ReturnT<String> dingdingTestTask(String param) {
-        String message = "社会风险预警-测试";
-        SendDingMsgResponseObject sendDingMsgResponseObject = DingRobotUtil.sendDingRobotMsg(message);
-        return ReturnT.SUCCESS;
     }
 }

+ 2 - 2
src/main/java/com/jkcredit/illegal/info/util/OkHttpUtil.java

@@ -25,8 +25,9 @@ import java.util.concurrent.TimeUnit;
  **/
 @Slf4j
 public class OkHttpUtil {
+    private static OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
+
     public static String doPost(String url, String body, int outTime) {
-        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
         MediaType mediaType = MediaType.parse(CommonConstant.MEDIA_TYPE);
         RequestBody requestBody = RequestBody.create(mediaType, body);
 
@@ -59,7 +60,6 @@ public class OkHttpUtil {
         MediaType mediaType = MediaType.parse(CommonConstant.APPLICATION_JSON_UTF8_VALUE);
         RequestBody requestBody = RequestBody.create(mediaType, jsonString);
         Request okRequest = new Request.Builder().post(requestBody).url(url).build();
-        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
         OkHttpClient client = okHttpClient.newBuilder()
                 .connectTimeout(CommonConstant.DING_TIME_OUT, TimeUnit.MILLISECONDS)
                 .readTimeout(CommonConstant.DING_TIME_OUT, TimeUnit.MILLISECONDS)

+ 2 - 13
src/main/resources/application.yml

@@ -8,22 +8,11 @@ spring:
 server:
   port: 8005
 illegalInfo:
-  url: https://192.168.1.27:8004/ryhc
-  vehicleUrl: https://192.168.1.27:8004/clhc
+  url: https://47.95.12.213:8004/ryhc
+  vehicleUrl: https://47.95.12.213:8004/clhc
   certificateUrl: /home/tpuser/apps/illegal-info-v2/client.pfx
 #  certificateUrl: /Users/jkxy/Desktop/client.pfx
   certificatePassword: client
   decodeKey: 374c72b109292ad4
 dingding:
   url: https://oapi.dingtalk.com/robot/send?access_token=64e1e991ba9d148cd837c9d49f9ce82bfcfb7f8ad371c08ec392c0fca5806738
-xxl:
-  job:
-    admin:
-      addresses: http://192.168.1.184:18083/xxl-job-admin
-    executor:
-      appname: illegal-info-executor
-      ip:
-      port: -1
-      logpath: /home/tpuser/apps/illegal-info-v2/logs/xxljob
-      logretentiondays: 30
-    accessToken: