我有代码可以安排作业每5分钟执行一次。代码如下
JobDetail jobDetail = JobBuilder.newJob(ChieldJob.class).withIdentity(("job_"+sched_id+"_"+schd_name), "todaysJob")
.setJobData(chieldJobData).build();
进行触发的代码如下。
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(("trg_" + sched_id +"_"+schd_name), "todaysJob")
.startAt(new Date(row.get(field_Schd_Start_TS).longValue()))
.build();
trigger = trigger.getTriggerBuilder().withSchedule((ScheduleBuilder) SimpleScheduleBuilder.repeatMinutelyForever(5)).build();
现在我通过以下代码安排工作
context.getScheduler().scheduleJob(jobDetail,trigger);
这里上下文是JobExecutionContext的对象,因为我正在从其他作业执行类中启动此作业。 getScheduler()方法将返回与执行主作业相同的调度程序实例
问题是,“ jobDetail”对象中的此作业仅在指定时间由“ .startAt(new Date(row.get(field_Schd_Start_TS)””指定)执行一次,然后每5个执行一次分钟。 我无法找出为什么它不会被重复执行。
nextFireTime在触发器中为null。为什么?我无法找到这个。 我在main方法中编写了一个小程序来调度作业,在这里我看到,当作业执行时,我们在该作业中获得JobExecutionContext对象,在这里我调试并看到它包含触发器的对象,该对象具有nextFireTime设置如预期的那样。 但是在我使用的那个代码的实际实现中不会发生。
答案 0 :(得分:0)
我有一些可以正常工作的示例代码(我使用了.repeatSecondlyForever,因为我想快速调试输出):
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import java.util.Calendar;
import java.util.Date;
public class SimpleTriggerExample {
public static void main(String[] args) throws SchedulerException {
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
JobDetail jobDetail = JobBuilder.newJob(PrintDateJob.class).withIdentity("goodjob", "mygroup").build();
SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("goodjob", "mygroup")
.startAt(new Date(Calendar.getInstance().getTimeInMillis() + 3000))
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5))
.build();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
try {
//wait to finish the job
Thread.sleep(200000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//shutdown scheduler gracefully
scheduler.shutdown(true);
}
}
“ PrintDateJob.class”在这里:
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import java.util.Date;
public class PrintDateJob implements Job {
public void execute(JobExecutionContext context)
throws JobExecutionException {
JobKey jobKey = context.getJobDetail().getKey();
System.out.println(jobKey+": "+ new Date());
}
}
pom.xml在这里:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>chocksaway</groupId>
<artifactId>001</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>
</project>
运行示例给出以下输出:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
mygroup.goodjob: Thu Jun 21 15:15:53 BST 2018
mygroup.goodjob: Thu Jun 21 15:15:58 BST 2018
mygroup.goodjob: Thu Jun 21 15:16:03 BST 2018
mygroup.goodjob: Thu Jun 21 15:16:08 BST 2018