Interrupt spring scheduler task before next invocation

ε祈祈猫儿з 提交于 2019-11-30 15:43:15

So using SchedulingConfigurer->configureTasks you can't get access to the ScheduledFuture(s) in the Spring version I am using (4.2.7.RELEASE). From several posts I have read it has been mentioned as possible functionality for the future. I got around this by doing the following:

package com.bts.poc;

import com.bts.poc.service.DynamicCron;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.ScheduledFuture;

@SpringBootApplication(exclude = MessageSourceAutoConfiguration.class)
@EnableScheduling
@RestController
public class Application extends SpringBootServletInitializer {

    @Autowired
    private DynamicCron dynamicCron;
    @Autowired
    private PropertyManager propertyManager;
    private static List<ScheduledFuture> scheduledFutures = new ArrayList<>();
    private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
    private static TaskScheduler scheduler;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    private String cronConfig() {
        return propertyManager.getProperty("COMPANY", "JOB_NAME", "CRON_EXPRESSION");
    }

    @RequestMapping(value = {"scheduling/start"}, method = RequestMethod.GET)
    public @ResponseBody String startScheduling() {
        scheduleAll();

        LOGGER.info("Scheduling of jobs has been started.");
        return "Scheduling of jobs has been started.";
    }

    @RequestMapping(value = {"scheduling/cancel"}, method = RequestMethod.GET)
    public @ResponseBody String cancelScheduling() {
        cancelAll();

        LOGGER.info("Cancelling all scheduled jobs.");
        return "Cancelling all scheduled jobs.";
    }

    private void scheduleAll() {
        LOGGER.info("Scheduling all applications to run.");
        cancelAll();

        //eventually go through the database and load all jobs to be scheduled here.
        schedule(cronConfig());
    }

    /**
     * Cancel all the scheduled reports
     */
    private void cancelAll() {
        for (ScheduledFuture scheduledFuture : scheduledFutures) {
            scheduledFuture.cancel(true);
        }
        scheduledFutures.clear();
    }

    /**
     * Schedule the scheduled report with the given cron schedule information
     */
    private void schedule(String cronSchedule) {
        TimeZone tz = TimeZone.getDefault();
        LOGGER.info("Setting up application {} to execute with cron string: '{}'.", cronSchedule);
        CronTrigger trigger = new CronTrigger(cronSchedule, tz);

        scheduler = scheduler();
        if (scheduler == null) {
            LOGGER.error("Unable to schedule job as scheduler was not found");
            return;
        }

        ScheduledFuture<?> future = scheduler.schedule(new DynamicCron(), trigger);
        scheduledFutures.add(future);
    }

    @Bean
    public TaskScheduler scheduler() {
        if (scheduler == null) {
            ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
            scheduler.setPoolSize(10);
            scheduler.afterPropertiesSet();
        }
        return scheduler;
    }
}

This basically replicates the functionality the ScheduledTaskRegistrar provides allowing you manage the ScheduledFuture(s). Hopefully this can help someone else in the future.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!