Spring Boot and RESTful API(10)Schedule and Dynamic Job

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值