Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz.net: Run a job for specific interval of time

Tags:

c#

quartz.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;
using Quartz.Job;
using ConsoleApplication2;

namespace Lesson1
{
    class Program
    {
        static void Main(string[] args)
       {
            //Create the scheduler factory
            ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

            //Ask the scheduler factory for a scheduler
            IScheduler scheduler = schedulerFactory.GetScheduler();

            //Start the scheduler so that it can start executing jobs
            scheduler.Start();

            // Create a job of Type WriteToConsoleJob
            IJobDetail job = JobBuilder.Create(typeof(WriteToConsoleJob)).Build();

            ITrigger trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule(s => s.WithIntervalInMinutes(15).OnMondayThroughFriday().StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(9, 0))).Build();


            scheduler.ScheduleJob(job, trigger);


            //A nice way to stop the scheduler, waiting for jobs that are running to finish
            scheduler.Shutdown(true);
        }
    }
}

I have created a test job and its working fine for weekdays repeating after 15 minutes starting at 0900 hours but i want to run it for specific interval of time i.e. 0900 to 1500 hours. And i don't want to use CronTrigger for this.

like image 944
AF. Avatar asked Jan 28 '15 06:01

AF.


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

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 do I Unschedule a Quartz job?

We can unschedule a Job by calling the unschedule() method of the Scheduler class and passing the TriggerKey . If the related job does not have any other triggers, and the job is not durable, then the job will also be deleted.


1 Answers

Add an EndingDailyAt call:

ITrigger trigger = TriggerBuilder
            .Create()
            .WithDailyTimeIntervalSchedule(s =>  
                         s.WithIntervalInMinutes(15)
                          .OnMondayThroughFriday()
                          .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(9, 0))
                          .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(15, 0)))
           .Build();
like image 182
stuartd Avatar answered Sep 21 '22 18:09

stuartd