Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 949
Swastik Avatar asked Feb 03 '14 12:02

Swastik


People also ask

How do you schedule multiple jobs using Quartz?

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

How many jobs can Quartz handle?

The actual number of jobs that can be running at any moment in time is limited by the size of the thread pool. If there are five threads in the pool, no more than five jobs can run at a time.

How does Quartz scheduling work?

Quartz scheduler allows an enterprise to schedule a job at a specified date and time. It allows us to perform the operations to schedule or unschedule the jobs. It provides operations to start or stop or pause the scheduler. It also provides reminder services.


2 Answers

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"));
    }
    }
}
like image 193
LeftyX Avatar answered Sep 30 '22 18:09

LeftyX


I use this solution:

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

from: https://stackoverflow.com/a/20419575/1676736

like image 33
Sayed Abolfazl Fatemi Avatar answered Sep 30 '22 18:09

Sayed Abolfazl Fatemi