Phillip Schmidt
Phillip Schmidt

Reputation: 8818

Scheduling a windows service with quartz .NET

I have a custom windows service, and I'd like to use Quartz .NET to schedule when the service runs. Now, I understand the basics of quartz.NET, but I'm not sure how I would hook it up to a windows service.. So, lets say i have Service.exe which I want to run every hour. How would I implement this functionality via Quartz? I know this is kind of a vague question, but there's really no other way to ask it.

Thanks in advance.

Upvotes: 3

Views: 14720

Answers (1)

Steve Sloka
Steve Sloka

Reputation: 3464

You need to setup a job and a trigger. The job is called by a trigger.(http://quartznet.sourceforge.net/tutorial/lesson_3.html). Here's an example running every hour.

// construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();

// get a scheduler
IScheduler sched = schedFact.GetScheduler();
sched.Start();

// construct job info
JobDetail jobDetail = new JobDetail("myJob", null, typeof(DumbJob));
// fire every hour
Trigger trigger = TriggerUtils.MakeHourlyTrigger();
// start on the next even hour
trigger.StartTime = TriggerUtils.GetEvenHourDate(DateTime.UtcNow);  
trigger.Name = "myTrigger";
sched.ScheduleJob(jobDetail, trigger);

Here is your class which calls Service.exe.

public class DumbJob : IJob
{
    public void Execute(JobExecutionContext context)
    {
        string instName = context.JobDetail.Name;
        string instGroup = context.JobDetail.Group;

        // Note the difference from the previous example
        JobDataMap dataMap = context.MergedJobDataMap;

        string jobSays = dataMap.GetString("jobSays");
        float myFloatValue = dataMap.GetFloat("myFloatValue");
        ArrayList state = (ArrayList) dataMap.Get("myStateData");
        state.Add(DateTime.UtcNow);

        Console.WriteLine("Instance {0} of DumbJob says: {1}", instName, jobSays);
    }
}

You could also just start a thread in a windows service, keep track of when you last fired the exe and then reset afterwards. It's a bit simpler thatn Quartz, and would accomplish the same things. However, your question was Quartz specific.

Upvotes: 6

Related Questions