Quartz Scheduler:如何将自定义对象作为JobParameter传递? [英] Quartz Scheduler: How to pass custom objects as JobParameter?

查看:174
本文介绍了Quartz Scheduler:如何将自定义对象作为JobParameter传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算编写一个ASP.NET页面以按需触发作业.当前,我正在使用SimpleTrigger类来触发作业,但是__Trigger类都不支持对象类型作为JobParameters中的值,据我所知,在挂钩下使用WCF Tcp绑定将参数传递给作业调度引擎.我想知道如何将自定义对象(可序列化)作为作业参数传递. 感谢您的建议!

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 object type as value in JobParameters and it has come to my knowledge that WCF Tcp binding is used under the hook to pass the parameters to job scheduling engine. I would like to know how to pass custom object (serializable) as job parameters. Thanks for your advice!

推荐答案

有两种方法可以传递在执行Quartz作业时可以检索的对象:

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...

像这样在作业的Execute()方法中检索实例:

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...

像这样在作业的Execute()方法中检索实例:

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

这篇关于Quartz Scheduler:如何将自定义对象作为JobParameter传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆