|
|
@@ -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;
|
|
|
}
|
|
|
}
|