SpringBoot整合Quartz,做到数据库持久化

SpringBoot整合Quartz,做到数据库持久化,第1张

SpringBoot整合Quartz,做到数据库持久化

pom文件引入



    4.0.0
    com.quartz
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
        UTF-8
        UTF-8
        2.3.7.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.4
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
            2.5.2
        
        
            mysql
            mysql-connector-java
        

        
            org.springframework.boot
            spring-boot-starter-quartz
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    1.8
                    1.8
                    UTF-8
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.3.7.RELEASE
                
                    com.quartz.demo.DemoApplication
                
                
                    
                        repackage
                        
                            repackage
                        
                    
                
            
        
    


properties配置

spring.application.name = quartz
server.port=8080


spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://rm-bp12g7w3j8kd398juco.mysql.rds.aliyuncs.com:3306/quartz?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123lmlmZqy05100321&***#

spring.quartz.job-store-type=jdbc   # 持久化
spring.quartz.jdbc.initialize-schema=never # 是设置每次启动项目是否重建表结构,并且清空里面的数据,有三个选项可以选择always,never,embedded
spring.quartz.scheduler-name=testScheduler

controller

package com.quartz.demo.controller;

import com.quartz.demo.quartz.MyJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.quartz.*;

@RestController
@RequestMapping("/quartz")
public class QuartzController {
    @Autowired
    private Scheduler scheduler;

    @GetMapping("index")
    public void index(){
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("1","123123");
        // 执行任务
        JobDetail jobBuilder =  JobBuilder.newJob(MyJob.class)
                .withIdentity("job1","job2").setJobData(jobDataMap).build();
        //触发器
        CronTrigger build = TriggerBuilder.newTrigger()
                .withIdentity("trigger", "trigger1").usingJobData(jobDataMap).
                        startNow().withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?")).build();

        try {

            scheduler.scheduleJob(jobBuilder,build);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
 }



# 主要执行目录
package com.quartz.demo.quartz;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.time.LocalDateTime;


public class MyJob extends QuartzJobBean {
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("11111111111111111111");

    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4999326.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存