spring boot 定时任务

    xiaoxiao2023-10-22  126

    @[TOC] spring boot 定时任务

    springboot定时任务

    1.先写一个QuartzUtil 的工具类

    工具类中 首先注册一个任务和触发器 //参数是要执行定时任务的方法所在的类 public void registerJobAndTrigger(TransalatePageService onlinePaymentService) { try { // 获取调度器 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); // 开启调度器 scheduler.start(); // 要做的相关服务 orderQuartz(scheduler, onlinePaymentService); } catch (SchedulerException e) { e.printStackTrace(); } } 将 orderQuartz 方法完善 public void orderQuartz(Scheduler scheduler, MarketingQuartzService marketingQuartzService) throws SchedulerException { //可有可无 看情况而定 String uuid = UUID.randomUUID().toString().replaceAll("-", ""); String quartzJobName = "orderVolumeJobName" + uuid; String quartzJobGroup = "orderVolumeJobGroup" + uuid; //写好的定时任务类 JobDetail job = JobBuilder.newJob(MarketingJob.class).withIdentity(quartzJobName, quartzJobGroup).build(); JobDataMap map = job.getJobDataMap(); map.put("quartzEntity", marketingQuartzService); String quartzTriName = "orderVolumeTriName" + uuid; String quartzTriGroup = "orderVolumeTriGroup" + uuid; // 每秒跑一任务 Trigger trigger = TriggerBuilder.newTrigger().withIdentity(quartzTriName, quartzTriGroup).startNow() .withSchedule(simpleSchedule().withIntervalInSeconds(1).repeatForever()) .build(); scheduler.scheduleJob(job, trigger); } 完善任务类 //该类需要实现JOB接口 并重写execute方法 public class testJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap(); ChangeTAffProductService changeTAffProductService = (ChangeTAffProductService)jobDataMap.get("quartzEntity"); //在该类中调用要定时执行的方法 changeTAffProductService.getChange(); } } 要把写好的定时任务工具类 配置到spring容器中 让spring来管理,所以需要写一个javaBean //使用注解 以便可以被spring容器管理 @Component public class testApplicationRunner implements ApplicationRunner { //将需要定时执行的方法 注入进来 @Autowired private ChangeTAffProductService changeTAffProductService; @Override public void run(ApplicationArguments applicationArguments) throws Exception { //启动工具类 将注入进来的service 当参数传递过去 testUtil testUtil = new testUtil(); testUtil.registerJobAndTrigger(changeTAffProductService); } } 这样 一个spring boot 定时任务就完成了

    (二)spring MVC 定时任务

    只需要在web层的 application-web.xml中 配置一下信息即可其中 ref 指向的是要 定时执行的任务所在的servicemethod 指向的是要 定时执行的方法名称cron 是配置时间 这个可以百度 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" ---------------------------配置这个 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task ---------------------------------------配置这个 http://www.springframework.org/schema/task/spring-task-3.0.xsd">-----------配置这个 <!--<task:executor id="executor" pool-size="5" /> <task:scheduler id="scheduler" pool-size="10" /> <task:annotation-driven executor="executor" scheduler="scheduler" />--> <context:property-placeholder location="classpath:web.properties"/> <import resource="classpath:spring-servlet.xml"/> <import resource="classpath*:applicationContext-biz.xml" /> <import resource="classpath*:applicationContext-cache.xml" /> <import resource="classpath*:applicationContext-common.xml" /> <import resource="classpath*:applicationContext-dao.xml" /> <task:scheduled-tasks> -----------------------------------------------------------------------------最后配置这个 <task:scheduled ref="payBillsService" method="getByStatusPayBills" cron="0 0/3 * * * ?"/> <task:scheduled ref="payBillsService" method="getByStatusPayBillss" cron="0 0/3 * * * ?"/> <!--<task:scheduled ref="payDataFeeManagementService" method="getBalanceOfMyTimer" cron="* * 8 * * ?"/>--> </task:scheduled-tasks> </beans>
    最新回复(0)