How to create a link to an instance of an object in a Quartz.Net job?

I have a Windows service with built-in Quartz.Net, but cannot find a way to create a link to an instance of the object in the Quartz.Net job ...

When the Windows service starts, it starts some objects for logging, database access, and other purposes, so I would like my Quartz.Net jobs to use these already created objects instead of creating their own instances of these objects. However, Quartz.Net jobs are created by the scheduler using a constructor with no arguments, and therefore there is no way to pass a link using the constructor.

Should I create my own JobFactory implementation and is this the only way to achieve this?

+6
source share
3 answers

Different context (Linux / JAVA), but create your own Factory, which inherits from quartz. Override the createScheduler method. Call the super method, save the instance in a static (synchronized) hash map. Write a static method to get an instance by name.

+1
source share

I think the approach that works for this situation is to use a job listener . You can create a dummy task that does nothing, and a listener that detects when the task was started. You can create an instance of the listener with links to any dependencies if they are available while you are setting up job scheduling.

IJobDetail job = JobBuilder.Create<DummyJob>() .WithIdentity("job1") .Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1") .StartNow() .WithSimpleSchedule(x => x .WithInterval(interval) .RepeatForever()) .Build(); _scheduler.ScheduleJob(job, trigger); MyJobListener myJobListener = new MyJobListener (dependency1, dependency2); _scheduler.ListenerManager.AddJobListener(myJobListener, KeyMatcher<JobKey>.KeyEquals(new JobKey("job1"))); 
+3
source share

You can add key-value object pairs to jobDetail.JobDataMap and get them from(JobExecutionContext) context.JobDetail.JobDataMap .

+2
source share

All Articles