@EnableScheduling
注解和@Scheduled注解實現定時任務,也可以通過SchedulingConfigurer接口來實現定時任務。但是這兩種方式不能動態添加、刪除、啟動、停止任務。要實現動態增刪啟停定時任務功能,比較廣泛的做法是集成Quartz框架。但是本人的開發原則是:在滿足項目需求的情況下,盡量少的依賴其它框架,避免項目過于臃腫和復雜。
查看spring-context這個jar包中org.springframework.scheduling.ScheduledTaskRegistrar
這個類的源代碼,發現可以通過改造這個類就能實現動態增刪啟停定時任務功能。
添加執行定時任務的線程池配置類
@Configuration
publicclassSchedulingConfig{
@Bean
publicTaskSchedulertaskScheduler(){
ThreadPoolTaskSchedulertaskScheduler=newThreadPoolTaskScheduler();
//定時任務執行線程池核心線程數
taskScheduler.setPoolSize(4);
taskScheduler.setRemoveOnCancelPolicy(true);
taskScheduler.setThreadNamePrefix("TaskSchedulerThreadPool-");
returntaskScheduler;
}
}
添加ScheduledFuture的包裝類。ScheduledFuture是ScheduledExecutorService定時任務線程池的執行結果。
publicfinalclassScheduledTask{
volatileScheduledFuture>future;
/**
*取消定時任務
*/
publicvoidcancel(){
ScheduledFuture>future=this.future;
if(future!=null){
future.cancel(true);
}
}
}
添加Runnable接口實現類,被定時任務線程池調用,用來執行指定bean里面的方法。
publicclassSchedulingRunnableimplementsRunnable{
privatestaticfinalLoggerlogger=LoggerFactory.getLogger(SchedulingRunnable.class);
privateStringbeanName;
privateStringmethodName;
privateStringparams;
publicSchedulingRunnable(StringbeanName,StringmethodName){
this(beanName,methodName,null);
}
publicSchedulingRunnable(StringbeanName,StringmethodName,Stringparams){
this.beanName=beanName;
this.methodName=methodName;
this.params=params;
}
@Override
publicvoidrun(){
logger.info("定時任務開始執行- bean:{},方法:{},參數:{}",beanName,methodName,params);
longstartTime=System.currentTimeMillis();
try{
Objecttarget=SpringContextUtils.getBean(beanName);
Methodmethod=null;
if(StringUtils.isNotEmpty(params)){
method=target.getClass().getDeclaredMethod(methodName,String.class);
}else{
method=target.getClass().getDeclaredMethod(methodName);
}
ReflectionUtils.makeAccessible(method);
if(StringUtils.isNotEmpty(params)){
method.invoke(target,params);
}else{
method.invoke(target);
}
}catch(Exceptionex){
logger.error(String.format("定時任務執行異常- bean:%s,方法:%s,參數:%s ",beanName,methodName,params),ex);
}
longtimes=System.currentTimeMillis()-startTime;
logger.info("定時任務執行結束- bean:{},方法:{},參數:{},耗時:{}毫秒",beanName,methodName,params,times);
}
@Override
publicbooleanequals(Objecto){
if(this==o)returntrue;
if(o==null||getClass()!=o.getClass())returnfalse;
SchedulingRunnablethat=(SchedulingRunnable)o;
if(params==null){
returnbeanName.equals(that.beanName)&&
methodName.equals(that.methodName)&&
that.params==null;
}
returnbeanName.equals(that.beanName)&&
methodName.equals(that.methodName)&&
params.equals(that.params);
}
@Override
publicinthashCode(){
if(params==null){
returnObjects.hash(beanName,methodName);
}
returnObjects.hash(beanName,methodName,params);
}
}
添加定時任務注冊類,用來增加、刪除定時任務。
@Component
publicclassCronTaskRegistrarimplementsDisposableBean{
privatefinalMapscheduledTasks=newConcurrentHashMap<>(16);
@Autowired
privateTaskSchedulertaskScheduler;
publicTaskSchedulergetScheduler(){
returnthis.taskScheduler;
}
publicvoidaddCronTask(Runnabletask,StringcronExpression){
addCronTask(newCronTask(task,cronExpression));
}
publicvoidaddCronTask(CronTaskcronTask){
if(cronTask!=null){
Runnabletask=cronTask.getRunnable();
if(this.scheduledTasks.containsKey(task)){
removeCronTask(task);
}
this.scheduledTasks.put(task,scheduleCronTask(cronTask));
}
}
publicvoidremoveCronTask(Runnabletask){
ScheduledTaskscheduledTask=this.scheduledTasks.remove(task);
if(scheduledTask!=null)
scheduledTask.cancel();
}
publicScheduledTaskscheduleCronTask(CronTaskcronTask){
ScheduledTaskscheduledTask=newScheduledTask();
scheduledTask.future=this.taskScheduler.schedule(cronTask.getRunnable(),cronTask.getTrigger());
returnscheduledTask;
}
@Override
publicvoiddestroy(){
for(ScheduledTasktask:this.scheduledTasks.values()){
task.cancel();
}
this.scheduledTasks.clear();
}
}
添加定時任務示例類
@Component("demoTask")
publicclassDemoTask{
publicvoidtaskWithParams(Stringparams){
System.out.println("執行有參示例任務:"+params);
}
publicvoidtaskNoParams(){
System.out.println("執行無參示例任務");
}
}
定時任務數據庫表設計
添加定時任務實體類
publicclassSysJobPO{
/**
*任務ID
*/
privateIntegerjobId;
/**
*bean名稱
*/
privateStringbeanName;
/**
*方法名稱
*/
privateStringmethodName;
/**
*方法參數
*/
privateStringmethodParams;
/**
*cron表達式
*/
privateStringcronExpression;
/**
*狀態(1正常0暫停)
*/
privateIntegerjobStatus;
/**
*備注
*/
privateStringremark;
/**
*創建時間
*/
privateDatecreateTime;
/**
*更新時間
*/
privateDateupdateTime;
publicIntegergetJobId(){
returnjobId;
}
publicvoidsetJobId(IntegerjobId){
this.jobId=jobId;
}
publicStringgetBeanName(){
returnbeanName;
}
publicvoidsetBeanName(StringbeanName){
this.beanName=beanName;
}
publicStringgetMethodName(){
returnmethodName;
}
publicvoidsetMethodName(StringmethodName){
this.methodName=methodName;
}
publicStringgetMethodParams(){
returnmethodParams;
}
publicvoidsetMethodParams(StringmethodParams){
this.methodParams=methodParams;
}
publicStringgetCronExpression(){
returncronExpression;
}
publicvoidsetCronExpression(StringcronExpression){
this.cronExpression=cronExpression;
}
publicIntegergetJobStatus(){
returnjobStatus;
}
publicvoidsetJobStatus(IntegerjobStatus){
this.jobStatus=jobStatus;
}
publicStringgetRemark(){
returnremark;
}
publicvoidsetRemark(Stringremark){
this.remark=remark;
}
publicDategetCreateTime(){
returncreateTime;
}
publicvoidsetCreateTime(DatecreateTime){
this.createTime=createTime;
}
publicDategetUpdateTime(){
returnupdateTime;
}
publicvoidsetUpdateTime(DateupdateTime){
this.updateTime=updateTime;
}
}
booleansuccess=sysJobRepository.addSysJob(sysJob);
if(!success)
returnOperationResUtils.fail("新增失敗");
else{
if(sysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())){
SchedulingRunnabletask=newSchedulingRunnable(sysJob.getBeanName(),sysJob.getMethodName(),sysJob.getMethodParams());
cronTaskRegistrar.addCronTask(task,sysJob.getCronExpression());
}
}
returnOperationResUtils.success();
修改定時任務,先移除原來的任務,再啟動新任務
booleansuccess=sysJobRepository.editSysJob(sysJob);
if(!success)
returnOperationResUtils.fail("編輯失敗");
else{
//先移除再添加
if(existedSysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())){
SchedulingRunnabletask=newSchedulingRunnable(existedSysJob.getBeanName(),existedSysJob.getMethodName(),existedSysJob.getMethodParams());
cronTaskRegistrar.removeCronTask(task);
}
if(sysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())){
SchedulingRunnabletask=newSchedulingRunnable(sysJob.getBeanName(),sysJob.getMethodName(),sysJob.getMethodParams());
cronTaskRegistrar.addCronTask(task,sysJob.getCronExpression());
}
}
returnOperationResUtils.success();
刪除定時任務
booleansuccess=sysJobRepository.deleteSysJobById(req.getJobId());
if(!success)
returnOperationResUtils.fail("刪除失敗");
else{
if(existedSysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())){
SchedulingRunnabletask=newSchedulingRunnable(existedSysJob.getBeanName(),existedSysJob.getMethodName(),existedSysJob.getMethodParams());
cronTaskRegistrar.removeCronTask(task);
}
}
returnOperationResUtils.success();
定時任務啟動/停止狀態切換
if(existedSysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())){
SchedulingRunnabletask=newSchedulingRunnable(existedSysJob.getBeanName(),existedSysJob.getMethodName(),existedSysJob.getMethodParams());
cronTaskRegistrar.addCronTask(task,existedSysJob.getCronExpression());
}else{
SchedulingRunnabletask=newSchedulingRunnable(existedSysJob.getBeanName(),existedSysJob.getMethodName(),existedSysJob.getMethodParams());
cronTaskRegistrar.removeCronTask(task);
}
添加實現了CommandLineRunner接口的SysJobRunner類,當spring boot項目啟動完成后,加載數據庫里狀態為正常的定時任務。
@Service
publicclassSysJobRunnerimplementsCommandLineRunner{
privatestaticfinalLoggerlogger=LoggerFactory.getLogger(SysJobRunner.class);
@Autowired
privateISysJobRepositorysysJobRepository;
@Autowired
privateCronTaskRegistrarcronTaskRegistrar;
@Override
publicvoidrun(String...args){
//初始加載數據庫里狀態為正常的定時任務
ListjobList=sysJobRepository.getSysJobListByStatus(SysJobStatus.NORMAL.ordinal());
if(CollectionUtils.isNotEmpty(jobList)){
for(SysJobPOjob:jobList){
SchedulingRunnabletask=newSchedulingRunnable(job.getBeanName(),job.getMethodName(),job.getMethodParams());
cronTaskRegistrar.addCronTask(task,job.getCronExpression());
}
logger.info("定時任務已加載完畢...");
}
}
}
工具類SpringContextUtils,用來從spring容器里獲取bean
@Component
publicclassSpringContextUtilsimplementsApplicationContextAware{
privatestaticApplicationContextapplicationContext;
@Override
publicvoidsetApplicationContext(ApplicationContextapplicationContext)
throwsBeansException{
SpringContextUtils.applicationContext=applicationContext;
}
publicstaticObjectgetBean(Stringname){
returnapplicationContext.getBean(name);
}
publicstaticTgetBean(ClassrequiredType) {
returnapplicationContext.getBean(requiredType);
}
publicstaticTgetBean(Stringname,ClassrequiredType) {
returnapplicationContext.getBean(name,requiredType);
}
publicstaticbooleancontainsBean(Stringname){
returnapplicationContext.containsBean(name);
}
publicstaticbooleanisSingleton(Stringname){
returnapplicationContext.isSingleton(name);
}
publicstaticClass?extends?Object>getType(Stringname){
returnapplicationContext.getType(name);
}
}
本文完,參考本文代碼可成功運行,親測!
(感謝閱讀,希望對你所有幫助)來源:www.jianshu.com/p/0f68936393fd-
源代碼
+關注
關注
96文章
2945瀏覽量
66730 -
spring
+關注
關注
0文章
340瀏覽量
14338 -
Boot
+關注
關注
0文章
149瀏覽量
35823 -
SpringBoot
+關注
關注
0文章
173瀏覽量
177
原文標題:告別硬編碼,SpringBoot實現動態增刪啟停定時任務
文章出處:【微信號:AndroidPush,微信公眾號:Android編程精選】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
評論