Firing Quartz jobs manually

和自甴很熟 提交于 2019-12-02 19:28:14
Gaurav Tailor

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

  }
Minarul Haque

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).
  • user4706742

    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>
    

    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
        }
    
    }
    
    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!