Quartz.net-quartz.net Quick Start Guide

Source: Internet
Author: User

Transferred from: http://www.quartz-scheduler.net/documentation/quartz-2.x/quick-start.html

Quartz.net Quick Start Guide

Welcome to the "Quick Start Guide" for Quartz.net. As you read the expect to see details of:

    • Downloading Quartz.net
    • Installing Quartz.net
    • Configuring Quartz to your own particular needs
    • Starting a sample application
Download and Install

You can either download the zip file or use the NuGet package. NuGet package contains only the binaries needed to run quartz.net, zip file comes with source code, samples and Quartz.net Server sample application.

Zip Archive

Short version:once you ' ve downloaded quartz.net, unzip it somewhere, grab the Quartz.dll and Common.Logging.dll from bin Directory and start to use them.

Quartz depends only in single Third-party library called Common.logging (which contains Logging abstractions this allow yo U to use the logging provider this suites you). You need to has Quartz.dll and Commong.Logging.dll beside your app binaries to successfully run quartz.net. So just add them as references to your Visual Studio project, that uses them. You can find these DLLs from extracted archive from path Bin\your-target-framework-version\release\quartz.

NuGet Package

Couldn ' t get any simpler than this. Just fire up Visual Studio, with NuGet installed, and add reference to Packagequartz from the Package Manager extension:

    • Right-click on your project ' s References and choose Manage NuGet Packages ...
    • Choose Online category from the left
    • Enter Quartz to the top right search and hit enter
    • Choose quartz.net from search results and hit install
    • done!

or from NuGet command-line:

Install-Package Quartz
Configuration

This is the big bit! Quartz.net is a very configurable library. There is three ways (which is not mutually exclusive) to supply quartz.net configuration information:

    • programmatically via providing NameValueCollection parameter to scheduler factory
    • Via standard youapp.exe.config configuration file using Quartz-element
    • Quartz.config file in your application ' s root directory

You can find samples of any these alternatives in the quartz.net zip file.

Full documentation of available properties are available in the Quartz Configuration Reference.

To get up and running quickly, a basic quartz.config looks something like this:

quartz.scheduler.instanceName = MySchedulerquartz.threadPool.threadCount = 3quartz.jobStore.type = Quartz.Simpl.RAMJobStore, Quartz

Remember to set the copy to Output Directory on Visual Studio's File property pages to has the value Copy always. Otherwise The config won't be seen if it's not in build directory.

The scheduler created by this configuration has the following characteristics:

    • Quartz.scheduler.instancename-this Scheduler ' s name would be "Myscheduler".
    • Quartz.threadpool.threadcount-there is 3 threads in the thread pool, which means that a maximum of 3 jobs can be run Si multaneously.
    • Quartz.jobstore.type-all of Quartz ' s data, such as details of jobs and triggers, is held in memory (rather than in a dat Abase). Even if you had a database and want to use it with Quartz, I suggest get Quartz working with the Ramjobstore before Y OU open up a whole new dimension by working with a database.

Actually you don ' t need to define these properties if you don ' t want to, Quartz.net comes with sane defaults

Starting a Sample Application

Now you ' ve downloaded and installed Quartz, it's time to get a sample application up and running. The following code obtains an instance of the scheduler, starts it, then shuts it down:

Program.cs

UsingSystem;UsingSystem.Threading;UsingQuartz;UsingQuartz.impl;NamespaceConsoleApplication1{PublicClassProgram{PrivateStaticvoidMain(String[]Args){Try{Grab the Scheduler instance from the FactoryISchedulerScheduler=Stdschedulerfactory.Getdefaultscheduler();and start it offScheduler.Start();Some sleep to show what ' s happeningthread. Sleep (timespan. Fromseconds (60//and last shut, the scheduler when your is ready-to-close your program scheduler
                                                            
                                                             shutdown
                                                              (); } catch  (schedulerexception se< Span class= "P" >) {console. Writeline (se} } }}        /span>          
                                                                  

Once you obtain a scheduler using Stdschedulerfactory.getdefaultscheduler (), your application won't terminate by defaul T until you call scheduler. Shutdown (), because there'll be active threads (Non-daemon threads).

No running the program would not show anything. When ten seconds has passed the program would just terminate. Lets Add some logging to console.

Adding Logging

Common.logging can configured to use different Logging frameworks under the hood; namely Enterprise Library, Log4net and NLog.

However, to keep things simple for our example we take the simple route and configure logging using code to just log to th E console using common.logging basic Logging mechanism.

Add the following line to the beginning of your Program.cs

Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter { Level = Common.Logging.LogLevel.Info};
Trying out the application and adding jobs

No we should get a lot more information when we start the application.

11.1.2014 14:52:04 [INFO] quartz.impl.stdschedulerfactory-quartz.net properties loaded from configuration file ' C:\Cons Oleapplication1\bin\debug\quartz.config ' 11.1.2014 14:52:04 [INFO] quartz.impl.stdschedulerfactory-using default Implementation for Object serializer11.1.2014 14:52:04 [INFO] quartz.impl.stdschedulerfactory-using default Implementa tion for ThreadExecutor11.1.2014 14:52:04 [INFO] quartz.core.schedulersignalerimpl-initialized Scheduler signaller of T ype:quartz.core.schedulersignalerimpl11.1.2014 14:52:04 [INFO] Quartz.core.quartzscheduler-quartz Scheduler v.2.2.1.400 created.11.1.2014 14:52:04 [INFO] Quartz.simpl.ramjobstore-ramjobstore initialized.11.1.2014 14:52:04 [ INFO] Quartz.core.quartzscheduler-scheduler Meta-data:quartz Scheduler (v2.2.1.400) ' Myscheduler ' with InstanceId ' NON  _clustered ' Scheduler class: ' Quartz.Core.QuartzScheduler '-running locally.  Not STARTED.  Currently in standby mode. Number of jobs executed:0 Using thread pool' Quartz.Simpl.SimpleThreadPool '-with 3 threads. Using job-store ' Quartz.Simpl.RAMJobStore '-which does not the support persistence. and is not clustered.11.1.2014 14:52:04 [INFO] Quartz.impl.stdschedulerfactory-quartz Scheduler ' Myscheduler ' Initializ ed11.1.2014 14:52:04 [INFO] Quartz.impl.stdschedulerfactory-quartz Scheduler version:2.2.1.40011.1.2014 14:52:04 [INF O] Quartz.core.quartzscheduler-scheduler myscheduler_$_non_clustered started.

We need a simple test job to test the functionality, lets create hellojob that outputs greetings to console.

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

To does something interesting, you need code just after Start () method, before the Thread.Sleep.

Define the job and tie it to our Hellojob classIjobdetailJob=Jobbuilder.Create<Hellojob> ().Withidentity("Job1","Group1").Build();Trigger the job to run now, and then repeat every secondsItriggerTrigger=Triggerbuilder.Create().Withidentity ( "Trigger1" startnow () . Withsimpleschedule (x => x .< span class= "n" >withintervalinseconds (10) .repeatforever ()) . Buildscheduler. Schedulejob (jobtrigger   

The complete console application would now look like this

UsingSystem;UsingSystem.Threading;UsingQuartz;UsingQuartz.impl;UsingQuartz.job;NamespaceConsoleApplication1{PublicClassProgram{PrivateStaticvoidMain(String[]Args){Try{Common.Logging.Logmanager.Adapter=NewCommon.Logging.Simple.Consoleoutloggerfactoryadapter{Level=Common.Logging.LogLevel.Info};Grab the Scheduler instance from the FactoryISchedulerScheduler=Stdschedulerfactory.Getdefaultscheduler();and start it offScheduler.Start();Define the job and tie it to our Hellojob classIjobdetailJob=Jobbuilder.Create<Hellojob> ().Withidentity("Job1","Group1").Build();Trigger the job to run now, and then repeat every secondsItriggerTrigger=Triggerbuilder.Create().Withidentity("Trigger1","Group1").Startnow().Withsimpleschedule(X=X.Withintervalinseconds(10).RepeatForever()).Build();Tell Quartz to schedule the job using our triggerScheduler.Schedulejob(Job,Trigger);Some sleep to show what ' s happeningThread.Sleep(TimeSpan.FromSeconds(60));And last shut off the scheduler when you were ready to close your programScheduler.Shutdown();}Catch(SchedulerexceptionSe){Console.WriteLine(Se);}Consolewriteline ( "Press any key to close the application" console. Readkey (); } } public class hellojob : ijob {public void  (ijobexecutioncontext context) {console. Writeline ( "Greetings from hellojob!" ); } }}          /span>                

Now go has some fun exploring quartz.net! You can continue by reading the tutorial.

Quartz.net-quartz.net Quick Start Guide

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.