Installing a Quartz.NET trigger to launch an instance of a specific object

How can I get Quartz to fire a trigger on an already created and initialized object?

eg.

public class Foo : IJob
{
    public Foo ( configuration items ... ) { }
}

// Calling Code...
Foo f = new Foo( /*Non empty constructor*/ );
Schedular sched = new SchedulerFactory().GetScheduler();
JobDetail jD = new JobDetail("fooDetail", null typeof(Foo));
Trigger trig = TriggerUtils.MakeSecondlyTrigger(15);
sched.ScheduleJob( jD, trig );
sched.Start();

Since foo does not have a 0 args constructor, Quartz.NET creates problems by instantiating the task and starting it. Is there a way to get Quartz to call an instance of Foo, f?

Please forgive me if I miss the fundamental fact about the quartet and its use.

+3
source share
3 answers

The Quartz.NET documentation says: (In a very small entry!)

( ) , , Execute (..). , .

, - -!

B:

"". .JobDetail.JobDataMap, , . Execute :

public void Execute( JobExecutionContext context )
{
    JobDataMap dataMap = context.JobDetail.JobDataMap;

    // Execute the stored Task
    Foo taskToExecute = dataMap["Task"] as Foo;
    if ( taskToExecute != null )
    {
        taskToExecute.Execute();
    }           
}

, , , .

JobDetail detail = new JobDetail( "foo", null, typeof( MasterTask) );
detail.JobDataMap["Task"] = task;

, Quartz, , , !

0

Quartz.Spi.IJobFactory ( DI), , . .

JobFactory, unity . , .

public class UnityJobFactory : IJobFactory {

    public UnityJobFactory(IUnityContainer container) {
        Container = container;
    }

    public IUnityContainer Container { get; private set; }

    public IJob NewJob(TriggerFiredBundle bundle) {
        try {
            return Container.Resolve(bundle.JobDetail.JobType, null) as IJob;
        }
        catch (Exception exception) {
            throw new SchedulerException("Error on creation of new job", exception);
        }
    }
}
+3

, IJob, , IJobListener .

var myJobListener = new YourClassImplementingIJobListener(param1, param2);
sched.ListenerManager.AddJobListener(myJobListener,GroupMatcher<JobKey>.GroupEquals("myJobGroup"));

http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/trigger-and-job-listeners.html

0

Source: https://habr.com/ru/post/1794649/


All Articles