我需要一些帮助。我正在尝试弄清楚如何在 Quartz.Net 中安排工作。 Quartz 中的作业对应于我的 Web 应用程序中的任务,它们都是我的 Web 应用程序中作业的一部分。我希望用户能够按需启动作业(Web应用程序上下文)并使其立即运行或安排将来的作业,并且可能在给定的时间间隔内重复。我知道所有这些项目是如何在 Quartz 中单独完成的,但我很难将它们放在一起。
例如,在我的 Web 应用程序中,我可能有一项包含多个任务的工作,并且按特定顺序进行。我希望能够在quartz中安排这些任务,以便它们按照我的Web应用程序中确定的相同顺序执行。有人知道如何做到这一点吗?我读过 Quartz 文档,说要将下一个作业存储在 JobDataMap 中,只是在努力解决这个问题。
我目前正在等待创建 Quartz 作业,直到用户请求安排作业或运行它。您认为我应该创建作业并在 Web 应用程序中创建任务时触发,然后从任务对象中提取该信息以在 Quartz 中进行调度吗?
你需要的是JobChainingJobListener
类,它可以帮助您按照您想要的特定顺序为您的作业创建执行链。
using System;
using System.Text;
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Calendar;
using Quartz.Listener;
using Quartz.Impl.Matchers;
using System.Threading;
namespace QuartzNET.Samples
{
class Program
{
static void Main(string[] args)
{
// Create RAMJobStore instance
DirectSchedulerFactory.Instance.CreateVolatileScheduler(5);
ISchedulerFactory factory = DirectSchedulerFactory.Instance;
// Get scheduler and add object
IScheduler scheduler = factory.GetScheduler();
StringBuilder history = new StringBuilder("Runtime History: ");
scheduler.Context.Add("History", history);
JobKey firstJobKey = JobKey.Create("FirstJob", "Pipeline");
JobKey secondJobKey = JobKey.Create("SecondJob", "Pipeline");
JobKey thirdJobKey = JobKey.Create("ThirdJob", "Pipeline");
// Create job and trigger
IJobDetail firstJob = JobBuilder.Create<FirstJob>()
.WithIdentity(firstJobKey)
//.StoreDurably(true)
.Build();
IJobDetail secondJob = JobBuilder.Create<SecondJob>()
.WithIdentity(secondJobKey)
.StoreDurably(true)
.Build();
IJobDetail thirdJob = JobBuilder.Create<ThirdJob>()
.WithIdentity(thirdJobKey)
.StoreDurably(true)
.Build();
ITrigger firstJobTrigger = TriggerBuilder.Create()
.WithIdentity("Trigger", "Pipeline")
.WithSimpleSchedule(x => x
.WithMisfireHandlingInstructionFireNow()
.WithIntervalInSeconds(5)
.RepeatForever())
.Build();
JobChainingJobListener listener = new JobChainingJobListener("Pipeline Chain");
listener.AddJobChainLink(firstJobKey, secondJobKey);
listener.AddJobChainLink(secondJobKey, thirdJobKey);
scheduler.ListenerManager.AddJobListener(listener, GroupMatcher<JobKey>.GroupEquals("Pipeline"));
// Run it all in chain
scheduler.Start();
scheduler.ScheduleJob(firstJob, firstJobTrigger);
scheduler.AddJob(secondJob, false, true);
scheduler.AddJob(thirdJob, false, true);
Console.ReadLine();
scheduler.Shutdown();
Console.WriteLine("Scheduler shutdown.");
Console.WriteLine(history);
Console.ReadLine();
}
}
class FirstJob : IJob
{
public void Execute(IJobExecutionContext context)
{
var history = context.Scheduler.Context["History"] as StringBuilder;
history.AppendLine();
history.AppendFormat("First {0}", DateTime.Now);
Console.WriteLine("First {0}", DateTime.Now);
}
}
class SecondJob : IJob
{
public void Execute(IJobExecutionContext context)
{
var history = context.Scheduler.Context["History"] as StringBuilder;
history.AppendLine();
history.AppendFormat("Second {0}", DateTime.Now);
Console.WriteLine("Second {0}", DateTime.Now);
}
}
class ThirdJob : IJob
{
public void Execute(IJobExecutionContext context)
{
var history = context.Scheduler.Context["History"] as StringBuilder;
history.AppendLine();
history.AppendFormat("Third {0}", DateTime.Now);
Console.WriteLine("Third {0}", DateTime.Now);
Console.WriteLine();
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)