我们在应用程序中配置了几个Quartz作业。在开发过程中,我们将石英调度程序置于待机状态 - 但是,我们有时希望手动启动作业(用于开发目的)。如果我调用fireTrigger,它会告诉我需要启动调度程序。但是,如果我启动调度程序,它还会立即安排所有其他作业,这不是我想要的(因为它们可能在我调试手动触发的作业时触发)。
当我启动调度程序时,我可以暂停所有触发器,但之后我必须处理失火指令等。
是否有一种简单的方法可以手动触发作业而无需处理暂停和失火(即即使调度程序处于待机状态也能正常工作的fireTrigger)?
答案 0 :(得分:21)
这是手动触发作业所需的循环:
scheduler = stdSchedulerFactory.getScheduler();
//note: "stdSchedulerFactory" is the object created of
//the schedulerFactory(Standard) class.
// loop jobs by group
for (String groupName : scheduler.getJobGroupNames()) {
// get jobkey
for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher
.jobGroupEquals(groupName))) {
String jobName = jobKey.getName();
String jobGroup = jobKey.getGroup();
scheduler.triggerJob(jobName, jobGroup);
}
}
答案 1 :(得分:3)
在Quartz Scheduler中注册的所有作业都由JobKey唯一标识,JobKey由名称和组组成。您可以通过调用Scheduler实例的triggerJob(JobKey jobKey)
来立即触发具有给定JobKey的作业。
//Create a new Job
JobKey jobKey = JobKey.jobKey("myNewJob", "myJobGroup");
JobDetail job =JobBuilder.newJob(MyJob.class).withIdentity(jobKey).storeDurably().build();
//Register this job to the scheduler
scheduler.addJob(job, true);
//Immediately fire the Job MyJob.class
scheduler.triggerJob(jobKey);
注意:强>
triggerJob(JobKey jobKey)
以编程方式触发。
答案 2 :(得分:1)
无需start-time
和end-time
。
<trigger>
<cron>
<name>TestTrigger</name>
<group>CronSampleTrigger</group>
<description>CronSampleTrigger</description>
<job-name>TestJob</job-name>
<job-group>jobGroup1</job-group>
<!--<start-time>1982-06-28T18:15:00.0Z</start-time>
<end-time>2020-05-04T18:13:51.0Z</end-time>-->
<cron-expression>0 0/1 * * * ?</cron-expression>
</cron>
</trigger>
答案 3 :(得分:0)
您可以尝试在计划程序中添加触发器过滤器
this.scheduler.addGlobalTriggerListener(new DebugExecutionFilter());
当执行不是易失性(未安排立即运行)并且您处于调试模式时,调试执行过滤器将添加否决权。
这是一个实现示例:
private static class DebugExecutionFilter implements TriggerListener
{
public DebugExecutionFilter()
{
}
@Override
public String getName()
{
return "Task execution filter";
}
@Override
public void triggerFired(Trigger trigger, JobExecutionContext context)
{
// Do nothing
}
/* (non-Javadoc)
*
* @see org.quartz.TriggerListener#vetoJobExecution(org.quartz.Trigger, org.quartz.JobExecutionContext) */
@Override
@SuppressWarnings("unchecked")
/**
* A veto is added if :
* - For non volatile trigger if we are in debug mode
*/
public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context)
{
try
{
//
if ( !trigger.isVolatile() && isDebugMode() )
{
return true;
}
//task is run by scheduler.triggerJobWithVolatileTrigger() for immediate schedule
//or task is schedule and we are not in debugMode
return false;
}
@Override
public void triggerMisfired(Trigger trigger)
{
// do nothing
}
@Override
public void triggerComplete(Trigger trigger, JobExecutionContext context, int triggerInstructionCode)
{
// do nothing
}
}