在各自岗位上尽职尽责,无需豪言壮语,默默行动会诠释一切。这世界,虽然没有绝对的公平,但是努力就会增加成功和变好的可能性!而这带着未知变量的可能性,就足以让我们普通人拼命去争取了。
欢迎来到~一支会记忆的笔~博客主页

spring -boot定时任务基于 quartz 实现

 

任务类一

 

package com.quartz.springboot_quartz.job;

import org.quartz.*;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ScheduledJob implements Job {

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        //获取任务名称
        JobKey key = jobExecutionContext.getJobDetail().getKey();
        //JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        //String jobSays = dataMap.getString("jobSays");



        System.out.println("Instance " + key + "     CRON ----> schedule job1 is running ... + " + name + "  ---->  " + dateFormat.format(new Date()));
    }
}

 

任务类二

package com.quartz.springboot_quartz.job;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;

import java.text.SimpleDateFormat;
import java.util.Date;
public class ScheduledJob2 implements Job {

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        //获取任务名称
        JobKey key = jobExecutionContext.getJobDetail().getKey();
        //JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap();

        System.out.println("Instance " + key + "     CRON ----> schedule job1 is running ... + " + name + "  ---->  " + dateFormat.format(new Date()));
    }
}

开始构建

接下来构建 JobDetail,并且构建时传⼊ name 属性的值,构建 JobTrigger 和 scheduleBuilder,最后使⽤
Scheduler 启动定时任务。

package com.quartz.springboot_quartz.scheduler;

import com.quartz.springboot_quartz.job.ScheduledJob;
import com.quartz.springboot_quartz.job.ScheduledJob2;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

@Component
public class CronSchedulerJob {

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    private void scheduleJob1(Scheduler scheduler) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class) .withIdentity("ScheduledJob", "group1").build();
        // 6的倍数秒执行 也就是 6 12 18 24 30 36 42 ....
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/6 * * * * ?");
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")
                .usingJobData("name","王智1").withSchedule(scheduleBuilder).build();
        scheduler.scheduleJob(jobDetail,cronTrigger);
    }

    private void scheduleJob2(Scheduler scheduler) throws SchedulerException{
        JobDetail jobDetail = JobBuilder.newJob(ScheduledJob2.class) .withIdentity("job222222", "group2").build();
        // 12秒的倍数执行  12  24 36  48  60
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/12 * * * * ?");
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "group2")
                .usingJobData("name","王智2").withSchedule(scheduleBuilder).build();
        scheduler.scheduleJob(jobDetail,cronTrigger);
    }

    /**
     * 
     * @Description 同时启动两个定时任务
     * 
     **/
    public void scheduleJobs() throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        scheduleJob1(scheduler);
        scheduleJob2(scheduler);
    }
}

执行任务

package com.quartz.springboot_quartz;

import com.quartz.springboot_quartz.scheduler.CronSchedulerJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 *
 * @Description 项目启动即执行
 * 
 */
@Component
public class MyStartupRunner implements CommandLineRunner {

    @Autowired
    public CronSchedulerJob scheduleJobs;

    @Override
    public void run(String... args) throws Exception {
        scheduleJobs.scheduleJobs();
        System.out.println(">>>>>>>>>>>>>>>定时任务开始执行<<<<<<<<<<<<<");
    }
}

启动类

package com.quartz.springboot_quartz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class SpringbootQuartzApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootQuartzApplication.class, args);
    }

}

 输出结果:

Instance group1.ScheduledJob     CRON ----> schedule job1 is running ... + 王智1  ---->  17:35:24
Instance group2.job222222     CRON ----> schedule job1 is running ... + 王智2  ---->  17:35:24
Instance group1.ScheduledJob     CRON ----> schedule job1 is running ... + 王智1  ---->  17:35:30

 

posted @ 2019-07-26 15:18  一支会记忆的笔  阅读(525)  评论(0)    收藏  举报
返回顶部
【学无止境❤️谦卑而行】