spring boot 定时任务

@[TOC] spring boot 定时任务

springboot定时任务

1.先写一个QuartzUtil 的工具类

  1. 工具类中 首先注册一个任务和触发器
//参数是要执行定时任务的方法所在的类
public void registerJobAndTrigger(TransalatePageService onlinePaymentService) {
        try {
            // 获取调度器
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            // 开启调度器
            scheduler.start();
            // 要做的相关服务
            orderQuartz(scheduler, onlinePaymentService);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
  1. 将 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);
    }
  1. 完善任务类
//该类需要实现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();
    }
}
  1. 要把写好的定时任务工具类 配置到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);
   }
}
  1. 这样 一个spring boot 定时任务就完成了

(二)spring MVC 定时任务

  1. 只需要在web层的 application-web.xml中 配置一下信息即可
  2. 其中 ref 指向的是要 定时执行的任务所在的service
  3. method 指向的是要 定时执行的方法名称
  4. 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值