pom文件引入
4.0.0 com.quartz demo0.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-weborg.mybatis.spring.boot mybatis-spring-boot-starter2.1.4 org.springframework.boot spring-boot-starter-data-jpa2.5.2 mysql mysql-connector-javaorg.springframework.boot spring-boot-starter-quartzmysql mysql-connector-javaruntime org.springframework.boot spring-boot-starter-testtest org.junit.vintage junit-vintage-engineorg.springframework.boot spring-boot-dependencies${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin3.8.1 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin2.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"); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)