Quartz Scheduler:如何将自定义对象作为 JobParameter 传递?

发布于 2024-11-30 10:09:04 字数 180 浏览 2 评论 0原文

我计划编写一个 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你的背包 2024-12-07 10:09:04

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

传递数据映射中的实例。设置作业时,使用如下所示的键将实例添加到映射中:

// Create job etc...
var MyClass _myInstance;
statusJob.JobDataMap.Put("myKey", _myInstance);
// Schedule job...

在作业的 Execute() 方法中检索实例,如下所示:

public void Execute(IJobExecutionContext context)
        {
            var dataMap = context.MergedJobDataMap;
            var myInstance = (MyClass)dataMap["myKey"];

或者

在设置时将实例添加到调度程序上下文 ,如下所示:

  ISchedulerFactory schedFact = new StdSchedulerFactory();
  _sched = schedFact.GetScheduler();
  _sched.Start();
  // Create job etc...
  var MyClass _myInstance;
  _sched.Context.Put("myKey", myInstance);
  // Schedule job...

检索作业的 Execute() 方法中的实例

public void Execute(IJobExecutionContext context)
        {
            var schedulerContext = context.Scheduler.Context;
            var myInstance = (MyClass)schedulerContext.Get("myKey");

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");
百变从容 2024-12-07 10:09:04

在多线程环境中,我在上面的 hillstuk 答案中得到了意想不到的结果。
这是我使用 Newtonsoft 的解决方案......享受

public void InitJob() {

    MyClass data = new MyClass {Foo = “Foo fighters”}; 

    /* a unique identifier for demonstration purposes.. Use your own concoction here. */
    int uniqueIdentifier = new Random().Next(int.MinValue, int.MaxValue); 

    IJobDetail newJob = JobBuilder.Create<MyAwesomeJob>()
    .UsingJobData("JobData", JsonConvert.SerializeObject(data))
    .WithIdentity($"job-{uniqueIdentifier}", "main")                
    .Build();

}

/* the execute method */
public class MyAwesomeJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {                   
        var jobData = JsonConvert.DeserializeObject<MyClass>(context.JobDetail.JobDataMap.GetString("JobData"));    
    }
}

/* for completeness */
public class MyClass {
    string Foo { get; set; } 
}

I was having unexpected results with hillstuk's answer above in multithreaded environments.
Here's my solution using Newtonsoft… Enjoy

public void InitJob() {

    MyClass data = new MyClass {Foo = “Foo fighters”}; 

    /* a unique identifier for demonstration purposes.. Use your own concoction here. */
    int uniqueIdentifier = new Random().Next(int.MinValue, int.MaxValue); 

    IJobDetail newJob = JobBuilder.Create<MyAwesomeJob>()
    .UsingJobData("JobData", JsonConvert.SerializeObject(data))
    .WithIdentity($"job-{uniqueIdentifier}", "main")                
    .Build();

}

/* the execute method */
public class MyAwesomeJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {                   
        var jobData = JsonConvert.DeserializeObject<MyClass>(context.JobDetail.JobDataMap.GetString("JobData"));    
    }
}

/* for completeness */
public class MyClass {
    string Foo { get; set; } 
}
烦人精 2024-12-07 10:09:04

当您安排作业时,您可以在 JobDetail 上设置 JobDataMap 对象并将其传递给您的调度程序,quartz.net 教程。该作业可以通过以下方式访问数据:

JobDataMap dataMap = context.JobDetail.JobDataMap;

但是,我更喜欢通过注入到作业中的存储库来访问我的作业配置。

When you schedule a job you can set a JobDataMap on the JobDetail object and pass this to your scheduler, there are some limitations described in the quartz.net tutorial. The job can access the data via:

JobDataMap dataMap = context.JobDetail.JobDataMap;

However I prefer to access my job configuration, via an repository injected into the job.

情绪 2024-12-07 10:09:04

您可以将实例/对象放入 IJobDetail 中。

 JobDataMap m = new JobDataMap();
  m.Put("Class1", new Class1(){name="xxx"});


  IJobDetail job = JobBuilder.Create<Job>()
            .WithIdentity("myJob", "group1")
            .UsingJobData(m)//class object
            .UsingJobData("name2", "Hello World!")
            .Build();

用法

  public void Execute(IJobExecutionContext context)
        {
 JobDataMap dataMap = context.JobDetail.JobDataMap;
            Class1 class1 = (Class1)dataMap.Get("Class1");
string x = class1.name;
}

You can put your instance/object in the IJobDetail.

 JobDataMap m = new JobDataMap();
  m.Put("Class1", new Class1(){name="xxx"});


  IJobDetail job = JobBuilder.Create<Job>()
            .WithIdentity("myJob", "group1")
            .UsingJobData(m)//class object
            .UsingJobData("name2", "Hello World!")
            .Build();

usage

  public void Execute(IJobExecutionContext context)
        {
 JobDataMap dataMap = context.JobDetail.JobDataMap;
            Class1 class1 = (Class1)dataMap.Get("Class1");
string x = class1.name;
}
烟若柳尘 2024-12-07 10:09:04

的方式传递了对象

JobDetail job1 = JobBuilder.newJob(JobAutomation.class)
                .usingJobData("path", path)
                .withIdentity("job2", "group2").build();

        CronTrigger trigger1 = TriggerBuilder.newTrigger()
                .withIdentity("cronTrigger2", "group2")
                .withSchedule(CronScheduleBuilder.cronSchedule("40 27 11 * * ?"))
                .build();

我通过以下代码行获取 jobdatamap

JobDataMap dataMap = context.getJobDetail().getJobDataMap();
String path =dataMap.getString("path");

I passed the object following way

JobDetail job1 = JobBuilder.newJob(JobAutomation.class)
                .usingJobData("path", path)
                .withIdentity("job2", "group2").build();

        CronTrigger trigger1 = TriggerBuilder.newTrigger()
                .withIdentity("cronTrigger2", "group2")
                .withSchedule(CronScheduleBuilder.cronSchedule("40 27 11 * * ?"))
                .build();

get jobdatamap by following lines of code

JobDataMap dataMap = context.getJobDetail().getJobDataMap();
String path =dataMap.getString("path");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文