SpringBoot基于数据库的定时任务统一管理的实现
定时任务1
importlombok.extern.slf4j.Slf4j; /** *@authorCreatedbyniugangon2019/12/24/15:29 */ @Slf4j publicclassTaskTest{ publicvoidtask1(){ log.info("反射调用测试[一]类"); } }
定时任务2
importlombok.extern.slf4j.Slf4j; /** *@authorCreatedbyniugangon2019/12/24/15:54 */ @Slf4j publicclassTaskTest2{ publicvoidtask2(){ log.info("反射调用测试[二]类"); } }
配置类
importlombok.Data; importlombok.extern.slf4j.Slf4j; importorg.springframework.context.annotation.Configuration; importorg.springframework.scheduling.annotation.EnableScheduling; importorg.springframework.scheduling.annotation.SchedulingConfigurer; importorg.springframework.scheduling.config.CronTask; importorg.springframework.scheduling.config.ScheduledTask; importorg.springframework.scheduling.config.ScheduledTaskRegistrar; importjava.lang.reflect.Method; importjava.util.ArrayList; importjava.util.List; /** *@authorCreatedbyniugangon2019/12/24/15:19 */ @Configuration @EnableScheduling @Slf4j publicclassCompleteScheduleConfigimplementsSchedulingConfigurer{ privatestaticListtaskRecordList=newArrayList<>(); /* *模拟数据库存储 */ static{ TaskRecordtaskRecord=newTaskRecord(); taskRecord.setExecuteMehod("task1"); taskRecord.setClassPath("com.example.demo.pojo.TaskTest"); taskRecord.setCron("0/5****?"); taskRecordList.add(taskRecord); TaskRecordtaskRecord2=newTaskRecord(); taskRecord2.setExecuteMehod("task2"); taskRecord2.setClassPath("com.example.demo.pojo.TaskTest2"); taskRecord2.setCron("0/10****?"); taskRecordList.add(taskRecord2); } @Override publicvoidconfigureTasks(ScheduledTaskRegistrartaskRegistrar){ //taskRegistrar.addCronTask(()->log.info("执行定时任务,{}",LocalDateTime.now()),"0/5****?"); /*taskRegistrar.addCronTask(newRunnable(){ @Override publicvoidrun(){ try{ Class>aClass=Class.forName("com.example.demo.pojo.TaskTest"); Objecto=aClass.newInstance(); Method[]declaredMethods=aClass.getDeclaredMethods(); for(MethoddeclaredMethod:declaredMethods){ declaredMethod.invoke(o); //log.info("方法名称:{}",declaredMethod.getName()); } }catch(Exceptione){ e.printStackTrace(); } } },"0/5****?");*/ for(TaskRecordtaskRecord:taskRecordList){ StringclassPath=taskRecord.getClassPath(); Stringcron=taskRecord.getCron(); StringexecuteMehod=taskRecord.getExecuteMehod(); Runnablerunnable=()->{ Class>aClass; try{ aClass=Class.forName(classPath); Objecto=aClass.newInstance(); Method[]declaredMethods=aClass.getDeclaredMethods(); for(MethoddeclaredMethod:declaredMethods){ if(declaredMethod.getName().equals(executeMehod)){ ///log.info("方法名称:{}",declaredMethod.getName()); declaredMethod.invoke(o); } } }catch(Exceptione1){ e1.printStackTrace(); } }; CronTaskcronTask=newCronTask(runnable,cron); ScheduledTaskscheduledTask=taskRegistrar.scheduleCronTask(cronTask); //scheduledTask.cancel();取消定时任务 } } @Data privatestaticclassTaskRecord{ privateStringclassPath; privateStringexecuteMehod; privateStringcron; //可以在增加一个type执行其他类型的定时任务 } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。