1 / 30

Asynchronous Job Processing Using Quartz.Net

Asynchronous Job Processing Using Quartz.Net. Jay Vilalta jay.vilalta@gmail.com. What Is Quartz.Net. Scheduler (think task scheduler) Queue for asynchronous jobs C# port of Quartz (java) Apache license. Why Use Quartz.Net. Scale out Redundancy Smart handling of failures

alexia
Download Presentation

Asynchronous Job Processing Using Quartz.Net

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Asynchronous Job ProcessingUsing Quartz.Net Jay Vilalta jay.vilalta@gmail.com

  2. What Is Quartz.Net • Scheduler (think task scheduler) • Queue for asynchronous jobs • C# port of Quartz (java) • Apache license

  3. Why Use Quartz.Net • Scale out • Redundancy • Smart handling of failures • Job chaining (poor man’s workflow) • Custom scheduling

  4. How Can I Run It • Embedded in your application • As a stand alone windows service

  5. The Basics • Scheduler • Jobs • Triggers

  6. Scheduler • Runs jobs • Manages the scheduling

  7. Jobs • Do the work • Some built-in • Mostly roll you own • Implement IJob

  8. Built-in Jobs • FileScanJob: monitors last modified date • NativeJob: runs executables or batch files • NoOpJob: does nothing • SendMailJob: sends emails

  9. Jobs - Example public class MyJob : IJob { public void Execute(JobExecutionContext context) { try { int count= context.MergedJobDataMap.GetIntegerFromString(“count"); for (int i = 0; i < count; i++){ //do something useful } } catch (ApplicationException ex) { throw new JobExecutionException("Something happened", ex, false); } } }

  10. Triggers • Tell the scheduler when jobs should run • Some built-in • Simple Trigger • Cron Trigger • NthIncludedDayTrigger • Custom Triggers

  11. Simple Trigger • Start Time • Repeat Count • Repeat Interval

  12. Cron Trigger • Similar to UNIX cron • Start Time • Cron Expression • “0 15 10 ? * *” • “0 0,15,30,45 * ? * *”

  13. Custom Triggers • No example this time • Implementing a trigger is not trivial • Must implement 11 methods • Must be able to determine next fire time

  14. Scheduling • Associate a job to a trigger • Multiple triggers can be set • When the trigger fires, the job runs scheduler.ScheduleJob(jobDetail, trigger);

  15. Advanced Features • Listeners • Special Jobs • Remote management • Clustering • Plug-ins • Unit testing

  16. Listeners • Job Listeners • Trigger Listeners • Scheduler Listeners • Job and trigger listeners can be global

  17. Job Listener Example public class JobHistoryListener : IJobListener { public void JobExecutionVetoed(…) public void JobToBeExecuted(…) public void JobWasExecuted(…) }

  18. Trigger Listener Example public class MyTriggerListener:ITriggerListener { public string Name public void TriggerComplete(…) public void TriggerFired(…) public void TriggerMisfired(…) public bool VetoJobExecution(…) }

  19. Special Jobs • Stateful Jobs • Interruptible Jobs

  20. Stateful Jobs • Only one can run at a time • Allow you to save/restore state • You must manage state yourself • Implement IStatefulJob

  21. Interruptible Jobs • Mechanism to interrupt long running jobs • You must implement yourself • Implement IInterruptableJob

  22. Remote Management • Scheduler can be managed remotely • Exposed via Remoting • Most scheduler functions available

  23. JobFactory • Instantiates jobs • Default factory creates a new instance • Create your own if you use DI or IoC container

  24. Job Stores • RAMJobStore • AdoJobStore • MySql • Oracle • Postgres • SQL Lite • SQL Server

  25. Clustering • Load balancing • Job Failover • Caveat: clocks synchronized within a second

  26. Plug-ins • JobInitializationPlugin • LoggingJobHistoryPlugin • LoggingTriggerHistoryPlugin • ShutdownHookPlugin

  27. Plug-ins Stub public class SamplePlugin : ISchedulerPlugin { public void Initialize(string name, IScheduler sched) public void Shutdown() public void Start() }

  28. Unit Testing • You can / should unit test your quartz classes • Use a mocking framework • Mock the Scheduler (IScheduler) • Mock a calendar (ICalendar) • Use mocks to create your context

  29. Sample Unit Test [Test] public void ExecuteTests() { JobDetail detail = new JobDetail(); IScheduler scheduler = new Mock<IScheduler>().Object; ICalendar calendar = new Mock<ICalendar>().Object; IJob job = new NoOpJob(); detail.Name = "Test"; detail.JobDataMap.Add("SOMETHING", "ELSE"); TriggerFiredBundle bundle = new TriggerFiredBundle(detail, new SimpleTrigger(), calendar, false, null, null, null, null); JobExecutionContext context = new JobExecutionContext(scheduler, bundle, job); JobHistoryListener listener = new JobHistoryListener(); listener.JobToBeExecuted(context); listener.JobWasExecuted(context, null); //methods return void so need to get creative to determine if execution was successful } }

  30. Resources Project Home: http://quartznet.sourceforge.net/ Mailing List: http://groups.google.com/group/quartznet Getting Started: http://jvilalta.blogspot.com

More Related