Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz Scheduler: How to pass custom objects as JobParameter?

I am planning to write a ASP.NET page to trigger the job on demand. Currently, I am using SimpleTrigger class to trigger the job but none of the __Trigger class supports object type as value in JobParameters and it has come to my knowledge that WCF Tcp binding is used under the hook to pass the parameters to job scheduling engine. I would like to know how to pass custom object (serializable) as job parameters. Thanks for your advice!

like image 495
Thurein Avatar asked Aug 21 '11 11:08

Thurein


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.

Why we should not use Quartz Scheduler?

It has no built-in UI for configuration or monitoring, no useful and reasonably searchable logging or historical review, no support for multiple execution nodes, no administration interface, no alerts or notifications, not to mention buggy recovery mechanisms for failed jobs and missed jobs.

How does Quartz Scheduler work internally?

Quartz scheduler allows an enterprise to schedule a job at a specified date and time. It allows us to perform the operations to schedule or unschedule the jobs. It provides operations to start or stop or pause the scheduler. It also provides reminder services.

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>() .


2 Answers

There are two ways to pass an object that can be retrieved when a Quartz job executes:

Pass the instance in the data map. When you set the job up, add your instance to the map with a key like this:

// Create job etc...
var MyClass _myInstance;
statusJob.JobDataMap.Put("myKey", _myInstance);
// Schedule job...

Retrieve the instance in the job's Execute() method like this:

public void Execute(IJobExecutionContext context)
        {
            var dataMap = context.MergedJobDataMap;
            var myInstance = (MyClass)dataMap["myKey"];

OR

Add the instance to the scheduler context when you set the job up, like this:

  ISchedulerFactory schedFact = new StdSchedulerFactory();
  _sched = schedFact.GetScheduler();
  _sched.Start();
  // Create job etc...
  var MyClass _myInstance;
  _sched.Context.Put("myKey", myInstance);
  // Schedule job...

Retrieve the instance in the job's Execute() method like this:

public void Execute(IJobExecutionContext context)
        {
            var schedulerContext = context.Scheduler.Context;
            var myInstance = (MyClass)schedulerContext.Get("myKey");
like image 196
hillstuk Avatar answered Sep 18 '22 17:09

hillstuk


When you schedule a job you can set a JobDataMap on the JobDetail object and pass this to your scheduler, there are some limitations described in the quartz.net tutorial. The job can access the data via:

JobDataMap dataMap = context.JobDetail.JobDataMap;

However I prefer to access my job configuration, via an repository injected into the job.

like image 27
Andreas Avatar answered Sep 16 '22 17:09

Andreas