basquang™ on clouds

August 4, 2014

[Quartz.NET]: Get Started

Filed under: Quartz,Quartz.NET — basquang @ 4:32 PM
Tags: ,

What is Quartz.NET?

“Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems. ”

Quartz.NET is a pure .NET library written in C# and is a port of very popular open source Java job scheduling framework, Quartz. This project owes very much to original Java project, it’s father James House and the project contributors.

http://www.quartz-scheduler.net/

How to get started?

The official guide here: http://www.quartz-scheduler.net/documentation/quartz-2.x/quick-start.html

And the steps here:

  1. Create Console Application
  2. Install Quartz.NET from NuGet package by running following command

    Install-Package Quartz

    Quartz NuGet will install following packages at this time I writing the blog post

    <packages>
      <package id="Common.Logging" version="2.1.2" targetFramework="net45" />
      <package id="Quartz" version="2.2.4" targetFramework="net45" />
    </packages>
    
  3. Add following code in the namespace
  4. Program class

    class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    //Configure simple logging
                    Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter { Level = Common.Logging.LogLevel.Info };
    
                    // Grab the Scheduler instance from the Factory 
                    IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
    
                    // and start it off
                    scheduler.Start();
    
                    // define the job and tie it to our HelloJob class
                    IJobDetail job = JobBuilder.Create<HelloJob>()
                        .WithIdentity("job1", "group1")
                        .Build();
    
                    // Trigger the job to run now, and then repeat every 10 seconds
                    ITrigger trigger = TriggerBuilder.Create()
                        .WithIdentity("trigger1", "group1")
                        .StartNow()
                        .WithSimpleSchedule(x => x
                            .WithIntervalInSeconds(10)
                            .RepeatForever())
                        .Build();
    
                    // Tell quartz to schedule the job using our trigger
                    scheduler.ScheduleJob(job, trigger);
    
    
                    // some sleep to show what's happening
                    Thread.Sleep(TimeSpan.FromSeconds(60));
    
                    // and last shut down the scheduler when you are ready to close your program
                    scheduler.Shutdown();
                }
                catch (SchedulerException se)
                {
                    Console.WriteLine(se);
                }
    
    
                Console.WriteLine("Press any key to close the application");
                Console.ReadKey();
    
            }
        }
    

    Job class

    public class HelloJob : IJob
        {
            public void Execute(IJobExecutionContext context)
            {
                Console.WriteLine("Greetings from HelloJob!");
            }
        }
    
  5. The output like this.

Hope this help!

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: