springboot动态修改定时任务cro表达式

1.编写定时任务处理器

package com.tv.foc.task;

import com.tv.foc.handler.FlightDataRefreshHandler;
import com.tv.foc.service.AirlineService;
import com.tv.foc.util.DateUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author wjw
 * @Description:定时任务同步FOC数据航班信息
 * 航班时刻,航班延误,航班取消原因,机组航班信息关联
 * @date 2018/6/25
 * cron 表达式参考地址:http://blog.csdn.net/u011116672/article/details/52517247
 */
@Component
public class FOCAirLineScheduler implements SchedulingConfigurer {
    private Log log = LogFactory.getLog(FOCAirLineScheduler.class);
    //默认每5分钟执行基本信息更新
    private static final String AIRLINE_CRON = "0 0/5 * * * ?";
    //用于接收用户自定义编写cro表达式
    private String airLineDynamicCron=AIRLINE_CRON;
    @Resource
    private FlightDataRefreshHandler flightDataRefreshHandler;
    int i=0;

    public String getAirLineDynamicCron() {
        return airLineDynamicCron;
    }

    public void setAirLineDynamicCron(String airLineDynamicCron) {
        this.airLineDynamicCron = airLineDynamicCron;
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(new Runnable() {
            @Override
            public void run() {
               if(!airLineDynamicCron.equals(AIRLINE_CRON)){
                   i++;
                   if(i<=1){
                       log.info(":用户修改了航班信息更新cro表达式:"+airLineDynamicCron);
                   }
               }
               //执行更新方法
                Date beforeDate = DateUtil.getCurrentBeforeDate(-1);
                String dateStr = DateUtil.parseDateToStr(beforeDate, DateUtil.FORMAT_YYYY_MM_DD);
                flightDataRefreshHandler.updateFlightData(dateStr);
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                // 定时任务触发,可修改定时任务的执行周期
                CronTrigger trigger = new CronTrigger(airLineDynamicCron);
                return trigger.nextExecutionTime(triggerContext);
            }
        });
    }
}

2.controller层实现修改

package com.tv.foc.controller;

import com.tv.foc.config.CroAuthConfig;
import com.tv.foc.task.FOCAirLineScheduler;
import com.tv.foc.task.FOCBaseScheduler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wjw
 * @Description: 动态修改cro表达式
 * @date 2018/6/25
 */
@Controller
public class CroController {
    private Log log = LogFactory.getLog(CroController.class);
    @Resource
    private FOCBaseScheduler focScheduler;
    @Resource
    private FOCAirLineScheduler focAirLineScheduler;
    @Resource
    private CroAuthConfig croAuthConfig;
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Map map) {
        map.put("baseCronExpression",  focScheduler.getDynamicCron());
        map.put("currentCronExpression",  focAirLineScheduler.getAirLineDynamicCron());
        return "index";
    }

    @RequestMapping(value = "/changeCro",method = RequestMethod.POST)
    @ResponseBody
    public Object changeCro(String account,String pass,String cronExpression,String croType){
        Map data = new HashMap();
       if(!croAuthConfig.getAuthAccount().equals(account)||!croAuthConfig.getAuthPass().equals(pass)){
          data.put("code",0);
          data.put("message","认证账号信息错误");
       }else{
           try {
               //CronExpression exp = new CronExpression(cronExpression);
               //Date nextValidTimeAfter=exp.getNextValidTimeAfter(new Date());
               if(croType.equals("0")){
                   focScheduler.setDynamicCron(cronExpression);
               }
               if(croType.equals("1")){
                   focAirLineScheduler.setAirLineDynamicCron(cronExpression);
               }
               data.put("code",1);
               data.put("message","更新成功");
           } catch (Exception e) {
               log.error("Cro表达式输入错误:"+cronExpression);
               data.put("code",0);
               data.put("message","Cro表达式输入错误!");
           }
       }
        return data;
    }
}

 

你可能感兴趣的:(springboot动态修改定时任务cro表达式)