问题
I am looking for a working simple example of Quartz.net for Console application (it can be any other application as long as it simple enough...). And while I am there, is there any wrapper that could help me avoid implementing IJobDetail, ITrigger, etc.
回答1:
There is a guy who made the exact same observation as you, and he has published a blog post with a simple working example of a Quartz.net Console application.
The following is a working Quartz.net example that is built against Quartz.net 2.0 (Latest). What this job does is write a text message, “Hello Job is executed” in the console every 5 sec.
Start a Visual Studio 2012 project. Select Windows Console Application
. Name it Quartz1 or what ever you like.
Requirements
Download Quartz.NET
assembly using NuGet
. Right click on project, select “Manage Nuget Packages”. Then search for Quartz.NET
. Once found select and install.
using System;
using System.Collections.Generic;
using Quartz;
using Quartz.Impl;
namespace Quartz1
{
class Program
{
static void Main(string[] args)
{
// construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();
// get a scheduler, start the schedular before triggers or anything else
IScheduler sched = schedFact.GetScheduler();
sched.Start();
// create job
IJobDetail job = JobBuilder.Create<SimpleJob>()
.WithIdentity("job1", "group1")
.Build();
// create trigger
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
.Build();
// Schedule the job using the job and trigger
sched.ScheduleJob(job, trigger);
}
}
/// <summary>
/// SimpleJOb is just a class that implements IJOB interface. It implements just one method, Execute method
/// </summary>
public class SimpleJob : IJob
{
void IJob.Execute(IJobExecutionContext context)
{
//throw new NotImplementedException();
Console.WriteLine("Hello, JOb executed");
}
}
}
Sources
- Original url
- archive.org link
回答2:
between the documentation and samples in the source code there should be enough to get you started. the only interface you must implement is IJob
when creating custom jobs. all other interfaces are either already implemented for you, or they are not required for basic usage in quartz.net.
to build up jobs and triggers to use the JobBuilder and TriggerBuilder helper objects.
来源:https://stackoverflow.com/questions/8821535/simple-working-example-of-quartz-net