quartz定时任务

发布于:2023-01-01 ⋅ 阅读:(309) ⋅ 点赞:(0)

quartz定时任务

一、常规方法进行定时任务

1 Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.alicc</groupId>
    <artifactId>quartz</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.7.3</version>
        </dependency>
    </dependencies>

</project>

2 定时任务配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class QuartzConfig {

    //job:做什么事
    @Bean
    public MethodInvokingJobDetailFactoryBean 
           			methodInvokingJobDetailFactoryBean(MyJob myJob) {
        MethodInvokingJobDetailFactoryBean JobDetailFactoryBean = 
            							new MethodInvokingJobDetailFactoryBean();
        JobDetailFactoryBean.setTargetObject(myJob);//需要定时的方法存在的类
        JobDetailFactoryBean.setTargetMethod("myJob");//需要定时的方法
        return JobDetailFactoryBean;
    }

    //trigger:什么时候做
    @Bean//trigger(job)
    public CronTriggerFactoryBean cronTriggerFactoryBean(
            				MethodInvokingJobDetailFactoryBean JobDetailFactoryBean) {
        CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
        triggerFactoryBean.setCronExpression("*/2 * * * * ?");//cron表达式
        triggerFactoryBean.setJobDetail(JobDetailFactoryBean.getObject());
        return triggerFactoryBean;
    }

    //scheduled:什么时候做什么事
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(
        								CronTriggerFactoryBean triggerFactoryBean) {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setTriggers(triggerFactoryBean.getObject());
        return schedulerFactoryBean;
    }
}

3 创建Job

@Component
public class MyJob {
    public void myJob(){
    	//举例获取当前时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        System.out.println(format);
    }
}

4 启动类以及测试结果

package com.alicc;

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

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

测试结果:
在这里插入图片描述

二、通过注解进行定时任务

1 Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.alicc</groupId>
    <artifactId>quartz</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.7.3</version>
        </dependency>
    </dependencies>

</project>

2 创建Job

package com.alicc.myJob;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

@Component
public class MyJob {
    @Scheduled(cron = "*/2 * * * * ?")//开启定时任务并配置cron表达式
    public void myJob(){
    	//举例获取当前时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        System.out.println(format);
    }
}

3 启动类以及测试结果

package com.alicc;

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

@SpringBootApplication
@EnableScheduling
public class QuartzApp {
    public static void main(String[] args) {
        SpringApplication.run(QuartzApp.class,args);
    }
}

测试结果:
在这里插入图片描述

本文含有隐藏内容,请 开通VIP 后查看