Jelajahi Sumber

将定时任务频率改为从数据库读取

wangyc 7 tahun lalu
induk
melakukan
f9e4e92e56

+ 58 - 7
src/main/java/com/uas/ps/message/SchedulingConfig.java

@@ -1,11 +1,24 @@
 package com.uas.ps.message;
 
+import com.uas.ps.message.dao.CronDao;
+import com.uas.ps.message.domain.Cron;
+import com.uas.ps.message.exception.ParameterMissingException;
 import com.uas.ps.message.service.MessageService;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.concurrent.ScheduledFuture;
 import javax.annotation.Resource;
 import org.apache.log4j.Logger;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.Trigger;
+import org.springframework.scheduling.TriggerContext;
 import org.springframework.scheduling.annotation.EnableScheduling;
-import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.scheduling.annotation.SchedulingConfigurer;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
+import org.springframework.scheduling.config.ScheduledTaskRegistrar;
+import org.springframework.scheduling.support.CronTrigger;
+import org.springframework.util.StringUtils;
 
 /**
  * Created by wangyc on 2018/1/31.
@@ -14,18 +27,56 @@ import org.springframework.scheduling.annotation.Scheduled;
  */
 @Configuration
 @EnableScheduling
-public class SchedulingConfig {
+public class SchedulingConfig implements SchedulingConfigurer {
+
     private final Logger logger = Logger.getLogger(Logger.class);
 
+    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss");
+
+    private String cornStr = "";
+
     @Resource
     private MessageService messageService;
 
+    @Resource
+    private CronDao cronDao;
+
+    @Override
+    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
+        scheduledTaskRegistrar.addTriggerTask(new Runnable() {
+            @Override
+            public void run() {
+            messageService.sendMessage("{\"consumerApp\":\"B2B\"}");
+            // 定时任务的业务逻辑
+            logger.info("动态修改定时任务cron参数,当前时间:" + DATE_FORMAT.format(new Date()));
+            }
+        }, new Trigger() {
+            @Override
+            public Date nextExecutionTime(TriggerContext triggerContext) {
+                // 定时任务触发,可修改定时任务的执行周期
+                final Cron cron = cronDao.findOne(1L);
+                if (cron != null && !StringUtils.isEmpty(cron.getCron())) {
+                    cornStr = cron.getCron();
+                }
+
+                CronTrigger trigger = new CronTrigger(cron.getCron());
+                Date nextExecDate = trigger.nextExecutionTime(triggerContext);
+                return nextExecDate;
+            }
+        });
+    }
+
     /**
-     * 定时执行发送任务
+     * 设置定时频率
+     * @param cron 定时频率
+     * @return
      */
-    @Scheduled(cron = "0 15 14 * * ?") // 每10分钟执行一次
-    public void getToken() {
-        logger.info("getToken定时任务启动");
-        messageService.sendMessage("{\"consumerApp\":\"B2B\"}");
+    public String setCron(String cron) {
+        if (!StringUtils.isEmpty(cron)) {
+            this.cornStr = cron;
+        } else {
+            throw new ParameterMissingException("定时频率为空");
+        }
+        return this.cornStr;
     }
 }

+ 32 - 0
src/main/java/com/uas/ps/message/api/ConfigureTaskController.java

@@ -0,0 +1,32 @@
+package com.uas.ps.message.api;
+
+import com.uas.ps.message.SchedulingConfig;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * 定时任务接口
+ * Created by wangyc on 2018/2/1.
+ *
+ * @version 2018/2/1 18:01 wangyc
+ */
+@RestController
+@RequestMapping("/task")
+public class ConfigureTaskController {
+
+    @Autowired
+    private SchedulingConfig schedulingConfig;
+
+    /**
+     * 修改推送短信品牌
+     * @param cron 定时频率
+     * @return
+     */
+    @RequestMapping(value = "/pushsm/cron/update", method = RequestMethod.POST, produces = "application/json")
+    public String updateCron(@RequestBody String cron) {
+       return schedulingConfig.setCron(cron);
+    }
+}

+ 14 - 0
src/main/java/com/uas/ps/message/dao/CronDao.java

@@ -0,0 +1,14 @@
+package com.uas.ps.message.dao;
+
+import com.uas.ps.message.domain.Cron;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+
+/**
+ * Created by wangyc on 2018/2/1.
+ *
+ * @version 2018/2/1 14:50 wangyc
+ */
+public interface CronDao extends JpaSpecificationExecutor<Cron>, JpaRepository<Cron, Long> {
+
+}

+ 60 - 0
src/main/java/com/uas/ps/message/domain/Cron.java

@@ -0,0 +1,60 @@
+package com.uas.ps.message.domain;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+/**
+ * 定时推送消息配置
+ * Created by wangyc on 2018/2/1.
+ *
+ * @version 2018/2/1 14:46 wangyc
+ */
+@Entity
+@Table(name = "crons")
+public class Cron {
+
+    @Id
+    @Column(name = "cr_id")
+    @GeneratedValue(strategy = GenerationType.AUTO)
+    private Long id;
+
+    /**
+     * 任务定时
+     */
+    @Column(name = "cr_cron")
+    private String cron;
+
+    /**
+     * 备注
+     */
+    @Column(name = "cr_remark")
+    private String remark;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getCron() {
+        return cron;
+    }
+
+    public void setCron(String cron) {
+        this.cron = cron;
+    }
+
+    public String getRemark() {
+        return remark;
+    }
+
+    public void setRemark(String remark) {
+        this.remark = remark;
+    }
+}

+ 2 - 2
src/main/java/com/uas/ps/message/service/impl/MessageServiceImpl.java

@@ -267,7 +267,7 @@ public class MessageServiceImpl implements MessageService {
         if (!CollectionUtils.isEmpty(messages)) {
             for (Message message : messages) {
                 try {
-                    User receiver = AccountUtils.getImUserByUserUU(message.getReceiverUu());
+                    User receiver = AccountUtils.getImUserByUserUU(message.getReceiverUu(), message.getReceiverEnuu());
                     logger.info("接收人:" + receiver.getName());
                     // 发送邮件
 //                    if (message.getSmsType().contains(SMSType.MAIL)) {
@@ -333,7 +333,7 @@ public class MessageServiceImpl implements MessageService {
                 List<Object> obj = new ArrayList<Object>();
                 obj.add(receiver.getName());
 
-                User sender = AccountUtils.getImUserByUserUU(message.getSenderUu());
+                User sender = AccountUtils.getImUserByUserUU(message.getSenderUu(), message.getSenderEnuu());
                 logger.info("发送人:" + sender.getName());
                 if (sender != null) {
                     obj.add(sender.getName());