Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
847 views
in Technique[技术] by (71.8m points)

.net - Schedule multiple jobs in Quartz.Net

I am a beginner in Quartz.Net. How can I add multiple jobs in a scheduler?

For the sake of learning I am using Console Application.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you're new to Quartz.Net I would suggest you to start with Jay Vilalta's Blog and the old one where you can find loads of tutorials and useful infos about Quartz.Net.

If you want to schedule multiple jobs in your console application you can simply call Scheduler.ScheduleJob (IScheduler) passing the job and the trigger you've previously created:

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

You can download a working example here.

UPDATE:

If you want to pause and/or restart a job you can use PauseJob and ResumeJob (you can do the same for a trigger with PauseTrigger and ResumeTrigger).

This is a sample:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...