SpringBoot整合quartz定时任务

1.pom.xml添加依赖

     <!--定时任务-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>
        <!--/定时任务-->

2.创建执行类

在executeInternal函数中执行定时任务的逻辑

package com.youqian.wlyportal.schedule;

import com.youqian.wlyportal.service.frontEnd.NewsService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * 定时任务执行类.
 * 从官网同步新闻到数据库
 *
 * @author 易泽林
 */
public class NewsSchedule extends QuartzJobBean {
    private static final Logger LOGGER = LoggerFactory.getLogger(NewsSchedule.class);
    @Autowired
    private NewsService newsService;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        LOGGER.error("--------------------" + context.getJobDetail().getDescription() + "定时任务执行开始--------------------");
        boolean b = newsService.synchronizeNews();
        LOGGER.error("执行新闻同步是否成功?" + b);
        LOGGER.error("--------------------" + context.getJobDetail().getDescription() + "定时任务执行结束--------------------");

    }
}

3.创建定时任务

创建 ScheduleService.java,用于创建定时任务,这里动态的创建了两个定时任务,每隔1秒和每隔2秒运行。创建时需要新建JobDetail(任务)以及CronTrigger(定时任务触发器) ,并且scheduler.scheduleJob(jobDetail,cronTrigger);把任务添加到任务队列中。

package com.youqian.wlyportal.schedule.service;

import com.youqian.wlyportal.pojo.sys.Schedule;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* 定时任务.
*
* @author 易泽林
* @version 1.0
* Created on 2018/11/4
*/
@Service
public class ScheduleService {
 @Autowired
 private Scheduler scheduler;

 public <T> void scheduleTask(Schedule schedule) throws SchedulerException {
     JobDetail jobDetail = JobBuilder.newJob(schedule.getJobClass())
         .withIdentity(schedule.getName(), schedule.getGroup())
         .withDescription(schedule.getDescription())
         .build();

     //cron表达式
     CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(schedule.getCron())
         .withMisfireHandlingInstructionDoNothing();

     CronTrigger cronTrigger = TriggerBuilder.newTrigger()
         .withIdentity(schedule.getName(), schedule.getGroup())
         .withDescription(schedule.getDescription())
         .withSchedule(scheduleBuilder)
         .startNow().build();

     scheduler.scheduleJob(jobDetail, cronTrigger);
 }
}

4.执行定时任务

package com.youqian.wlyportal.schedule.listener;


import com.youqian.wlyportal.pojo.sys.Schedule;
import com.youqian.wlyportal.schedule.NewsSchedule;
import com.youqian.wlyportal.schedule.service.ScheduleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;


/**
 * 执行定时任务.
 *
 * @author 易泽林
 */
@Component
public class StartListen implements ApplicationListener<ApplicationReadyEvent> {
    private static final Logger LOGGER = LoggerFactory.getLogger(StartListen.class);


    @Autowired
    private ScheduleService ScheduleService;


    @Override
    public void onApplicationEvent(final ApplicationReadyEvent event) {
        try {
            Schedule schedule = new Schedule();
            schedule.setJobClass(NewsSchedule.class);
            schedule.setName("newsSynchronization");
            schedule.setGroup("news");
            schedule.setDescription("从官网同步新闻到数据库");
            schedule.setCron("0/10 * * * * ? *");
            ScheduleService.scheduleTask(schedule);
        } catch (Exception e) {
            LOGGER.error("任务调度失败", e);
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csweldn520

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值