spring boot整合squartz实现定时任务

一.新建项目:spt-integerate-quartz

1.pom文件:

  
    
      org.springframework.boot
      spring-boot-starter-web
      2.0.1.RELEASE
    
    
    
      org.quartz-scheduler
      quartz
      2.2.1
      
      
        
          slf4j-api
          org.slf4j
        
      
    
    
    
      org.springframework
      spring-context-support
      5.2.1.RELEASE
    
    
    
      org.springframework
      spring-tx
      5.2.1.RELEASE
    

2.自定义job类:

package com.ljf.spring.boot.demo.quartz.job;

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

import java.time.LocalTime;
import java.util.Date;

/**
 * @ClassName: MyDefineQuartz
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2020/07/27 08:39:16
 * @Version: V1.0
 **/
public class MyDefineQuartz implements Job {
    /**
     * 任务被触发时所执行的方法
     */
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("Execute...."+new Date()+"  "+ LocalTime.now());
    }
}

 3.配置quartz定时任务执行

package com.ljf.spring.boot.demo.quartz.config;

import com.ljf.spring.boot.demo.quartz.job.MyDefineQuartz;
import com.ljf.spring.boot.demo.quartz.model.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * @ClassName: QuartsConfig
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2020/07/27 09:13:13
 * @Version: V1.0
 **/
@Configuration
public class QuartsConfig {
    /**
     * 1.创建Job对象
     */
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean(){
        System.out.println("初始化,创建job对象......");
        JobDetailFactoryBean factory = new JobDetailFactoryBean();
        //关联我们自己的Job类
        factory.setJobClass(MyDefineQuartz.class);
        return factory;
    }

    /**
     * 2.Cron Trigger
     */
    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
        System.out.println("初始化,trigger对象......");
        CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
        factory.setJobDetail(jobDetailFactoryBean.getObject());
        //设置触发时间
        factory.setCronExpression("0/2 * * * * ?");
        return factory;
    }

    /**
     * 3.创建Scheduler对象
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean,User u){
        System.out.println("初始化,创建scheduler对象......");
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        //关联trigger
        factory.setTriggers(cronTriggerFactoryBean.getObject());
        System.out.println("初始化u的对象:"+u.getName());

        return factory;
    }
    /**
    * @author liujianfu
    * @description
    * @date 2020/7/27 0027 下午 12:26
    * @param []
    * @return com.ljf.spring.boot.demo.quartz.model.User
    */

    @Bean
    public User initUser(){
        User u=new User();
        u.setId(23);
        u.setName("ljf");
        return u;
    }
}

4.启动类:

package com.ljf.spring.boot.demo.quartz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
        System.out.println( "spring boot 和quarts集成程序启动成功!" );
    }
}

5.结果:

spring boot整合squartz实现定时任务_第1张图片

你可能感兴趣的:(java,springboot)