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

如何在春季使用EnableScheduling批注在运行时重新启动计划的任务?

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

如何在春季使用EnableScheduling批注在运行时重新启动计划的任务?

  1. 创建一个单例bean来注入
    TaskScheduler
    。这将持有的状态变量的所有
    ScheduledFuture
    小号,像
    private ScheduledFuture job1;
  2. 在部署时,从数据库加载所有计划数据并启动作业,并填充所有状态变量,例如
    job1
  3. 更改调度数据后,取消相应的
    Future
    (例如
    job1
    ),然后使用新的调度数据重新启动它。

此处的关键思想是在

Future
创建s时对其进行控制,以便将其保存在某些状态变量中,以便在调度数据中的某些内容发生更改时,可以将其取消。

这是工作代码:

applicationContext.xml

<task:annotation-driven /><task:scheduler id="infScheduler" pool-size="10"/>

单例bean,包含

Future
s

@Componentpublic class SchedulerServiceImpl implements SchedulerService {        private static final Logger logger = LoggerFactory.getLogger(SchedulerServiceImpl.class);        @Autowired        @Qualifier(value="infScheduler")        private TaskScheduler taskScheduler;        @Autowired        private MyService myService;        private ScheduledFuture job1;//for other jobs you can add new private state variables        //Call this on deployment from the ScheduleDataRepository and everytime when schedule data changes.        @Override        public synchronized void scheduleJob(int jobNr, long newRate) {//you are free to change/add new scheduling data, but suppose for now you only want to change the rate     if (jobNr == 1) {//instead of if/else you could use a map with all job data  if (job1 != null) {//job was already scheduled, we have to cancel it          job1.cancel(true);  }  //reschedule the same method with a new rate  job1 = taskScheduler.scheduleAtFixedRate(new ScheduledMethodRunnable(myService, "methodInMyServiceToReschedule"), newRate);     }        }}


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

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

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