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?
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.
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 .
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); } }
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With