Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass instance variables into Quartz job?

I wonder how to pass an instance variable externally in Quartz?

Below is pseudo code I would like to write. How can I pass externalInstance into this Job?

public class SimpleJob implements Job {         @Override         public void execute(JobExecutionContext context)                 throws JobExecutionException {              float avg = externalInstance.calculateAvg();         } } 
like image 343
janetsmith Avatar asked Oct 08 '12 07:10

janetsmith


People also ask

How do you pass parameters to Quartz job?

Start up the Quartz Scheduler. Schedule two jobs, each job will execute the every ten seconds for a total of times. The scheduler will pass a run-time job parameter of “Green” to the first job instance. The scheduler will pass a run-time job parameter of “Red” to the second job instance.

What is JobDataMap in quartz?

Holds state information for Job instances. JobDataMap instances are stored once when the Job is added to a scheduler. They are also re-persisted after every execution of StatefulJob instances. JobDataMap instances can also be stored with a Trigger .

How do I Unschedule a quartz job?

We can unschedule a Job by calling the unschedule() method of the Scheduler class and passing the TriggerKey . If the related job does not have any other triggers, and the job is not durable, then the job will also be deleted.

How do you schedule multiple jobs using quartz?

If you want to schedule multiple jobs in your console application you can simply call Scheduler. ScheduleJob (IScheduler) passing the job and the trigger you've previously created: IJobDetail firstJob = JobBuilder. Create<FirstJob>() .


1 Answers

you can put your instance in the schedulerContext.When you are going to schedule the job ,just before that you can do below:

getScheduler().getContext().put("externalInstance", externalInstance); 

Your job class would be like below:

public class SimpleJob implements Job {     @Override     public void execute(JobExecutionContext context)             throws JobExecutionException {         SchedulerContext schedulerContext = null;         try {             schedulerContext = context.getScheduler().getContext();         } catch (SchedulerException e1) {             e1.printStackTrace();         }         ExternalInstance externalInstance =             (ExternalInstance) schedulerContext.get("externalInstance");          float avg = externalInstance.calculateAvg();     } } 

If you are using Spring ,you can actually using spring's support to inject the whole applicationContext like answered in the Link

like image 162
Rips Avatar answered Oct 08 '22 23:10

Rips