maven工程
pom依赖
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency>
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* @author hrui
* @date 2023/11/17 0:52
*/
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
//Object user = context.getJobDetail().getJobDataMap().get("user");
Object user = context.getTrigger().getJobDataMap().get("user");
System.out.println(user+"在执行任务");
}
}
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
/**
* @author hrui
* @date 2023/11/17 0:52
*/
public class Test {
public static void main(String[] args) throws SchedulerException {
Scheduler scheduler= StdSchedulerFactory.getDefaultScheduler();
JobDetail jobDetail= JobBuilder.newJob(MyJob.class).withIdentity("job1", "group1")
.build();
Trigger trigger=TriggerBuilder.newTrigger().withIdentity("trg1", "tg1")
.usingJobData("user", "用户1")
.withSchedule(CronScheduleBuilder.cronSchedule("*/2 * * * * ?")).build();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Trigger newTrigger = TriggerBuilder.newTrigger()
.withIdentity("newTrigger", "tg1")
.startNow()
.usingJobData("user", "用户2")
.withSchedule(CronScheduleBuilder.cronSchedule("*/3 * * * * ?"))
.build();
// 替换原始触发器 替换之后,原来的触发器会被删除
scheduler.rescheduleJob(trigger.getKey(), newTrigger);
Trigger trigger1 = scheduler.getTrigger(trigger.getKey());
System.out.println(trigger1);
Trigger trigger2 = scheduler.getTrigger(newTrigger.getKey());
System.out.println(trigger2);
}
}
执行结果
解决日志问题
引入日志依赖 pom文件
<dependencies> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <!--log4j--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies>
resources下 新建 log4j.properties文件
# 设置日志级别为INFO log4j.rootCategory=INFO, stdout # 输出到控制台的appender配置 log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # 输出到文件的appender配置 log4j.appender.file=org.apache.log4j.RollingFileAppender #log4j.appender.file.File=/path/to/quartz.log log4j.appender.file.File=F:/quartz.log log4j.appender.file.MaxFileSize=10MB log4j.appender.file.MaxBackupIndex=5 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # 配置quartz的日志级别为INFO,并将日志输出到控制台和文件 log4j.logger.org.quartz=INFO, stdout, file