springboot整合quartz实现定时任务

1 篇文章 0 订阅

1. 创建springboot项目,引入相关依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2. 创建需要执行的定时任务

需继承 QuartzJobBean

public class TestTask1 extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("TestQuartz01----" + sdf.format(new Date(System.currentTimeMillis())));
    }
}

public class TestTask2 extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("TestQuartz02----" + sdf.format(new Date(System.currentTimeMillis())));
    }
}

3. 编写 Quartz 配置类

@Configuration
public class QuartzConfig {
    @Bean
    public JobDetail testQuartz1() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("启动时执行 TestQuartz01----" + sdf.format(new Date(System.currentTimeMillis())));
        return JobBuilder
            .newJob(TestTask1.class)
            .withIdentity("testTask1")
            .storeDurably()
            .build();
    }

    @Bean
    public Trigger testQuartzTrigger1() {
        //5秒执行一次
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
            .withIntervalInSeconds(5)
            .repeatForever();
        return TriggerBuilder.newTrigger().forJob(testQuartz1())
            .withIdentity("testTask1")
            .withSchedule(scheduleBuilder)
            .build();
    }

    @Bean
    public JobDetail testQuartz2() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("启动时执行 TestQuartz02----" + sdf.format(new Date(System.currentTimeMillis())));
        return JobBuilder
            .newJob(TestTask2.class)
            .withIdentity("testTask2")
            .storeDurably()
            .build();
    }

    @Bean
    public Trigger testQuartzTrigger2() {
        //cron方式,每隔5秒执行一次
        return TriggerBuilder.newTrigger().forJob(testQuartz2())
            .withIdentity("testTask2")
            .withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ? "))
            .build();
    }
}

4. 启动主启动类测试

上述为了测试 SimpleScheduleBuilder 和 CronScheduleBuilder 调度方式,所以创建了两个job
测试结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值