Using Quartz.Net in Windows Services

Mon Jul 04 2011 by MarkP

I was recently tasked to schedule an application using Windows Services for the current project I am working on. After researching the options available to me I decided to implement this using an open source project called Quartz.Net.

As with many .NET open source projects this is a port from the popular Java based scheduler. The basic premise of Quartz is based around the concept of scheduling jobs, which are fired, based on triggers assigned to the job. One of the most noticable things, was the use of CRON Unix expressions to be able to express schedules. Cron Expressions are used to define schedules in a clear and concise manner, as one line of text. e.g. to define the schedule to run every hour in office hours (9am-5pm) the following notation would be used "0 0 9-17 MON-FRI ? *". Further examples and tutorials can be found at Quartz.net

Testing and deploying Windows services is always a bit of a challenge espcially if the underlying service requires database or file access. I always ensure that I have sufficient logging in place to be able trace what the issue is. In this scenario, where the job ran quite a long running process I used both EntLib 5.0 to log the errors and custom logging which detailed the stages of the process once completed.

Text logs were created for the actual scheduling of the job and XML logs were created when the actual jobs were run. These were required in the UI so the user would be able to view if a job was successful or not and also display any relevant errors.

Another requirement was allow the user to manage the scheduling throught the UI .This was basically a CRON expression builder based on inputs entered by the user. One consideration that has to be accounted for, is that if the scheduler is amended, the windows service would have to be restarted for the new schedule to be implemented. This can be undertaken in C# code my accessing the ServiceController object and passing in the service name (see example).

Quartz is a very useful tool when scheduling jobs under window services and gived you the programmer a lot of flexibility and full control, in relation to scheduling jobs, rather than having to rely on the Timer object or scheduling jobs using third party applications.

Tags: Quartz.Net   C#  

Comments

Required fields are marked with required

The form could not be submitted please check the errors and resend:
Post a Comment