Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firing Quartz jobs manually

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)?

like image 695
roger armstrong Avatar asked Aug 12 '11 11:08

roger armstrong


3 Answers

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

SchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = stdSchedulerFactory.getScheduler();

// 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);
    }
}
like image 158
Gaurav Tailor Avatar answered Sep 29 '22 18:09

Gaurav Tailor


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)`.
  • like image 45
    Minarul Haque Avatar answered Sep 29 '22 18:09

    Minarul Haque


    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
        }
    
    }
    
    like image 33
    mmounirou Avatar answered Sep 29 '22 18:09

    mmounirou