SpringBoot整合quartz定时任务

SpringBoot整合quartz定时任务

1.pom.xml添加依赖

     
        
            org.springframework.boot
            spring-boot-starter-quartz
        
        

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  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 {
    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);
        }

    }

}

你可能感兴趣的:(SpringBoot整合quartz定时任务)