Firing Quartz jobs manually

2020-05-17 16:45发布

We have several Quartz jobs configured in our application. During development, we leave the quartz scheduler in standby - however, we sometimes want to start a job manually (for development purposes). If I call fireTrigger, it tells me I need to start the scheduler. However, if I start the scheduler, it will also immediately schedule all the other jobs, which is not what I want (since they may trigger while I'm debugging the manually fired job).

I could pause all triggers when I start the scheduler, but then I have to deal with misfire instructions etc.

Is there a simple way to fire off a job manually without having to deal with pausing and misfires (i.e. a fireTrigger which works even if the scheduler is in standby)?

4条回答
Summer. ? 凉城
2楼-- · 2020-05-17 17:18

this is the loop you will require to fire the job manually:

  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);
    }

  }
查看更多
▲ chillily
3楼-- · 2020-05-17 17:21

No need for start-time and 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>
查看更多
▲ chillily
4楼-- · 2020-05-17 17:26

All the Jobs registered in the Quartz Scheduler are uniquely identified by the JobKey which is composed of a name and group . You can fire the job which has a given JobKey immediately by calling triggerJob(JobKey jobKey) of your Scheduler instance.

    //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);

Note:

  • scheduler is the Scheduler instance used throughout your application . Its start() method should be already called after it is created.
  • The job is the durable job which cannot attach any triggers or cron to it .It can only be fired programmatically by calling triggerJob(JobKey jobKey).
  • 查看更多
    疯言疯语
    5楼-- · 2020-05-17 17:26

    You can try to add a trigger filter in your scheduler

    this.scheduler.addGlobalTriggerListener(new DebugExecutionFilter());
    

    The debug execution filter will add a veto when the execution is not volatile (not scheduled to run immediately) and you are in debug mode .

    Here is an implementation example :

    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
        }
    
    }
    
    查看更多
    登录 后发表回答