springBoot-quartz-Job

SpringBoot整合Quartz -JOB 完整实用例子

1.首先新建项目SpringBoot-Quartz pom.xml依赖 : 


    1.8



    
        org.springframework.boot
        spring-boot-starter-jdbc
    
    
        org.springframework.boot
        spring-boot-starter-web
    

    
        org.springframework.boot
        spring-boot-starter-test
        test
    

    
        com.microsoft.sqlserver
        sqljdbc4
        4.2
        4.2
    
    
        org.springframework.boot
        spring-boot-starter-thymeleaf
    
    
        org.springframework.boot
        spring-boot-starter
        
            
                org.springframework.boot
                spring-boot-starter-logging
            
        
    

    
    
        org.springframework.boot
        spring-boot-starter-log4j2
    

    
        org.quartz-scheduler
        quartz
        2.3.0
    

    
        org.springframework
        spring-context-support
    


    
        org.jmock
        jmock-junit4
        2.8.2
        test
    




    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    

2 编写job

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
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.stereotype.Component;
import java.io.Serializable;
import java.util.Date;


/**
 * Created by yan on 2017/5/16.
 * 个人待办任务
 */
@DisallowConcurrentExecution
public class MyJob implements Job, Serializable {
    private static final long serialVersionUID = 1L;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

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

        System.out.println("开始"+new Date());

    }


}

3 重新AdaptableJobFactory工厂,从spring获得bean(这个是很重要的)

import org.quartz.spi.TriggerFiredBundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;

@Component("MyJobFactory")
public class MyJobFactory extends AdaptableJobFactory
{

    // 需要使用这个BeanFactory对Qurartz创建好Job实例进行后续处理,属于Spring的技术范畴.
    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory;
    private static final Logger logger = LoggerFactory.getLogger(MyJobFactory.class);
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception
    {

        // 首先,调用父类的方法创建好Quartz所需的Job实例
        Object jobInstance = super.createJobInstance(bundle);
        // 然后,使用BeanFactory为创建好的Job实例进行属性自动装配并将其纳入到Spring容器的管理之中,属于Spring的技术范畴.
        this.capableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }


}

4、配置schedulerFactoryBean

Spring为了能集成Quartz,特意提供了管理Quartz的schedulerFactoryBean,必须配置,具

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.io.IOException;
import java.util.Properties;

    @Configuration
    @EnableScheduling
    public class SchedulerConfig {

        @Autowired
        private MyJobFactory myJobFactory;

        @Bean
        public SchedulerFactoryBean schedulerFactoryBean(Trigger updateTrigger) throws IOException {
            SchedulerFactoryBean factory = new SchedulerFactoryBean();
            factory.setOverwriteExistingJobs(true);
            // 延时启动
            factory.setStartupDelay(20);
            // 自定义Job Factory,用于Spring注入
            factory.setJobFactory(myJobFactory);
            factory.setTriggers(updateTrigger); return factory;
        }

        @Bean(name = "updateJobDetail")
        public JobDetailFactoryBean uploadJobDetail() {
            JobDetailFactoryBean jobDetail = new JobDetailFactoryBean();
            jobDetail.setJobClass(MyJob.class);
            return jobDetail;
        }

        @Bean(name = "updateTrigger")
        public CronTriggerFactoryBean updateTriggerFactoryBean(JobDetail updateJobDetail) {
            CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
            trigger.setCronExpression("0 0/1 * * * ?");
            trigger.setJobDetail(updateJobDetail);
            return trigger;
        }


5application.properties
#数据库连接
spring.datasource.url=jdbc:sqlserver://localhost:1433;DatabaseName=Text
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.username=sa
spring.datasource.password=123
spring.datasource.type= org.springframework.jdbc.datasource.DriverManagerDataSource

运行结果:

开始Mon Mar 11 14:15:16 CST 2019
开始Mon Mar 11 14:16:00 CST 2019
开始Mon Mar 11 14:17:00 CST 2019
开始Mon Mar 11 14:18:00 CST 2019
开始Mon Mar 11 14:19:00 CST 2019
开始Mon Mar 11 14:20:00 CST 2019
开始Mon Mar 11 14:21:00 CST 2019
开始Mon Mar 11 14:22:00 CST 2019
开始Mon Mar 11 14:23:00 CST 2019
开始Mon Mar 11 14:24:00 CST 2019
开始Mon Mar 11 14:25:00 CST 2019
开始Mon Mar 11 14:26:00 CST 2019

你可能感兴趣的:(SpringBoot)