Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send argument to class in Quartz.Net

Tags:

I'm using Quartz.Net (version 2) for running a method in a class every day at 8:00 and 20:00 (IntervalInHours = 12)

Everything is OK since I used the same job and triggers as the tutorials on Quartz.Net, but I need to pass some arguments in the class and run the method bases on those arguments.

Can any one help me how I can use arguments while using Quartz.Net?

like image 502
Hosein Avatar asked Dec 13 '12 09:12

Hosein


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 .


2 Answers

You can use JobDataMap

jobDetail.JobDataMap["jobSays"] = "Hello World!"; jobDetail.JobDataMap["myFloatValue"] =  3.141f; jobDetail.JobDataMap["myStateData"] = new ArrayList();   public class DumbJob : IJob {     public void Execute(JobExecutionContext context)     {         string instName = context.JobDetail.Name;         string instGroup = context.JobDetail.Group;          JobDataMap dataMap = context.JobDetail.JobDataMap;          string jobSays = dataMap.GetString("jobSays");         float myFloatValue = dataMap.GetFloat("myFloatValue");         ArrayList state = (ArrayList) dataMap["myStateData"];         state.Add(DateTime.UtcNow);          Console.WriteLine("Instance {0} of DumbJob says: {1}", instName, jobSays);     } }  
like image 190
Arsen Mkrtchyan Avatar answered Sep 28 '22 13:09

Arsen Mkrtchyan


To expand on @ArsenMkrt's answer, if you're doing the 2.x-style fluent job config, you could load up the JobDataMap like this:

var job = JobBuilder.Create<MyJob>()     .WithIdentity("job name")     .UsingJobData("x", x)     .UsingJobData("y", y)     .Build(); 
like image 20
Todd Menier Avatar answered Sep 28 '22 11:09

Todd Menier