栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

下次调用之前中断Spring Scheduler任务

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

下次调用之前中断Spring Scheduler任务

因此,使用SchedulingConfigurer->
configureTasks时,您无法访问我正在使用的Spring版本(4.2.7.RELEASE)中的ScheduledFuture。从我读过的几篇文章中,已经提到它是将来可能的功能。我通过执行以下操作解决了这个问题:

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@RestControllerpublic 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());    }        private void cancelAll() {        for (ScheduledFuture scheduledFuture : scheduledFutures) { scheduledFuture.cancel(true);        }        scheduledFutures.clear();    }        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;    }}

这基本上复制了ScheduledTaskRegistrar提供的功能,使您可以管理ScheduledFuture。希望这可以在将来对其他人有所帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/615602.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号