!!!超简单 springboot2.0中 单机 quartz yml文件配置 持久化到数据库 看完不会你打我

创建表

可到官网下载源码 解压之后。在docs\dbTables文件下选择自己所需要的slq文件。下载地址

添加引用

   
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-quartzartifactId>
        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);
    }

}

你可能感兴趣的:(java)