- 创建一个单例bean来注入
TaskScheduler
。这将持有的状态变量的所有ScheduledFuture
小号,像private ScheduledFuture job1;
- 在部署时,从数据库加载所有计划数据并启动作业,并填充所有状态变量,例如
job1
。 - 更改调度数据后,取消相应的
Future
(例如job1
),然后使用新的调度数据重新启动它。
此处的关键思想是在
Future创建s时对其进行控制,以便将其保存在某些状态变量中,以便在调度数据中的某些内容发生更改时,可以将其取消。
这是工作代码:
applicationContext.xml
<task:annotation-driven /><task:scheduler id="infScheduler" pool-size="10"/>
单例bean,包含
Futures
@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); } }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)