在Quartz.Net中安排多个工作

时间:2014-02-03 12:47:05

标签: .net quartz.net

我是Quartz.Net的初学者。 如何在调度程序中添加多个作业?

为了学习我正在使用控制台应用程序。

3 个答案:

答案 0 :(得分:9)

如果您是Quartz.Net的新手,我建议您从Jay Vilalta的Blog和旧的one开始,在那里您可以找到有关Quartz.Net的大量教程和有用的信息。

如果您想在控制台应用程序中安排多个作业,只需拨打Scheduler.ScheduleJob IScheduler )即可传递作业和您之前创建的触发器:

IJobDetail firstJob = JobBuilder.Create<FirstJob>()
               .WithIdentity("firstJob")
               .Build();

ITrigger firstTrigger = TriggerBuilder.Create()
                 .WithIdentity("firstTrigger")
                 .StartNow()
                 .WithCronSchedule("0 * 8-22 * * ?")
                 .Build();


IJobDetail secondJob = JobBuilder.Create<SecondJob>()
               .WithIdentity("secondJob")
               .Build();

ITrigger secondTrigger = TriggerBuilder.Create()
                 .WithIdentity("secondTrigger")
                 .StartNow()
                 .WithCronSchedule("0 0/2 * 1/1 * ? *")
                 .Build();

Scheduler.ScheduleJob(firstJob, firstTrigger);
Scheduler.ScheduleJob(secondJob, secondTrigger);

您可以下载工作示例here

更新:

如果您想暂停和/或重新开始工作,可以使用PauseJobResumeJob(对PauseTriggerResumeTrigger的触发器也可以这样做。

这是一个示例:

private static void Test001(IScheduler Scheduler)
{
    IJobDetail firstJob = JobBuilder.Create<FirstJob>()
                   .WithIdentity("firstJob")
                   .Build();

    ITrigger firstTrigger = TriggerBuilder.Create()
                     .WithIdentity("firstTrigger")
                     .StartNow()
                     .WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever())
                     .Build();


    IJobDetail secondJob = JobBuilder.Create<SecondJob>()
                   .WithIdentity("secondJob")
                   .Build();

    ITrigger secondTrigger = TriggerBuilder.Create()
                     .WithIdentity("secondTrigger")
                     .StartNow()
                     .WithSimpleSchedule(x=>x.WithIntervalInSeconds(1).RepeatForever())
                     .Build();


    Scheduler.ScheduleJob(firstJob, firstTrigger);
    Scheduler.ScheduleJob(secondJob, secondTrigger);

    for (int i = 0; i < 300; i++)
    {
    System.Threading.Thread.Sleep(100);
    if (i == 100)
    {
        Scheduler.PauseJob(new JobKey("firstJob"));
    }
    else if (i == 200)
    {
        Scheduler.ResumeJob(new JobKey("firstJob"));
    }
    }
}

答案 1 :(得分:6)

萨拉姆。

我使用这个解决方案:

IJobDetail jobDetail = JobBuilder
    .Create<ReportJob>()
    .WithIdentity("theJob")
    .Build();

ITrigger everydayTrigger = TriggerBuilder
    .Create()
    .WithIdentity("everydayTrigger")
    // fires 
    .WithCronSchedule("0 0 12 1/1 * ?")
    // start immediately
    .StartAt(DateBuilder.DateOf(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year))
    .Build();
ITrigger yearlyTrigger = TriggerBuilder.Create()
    .WithIdentity("yearlyTrigger")
    // fires 
    .WithCronSchedule("0 0 12 1 1 ? *")
    // start immediately
    .StartAt(DateBuilder.DateOf(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year))
    .Build();

var dictionary = new Dictionary<IJobDetail, Quartz.Collection.ISet<ITrigger>>();
dictionary.Add(jobDetail, new Quartz.Collection.HashSet<ITrigger>()
                          {
                              everydayTrigger,
                              yearlyTrigger
                          });
sched.ScheduleJobs(dictionary, true);

来自:https://stackoverflow.com/a/20419575/1676736

答案 2 :(得分:1)

您想要完成的事情非常简单:

ISchedulerFactory schFactory = new StdSchedulerFactory();
IScheduler sch = schFactory.GetScheduler();

sch.Start();

//Repeat the code below for as many jobs you'd like
//creating jobs and triggers for them. 
//If they fire at the same time, just one ITrigger is needed .....
IJobDetail job = JobBuilder.Create<HelloJob>()
    .WithIdentity("myJob", null)
    .Build();

ITrigger trigger = TriggerBuilder
                 .Create()
                 .WithSchedule(SimpleScheduleBuilder.RepeatMinutelyForever())
                 .ForJob(job)
                 .WithIdentity(job.Key.Name + "Trigger")
                 .Build();

sch.AddJob(trigger);