How to setup Quartz.NET for scheduling Emails

There are two solutions Quartz.server.2008.sln and quartz.2008.sln in the downloaded quartz.NET library. Now I have to setup recurring job. There would be table in the database where all schedules will be defined (like first friday of the month, each 5 in month, each sunday 12 am…etc). I have the method executed on schedule. Now how can i use quartz.net to get schedule from database and set the method to execute? what are the proper steps?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

I think you can create a windows service running in background. You can read the scheduleFromDatabase varaible from database and then pass it to Quartz.

This is a small example from a console app:

    static void Main(string[] args)
    {
        ISchedulerFactory schedFact = new StdSchedulerFactory();

        IScheduler sched = schedFact.GetScheduler();
        sched.Start();

        JobDetail jobDetail = new JobDetail("myJob", null, typeof(HelloJob));

        //read this string from database
        string scheduleFromDatabase="0 11 16 ? * FRI,SUN";

        CronTrigger trigger = new CronTrigger("trigger1", null, "myJob",
                                                null,scheduleFromDatabase );

        trigger.StartTimeUtc = DateTime.UtcNow;
        trigger.Name = "myTrigger";
        sched.ScheduleJob(jobDetail, trigger); 
    }

public class HelloJob:IJob
{

    public void Execute(JobExecutionContext context)
    {
        Console.WriteLine(DateTime.Now.ToString());
        //Call here your method!
    }
}

This can be useful:

Quartz.NET server documentation

Solution 2

The quartz documentation does seem to assume that everyone knows the basics and is only looking for details.

Hopefully the steps below, along with the Quartz documentation and samples, you’ll be able to get your project started.

Step 1: Open Windows Explorer to the …\Quartz.NET-1.0.3\database\tables folder
Step 2: Execute the script appropriate for your database
Step 3: Open Windows Explorer to the …\Quartz.NET-1.0.3\server\bin\3.5\console folder
Step 4: Create a class library assembly and add a class that implements the IJob interface.
Step 5: Edit the quartz.config file. Mine is below.

  ################################################################################
  # Added by Brad Bruce
  # please refer to http://quartznet.sourceforge.net/tutorial/lesson_9.html before making changes
  ################################################################################
  quartz.jobStore.type = Quartz.Impl.AdoJobStore.JobStoreTX, Quartz
  quartz.jobStore.driverDelegateType = Quartz.Impl.AdoJobStore.OracleDelegate, Quartz
  quartz.jobStore.tablePrefix = QRTZ_
  quartz.jobStore.dataSource = myDS
  quartz.dataSource.myDS.connectionString = Data Source=xe; User Id=quartz; Password=quartz;
  quartz.dataSource.myDS.provider = OracleODP-20
  quartz.jobStore.useProperties = true

Step 6: Run the console server and verify that the dummy job is running
Step 7: Copy the console project to a new project
Step 8: Modify the console source to schedule the job via the Quartz API. You will be able to reuse this project to schedule other jobs.

If they would only add a project to schedule and manage the jobs, I think Quartz.Net would really take off.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply