<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
package com.newpwr.wechat.service;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
public class HelloJob implements Job {
public static void main(String[] args) throws SchedulerException {
cc();
}
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("hello~~~~~~~~~~~~~~~~~~");
}
public static void cc() throws SchedulerException {
//定时器对象
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
//定义一个工作对象 设置工作名称与组名
JobDetail job =JobBuilder.newJob(HelloJob.class).withIdentity("job41","group1").build();
//定义一个触发器 简单Trigger 设置工作名称与组名 5秒触发一次
// Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1","group1").startNow().withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5)).build();
//定义一个任务调度的Trigger 设置工作名称与组名 每天的15:4触发一次
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1","group1").withSchedule(CronScheduleBuilder.cronSchedule("0 4 15 1-31 * ? *")).build();
//设置工作 与触发器
scheduler.scheduleJob(job, trigger);
// and start it off
//开始定时任务
scheduler.start();
}
}