Schedule multiple jobs in Quartz.Net
Asked Answered
M

5

12

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.

Mikol answered 3/2, 2014 at 12:47 Comment(0)
R
12

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"));
    }
    }
}
Rework answered 4/2, 2014 at 10:50 Comment(6)
Can you tell me another thing. Suppose I have two jobs running at same intervals. Then can I pause any one job for sometime and again let it run?Mikol
Glad I've helped. I've updated my answer with more info. Have a look at PauseJob and ResumeJob. Cheers.Rework
Thanks for your quick response. But the above code is not fulfilling my requirement. I have a scenario where I have suppose two jobs running at similar intervals let it be 30 seconds. Then suppose a job is paused suddenly at 15 seconds for certain reason while the other job is continued to run. Now I want that after resuming, the first job should start at 16 seconds i.e., where it was left. Will Misfire help?Mikol
what is the meaning of this "0 * 8-22 * * ?" and "0 0/2 * 1/1 * ? *"Neve
@Mou: That is a cron expressoin. You can check the result of that expression here.Rework
How can? Firstly, I want to add jobs to scheduler. Secondly, I only want to add one trigger. And finally, I run all jobs with one trigger.Unquiet
B
8

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://mcmap.net/q/412691/-calling-schedule-with-schedulejobs

Bootlace answered 16/11, 2015 at 16:55 Comment(0)
O
1

What you want to accomplish is very simple:

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);
Oblation answered 3/2, 2014 at 16:4 Comment(1)
It's not working.I am getting exception for the second job as "trigger doesn't reference given job".Mikol
U
1

if you want to add jobs and triggers dynamically you can try the following code.

` using Quartz;

public Task DoSomething(IScheduler schedule, CancellationToken ct)
{
    var job = JobBuilder.Create<StartTripJob>()
                        .WithIdentity("name", "group")
                        .Build();

    var trigger = TriggerBuilder.Create()
        .WithIdentity("name", "group")
        .StartNow()
        .Build();

    await scheduler.ScheduleJob(job, trigger, ct);
}

`

Unpaidfor answered 25/11, 2022 at 9:50 Comment(0)
C
0

For Quartz.AspNetCore with AspNetCoreWebApi projects, in Program.cs add this:

using Quartz;
...
builder.Services.AddQuartz(q =>
{
    var jobKey1 = new JobKey("ContractUpdaterJob");
    q.AddJob<ContractUpdaterJob>(opts => opts.WithIdentity(jobKey1));
    q.AddTrigger(opts => opts
        .ForJob(jobKey1)
        .WithIdentity("ContractUpdaterJob-trigger")
        .WithCronSchedule("0 0 12-19 * * ?")
    );

    var jobKey2 = new JobKey("SpaceMaintenanceJob");
    q.AddJob<SpaceMaintenanceJob>(opts => opts.WithIdentity(jobKey2));
    q.AddTrigger(opts => opts
        .ForJob(jobKey2)
        .WithIdentity("SpaceMaintenanceJob-trigger")
        .WithCronSchedule("0 0 10 * * ?")
    );

});
Cenis answered 19/2 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.