尝试在 java 中使用 quartz 制定每日工作计划
Trying to make a daily job schedule with quartz in java
我正在开发一个每天都应该做某事的 Discord 机器人。我尝试为此使用 Quartz 库,但该作业从未执行过。以下是我用于测试的代码:
import org.quartz.*;
import shedule.Daily;
import static org.quartz.CronScheduleBuilder.cronSchedule;
public class QuartzTestClass {
private void doIt() throws SchedulerException {
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler scheduler = schedFact.getScheduler();
scheduler.start();
JobBuilder jobBuilder = JobBuilder.newJob(Job.class);
JobDataMap data = new JobDataMap();
data.put("latch", this);
JobDetail jobDetail = jobBuilder.usingJobData("test", "QuartzTestClass")
.usingJobData(data)
.withIdentity("testJob", "Daily")
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("fireEveryDay", "Daily")
.startNow()
.withSchedule(cronSchedule("0 13 22 ? * * *")) //this is now +1 min
.build();
scheduler.scheduleJob(jobDetail, trigger);
}
public static void main (String[] args) throws SchedulerException {
new QuartzTestClass().doIt();
}
private class job implements Job{
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("executed");
}
}
}
作业从未被调用。有人能告诉我我做错了什么吗?
您好,
马丁.
- 您调用了 class 作业(小写字母),但在 JobBuilder Job.class 中使用(大写字母)- 它是 org.quartz 的接口。 .像调用您的 class - StartMyBotJob 来避免这种情况,或者打印 JobBuilder.newJob(job.class)
- 将你的工作 class 与你的 class 分开(在另一个文件中)或使其 public (对于另一个对象创建可见)和静态(如果嵌套 class 不是静态的,它依赖于 QuartzTestClass,并且需要它的实例来创建对象。查看更多 - https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html)
这个版本应该可以工作:
import org.quartz.*;
import static org.quartz.CronScheduleBuilder.cronSchedule;
public class QuartzTestClass {
private void doIt() throws SchedulerException {
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler scheduler = schedFact.getScheduler();
scheduler.start();
JobBuilder jobBuilder = JobBuilder.newJob(StartMyBotJob.class);
JobDataMap data = new JobDataMap();
data.put("latch", this);
JobDetail jobDetail = jobBuilder.usingJobData("test", "QuartzTestClass")
.usingJobData(data)
.withIdentity("testJob", "Daily")
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("fireEveryDay", "Daily")
.startNow()
.withSchedule(cronSchedule("0 13 22 ? * * *")) //this is now +1 min
.build();
scheduler.scheduleJob(jobDetail, trigger);
}
public static void main (String[] args) throws SchedulerException {
new QuartzTestClass().doIt();
}
public static class StartMyBotJob implements Job{
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("executed");
}
}
}
我正在开发一个每天都应该做某事的 Discord 机器人。我尝试为此使用 Quartz 库,但该作业从未执行过。以下是我用于测试的代码:
import org.quartz.*;
import shedule.Daily;
import static org.quartz.CronScheduleBuilder.cronSchedule;
public class QuartzTestClass {
private void doIt() throws SchedulerException {
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler scheduler = schedFact.getScheduler();
scheduler.start();
JobBuilder jobBuilder = JobBuilder.newJob(Job.class);
JobDataMap data = new JobDataMap();
data.put("latch", this);
JobDetail jobDetail = jobBuilder.usingJobData("test", "QuartzTestClass")
.usingJobData(data)
.withIdentity("testJob", "Daily")
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("fireEveryDay", "Daily")
.startNow()
.withSchedule(cronSchedule("0 13 22 ? * * *")) //this is now +1 min
.build();
scheduler.scheduleJob(jobDetail, trigger);
}
public static void main (String[] args) throws SchedulerException {
new QuartzTestClass().doIt();
}
private class job implements Job{
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("executed");
}
}
}
作业从未被调用。有人能告诉我我做错了什么吗?
您好, 马丁.
- 您调用了 class 作业(小写字母),但在 JobBuilder Job.class 中使用(大写字母)- 它是 org.quartz 的接口。 .像调用您的 class - StartMyBotJob 来避免这种情况,或者打印 JobBuilder.newJob(job.class)
- 将你的工作 class 与你的 class 分开(在另一个文件中)或使其 public (对于另一个对象创建可见)和静态(如果嵌套 class 不是静态的,它依赖于 QuartzTestClass,并且需要它的实例来创建对象。查看更多 - https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html)
这个版本应该可以工作:
import org.quartz.*;
import static org.quartz.CronScheduleBuilder.cronSchedule;
public class QuartzTestClass {
private void doIt() throws SchedulerException {
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler scheduler = schedFact.getScheduler();
scheduler.start();
JobBuilder jobBuilder = JobBuilder.newJob(StartMyBotJob.class);
JobDataMap data = new JobDataMap();
data.put("latch", this);
JobDetail jobDetail = jobBuilder.usingJobData("test", "QuartzTestClass")
.usingJobData(data)
.withIdentity("testJob", "Daily")
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("fireEveryDay", "Daily")
.startNow()
.withSchedule(cronSchedule("0 13 22 ? * * *")) //this is now +1 min
.build();
scheduler.scheduleJob(jobDetail, trigger);
}
public static void main (String[] args) throws SchedulerException {
new QuartzTestClass().doIt();
}
public static class StartMyBotJob implements Job{
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("executed");
}
}
}