1. 程式人生 > >!!!超簡單 springboot2.0中 單機 quartz yml檔案配置 持久化到資料庫 看完不會你打我

!!!超簡單 springboot2.0中 單機 quartz yml檔案配置 持久化到資料庫 看完不會你打我

建立表

可到官網下載原始碼 解壓之後。在docs\dbTables檔案下選擇自己所需要的slq檔案。下載地址

新增引用

   <!--quartz-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>

編輯yml檔案

spring:
quartz: properties: org: quartz: scheduler: instanceName: clusteredScheduler instanceId: AUTO jobStore: class: org.quartz.impl.jdbcjobstore.JobStoreTX driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
tablePrefix: QRTZ_ useProperties: false dataSource: myDs threadPool: class: org.quartz.simpl.SimpleThreadPool threadCount: 10 threadPriority: 5 threadsInheritContextClassLoaderOfInitializingThread: true dataSource: myDs: driver: com
.mysql.jdbc.Driver URL: **** user: **** password: **** jdbc-store-type: jdbc

jobStore中的dataSource的值myDs和下面的dataSource的myDs對應

新增job類

public class OrderTimeoutJob extends QuartzJobBean {
    private Long orderId;
    @Autowired
    private ShopOrderService orderService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
       //程式碼
    }

    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }

}

專案使用

//import org.quartz.JobDetail;
//import org.quartz.Scheduler;
//import org.quartz.SchedulerException;
//import org.quartz.SimpleTrigger;

@Component
public class OrderJobMaker {
    @Autowired
    private Scheduler Scheduler;
    public void addScheme(){
        int timeout=10
        Long id=1000L;        
         SimpleTrigger trigger=(SimpleTrigger) newTrigger()
                .withIdentity("trigger-"+id, group)
                .startAt(futureDate(timeout, IntervalUnit.MINUTE)) // use DateBuilder to create a date in the future
                .build();

        JobDetail job = newJob(OrderTimeoutJob.class)
                .withIdentity("job-"+id, groupName)
                .usingJobData("orderId",id)
                .build();
        Scheduler.scheduleJob(job, trigger);
    }

}