Quartz實例

 <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context-support</artifactId>
  </dependency>
  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-quartz</artifactId>
  </dependency>
import com.example33.demo.job.QuartzDemo;
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;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

@Configuration
public class QuartzConfig {
    /**
     * 1,創(chuàng)建Job對象
     */
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean(){
        JobDetailFactoryBean factory = new JobDetailFactoryBean();
        factory.setJobClass(QuartzDemo.class);
        return factory;
    }

    /**
     * 2,創(chuàng)建Trigger對象 --簡單trigger
     */
//    @Bean
//    public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
//        SimpleTriggerFactoryBean factory = new SimpleTriggerFactoryBean();
//        //關(guān)聯(lián)JobDetail對象
//        factory.setJobDetail(jobDetailFactoryBean.getObject());
//        //該參數(shù)表示一個執(zhí)行的毫秒數(shù)
//        factory.setRepeatInterval(2000);
//        //重復次數(shù)
//        factory.setRepeatCount(5);
//        return factory;
//    }

    /**
     * 2,創(chuàng)建Trigger對象  -- cron 表達式
     * @param jobDetailFactoryBean
     * @return
     */
    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
        CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
        factory.setJobDetail(jobDetailFactoryBean.getObject());
        factory.setCronExpression("0/2 * * * * ?");
        return factory;
    }
    /**
     * 3,創(chuàng)建Scheduler對象
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean,MyAdaptableJobFactory myAdaptableJobFactory){
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        //關(guān)聯(lián)trigger
        factory.setTriggers(cronTriggerFactoryBean.getObject());
        factory.setJobFactory(myAdaptableJobFactory);
        return factory;
    }
}
cron 表達式
例如: 0/2 * * * * ?
 秒 分鐘 小時 日 月 星期
 星號(*):可用在所有字段中,表示對應時間域的每一個時刻,例如:"每分鐘"
 問號(?):該字段只在日期喝星期字段中使用,它通常指定為"無意義的值",相當于占位符
 減號(-):表達一個范圍,如再小時字段中使用"10-12",則表示從10到12點,即10,11,12
 逗號(,):表達一個列表值,如再星期字段中使用"MON,WED,FRI",則表示星期一,星期三和星期五
 斜杠(/):x/y表達一個等步長序列,x為起始值,y為增量步長值,如在分鐘字段中使用 0/15,則表示為0,15,30和45秒,
 星期字段 1~7 1表示星期日  7表示星期六
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;

@Component("myAdaptableJobFactory")
public class MyAdaptableJobFactory extends AdaptableJobFactory {

    private final AutowireCapableBeanFactory autowireCapableBeanFactory;

    public MyAdaptableJobFactory(AutowireCapableBeanFactory autowireCapableBeanFactory) {
        this.autowireCapableBeanFactory = autowireCapableBeanFactory;
    }

    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        Object obj = super.createJobInstance(bundle);
        this.autowireCapableBeanFactory.autowireBean(obj);
        return obj;
    }
}
import com.example33.demo.service.UsersService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

public class QuartzDemo implements Job {

    @Autowired
    private UsersService usersService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("Execute..."+new Date());
        this.usersService.addUsers();
    }
}
@SpringBootApplication(scanBasePackages = "com.example33")
@MapperScan(basePackages = "com.example33.**.mapper")
public class DemoApplication {

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

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容