Quartz Scheduler: How to pass custom objects as JobParameter?

后端 未结 5 1765
时光说笑
时光说笑 2020-12-06 16:28

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 obj

5条回答
  •  旧巷少年郎
    2020-12-06 17:12

    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");
    

提交回复
热议问题