Spring Boot and RESTful API(10)Schedule and Dynamic Job
The major class we use is ThreadPoolTaskScheduler and we keep the reference in ScheduledFuture
package
com.sillycat.jobsmonitorapi.service;
import java.util.concurrent.ScheduledFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;
import com.sillycat.jobsmonitorapi.service.jobs.MyRunnable;
@Service
public class JobDynamicCronService {
@Autowired
private ThreadPoolTaskScheduler threadPoolTaskScheduler;
@Autowired
private MyRunnable myRunnable;
private ScheduledFuture<?> future;
@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
return new ThreadPoolTaskScheduler();
}
public void startCron() {
this.stopCron();
future = threadPoolTaskScheduler.schedule(myRunnable, new CronTrigger("*/10 * * * * *"));
}
public void stopCron() {
if (future != null) {
future.cancel(true);
}
}
}
Here is the Task Runnable Interface Class, we can call Spring Beans inside here.
package com.sillycat.jobsmonitorapi.service.jobs;
import java.io.Serializable;
import java.util.Date;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import com.sillycat.jobsmonitorapi.service.JobService;
@Service
public class MyRunnable implements Runnable, ApplicationContextAware, Serializable {
private static final long serialVersionUID = 394663593893594867L;
private ApplicationContext applicationContext;
public void run() {
applicationContext.getBean(JobService.class).echo();
System.out.println("DynamicTask.MyRunnable.run()," + new Date());
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
How we test that, I just start the Cron when the application start.
package com.sillycat.jobsmonitorapi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import com.sillycat.jobsmonitorapi.service.JobDynamicCronService;
@SpringBootApplication
@EnableScheduling
public class JobsMonitorAPIApplication extends SpringBootServletInitializer{
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(JobsMonitorAPIApplication.class);
context.getBean(JobDynamicCronService.class).startCron();
}
}
We can provide RESTful Interface to Start that.
References:
http://dev.dafan.info/detail/364139?p=14-39
http://412887952-qq-com.iteye.com/blog/2367538
http://blog.leanote.com/post/elegant/spring-%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1-taskScheduler
http://www.jianshu.com/p/780235132d81
scalable
http://www.jianshu.com/p/780235132d81
http://www.jianshu.com/p/4a7eb40f6852
http://www.jianshu.com/p/c6dbd6f17850
Spring Boot and RESTful API(10)Schedule and Dynamic Job
猜你喜欢
转载自sillycat.iteye.com/blog/2392268
今日推荐
周排行