前言
最近在項(xiàng)目中使用到定時(shí)任務(wù),之前一直都是使用Quartz 來(lái)實(shí)現(xiàn),最近看Spring 基礎(chǔ)發(fā)現(xiàn)其實(shí)Spring 提供 Spring Schedule 可以幫助我們實(shí)現(xiàn)簡(jiǎn)單的定時(shí)任務(wù)功能。
下面說(shuō)一下兩種方式在Spring Boot 項(xiàng)目中的使用。
Spring Schedule 實(shí)現(xiàn)定時(shí)任務(wù)
Spring Schedule 實(shí)現(xiàn)定時(shí)任務(wù)有兩種方式 1. 使用XML配置定時(shí)任務(wù), 2. 使用 @Scheduled 注解。 因?yàn)槭荢pring Boot 項(xiàng)目 可能盡量避免使用XML配置的形式,主要說(shuō)注解的形式.
Spring Schedule 提供三種形式的定時(shí)任務(wù):
固定等待時(shí)間 @Scheduled(fixedDelay = 時(shí)間間隔 )
1
2
3
4
5
6
7
8
9
10
11
12
|
@Component public class ScheduleJobs { public final static long SECOND = 1 * 1000 ; FastDateFormat fdf = FastDateFormat.getInstance( "yyyy-MM-dd HH:mm:ss" ); @Scheduled (fixedDelay = SECOND * 2 ) public void fixedDelayJob() throws InterruptedException { TimeUnit.SECONDS.sleep( 2 ); System.out.println( "[FixedDelayJob Execute]" +fdf.format( new Date())); } } |
固定間隔時(shí)間 @Scheduled(fixedRate = 時(shí)間間隔 )
1
2
3
4
5
6
7
8
9
10
11
|
@Component public class ScheduleJobs { public final static long SECOND = 1 * 1000 ; FastDateFormat fdf = FastDateFormat.getInstance( "yyyy-MM-dd HH:mm:ss" ); @Scheduled (fixedRate = SECOND * 4 ) public void fixedRateJob() { System.out.println( "[FixedRateJob Execute]" +fdf.format( new Date())); } } |
Corn表達(dá)式 @Scheduled(cron = Corn表達(dá)式)
1
2
3
4
5
6
7
8
9
10
11
|
@Component public class ScheduleJobs { public final static long SECOND = 1 * 1000 ; FastDateFormat fdf = FastDateFormat.getInstance( "yyyy-MM-dd HH:mm:ss" ); @Scheduled (cron = "0/4 * * * * ?" ) public void cronJob() { System.out.println( "[CronJob Execute]" +fdf.format( new Date())); } } |
Spring Boot 整合 Quartz 實(shí)現(xiàn)定時(shí)任務(wù)
添加Maven依賴
1
2
3
4
5
6
7
8
|
< dependency > < groupId >org.quartz-scheduler</ groupId > < artifactId >quartz</ artifactId > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context-support</ artifactId > </ dependency > |
Spring Boot 整合 Quartz
Spring 項(xiàng)目整合 Quartz 主要依靠添加 SchedulerFactoryBean 這個(gè) FactoryBean ,所以在maven 依賴中添加 spring-context-support 。
首先添加 QuartzConfig 類 來(lái)聲明相關(guān)Bean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@Configuration public class QuartzConfig { @Autowired private SpringJobFactory springJobFactory; @Bean public SchedulerFactoryBean schedulerFactoryBean() { SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); schedulerFactoryBean.setJobFactory(springJobFactory); return schedulerFactoryBean; } @Bean public Scheduler scheduler() { return schedulerFactoryBean().getScheduler(); } } |
這里我們需要注意 我注入了一個(gè) 自定義的JobFactory ,然后 把其設(shè)置為SchedulerFactoryBean 的 JobFactory。其目的是因?yàn)槲以诰唧w的Job 中 需要Spring 注入一些Service。
所以我們要自定義一個(gè)jobfactory, 讓其在具體job 類實(shí)例化時(shí) 使用Spring 的API 來(lái)進(jìn)行依賴注入。
SpringJobFactory 具體實(shí)現(xiàn):
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Component public class SpringJobFactory extends AdaptableJobFactory { @Autowired private AutowireCapableBeanFactory capableBeanFactory; @Override protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { Object jobInstance = super .createJobInstance(bundle); capableBeanFactory.autowireBean(jobInstance); return jobInstance; } } |
具體使用 (摘取自項(xiàng)目代碼):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@Service public class QuartzEventServiceImpl implements QuartzEventService { private static final String JOB_GROUP = "event_job_group" ; private static final String TRIGGER_GROUP = "event_trigger_group" ; @Autowired private Scheduler scheduler; @Override public void addQuartz(Event event) throws SchedulerException { JSONObject eventData = JSONObject.parseObject(event.getEventData()); Date triggerDate = eventData.getDate( "date" ); JobDetail job = JobBuilder.newJob(EventJob. class ).withIdentity(event.getId().toString(), JOB_GROUP).usingJobData(buildJobDateMap(event)).build(); Trigger trigger = TriggerBuilder.newTrigger().withIdentity(event.getId().toString(), TRIGGER_GROUP).startAt(triggerDate).build(); scheduler.scheduleJob(job, trigger); } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/javanoob/p/springboot_schedule.html