Quartz. NET Summary (1), Quartz. NET Summary (

Source: Internet
Author: User
Tags log4net

Quartz. NET Summary (1), Quartz. NET Summary (

Some time ago, it took a lot of time to switch the original scheduled task to Quartz. NET for management. The original background scheduled service is implemented by planning tasks. However, as the business grows, more and more scheduled tasks exist. Each Background Service has to create a scheduled task. Routine maintenance and management are very troublesome.

 

As a result, I decided to introduce the Quartz. NET Framework to manage all background timer services in a unified manner.

 

Quartz. the advantages and application scenarios of. NET are not mentioned here. There are many instructions on the Internet, in general, Quartz. NET is an open-source job scheduling framework. It is very suitable for regular round robin Database Synchronization, regular mail notifications, and regular data processing during normal work. Quartz. NET allows developers to schedule jobs by time interval (or day. It realizes the many-to-many relationship between jobs and triggers, and can associate multiple jobs with different triggers for flexible and convenient configuration.

 

Refer to official learning documents: http://www.quartz-scheduler.net/documentation/index.html

 

Quickly build a Quartz and download the source code

 Step 1: create a solution and related projects, and install related packages, as shown in:

Quartz depends on Common. logging and Common. logging. log4Net, and Log4Net are also familiar with log tools. Therefore, we use log4net to record logs, and the scheduled jobs are generally in the window service, we can also use Topshelf to create our window service.

 

  Step 2: create two Job classes DemoJob1 and DemoJob2. To implement IJob, write the business logic to be processed in the Execute method, and the system will process it regularly according to the Quartz configuration.

Using System;
Namespace Quartz. Net. Jobs
{
/// <Summary>
/// Implement the IJob Interface
/// </Summary>
Public class DemoJob1: IJob
{
Private static readonly Common. Logging. ILog logger = Common. Logging. LogManager. GetLogger (System. Reflection. MethodBase. GetCurrentMethod (). DeclaringType );
Public void Execute (IJobExecutionContext context)
{
Try
{
Logger. Info ("DemoJob1 task starts running ");

For (int I = 0; I <10; I ++)
{
Logger. InfoFormat ("DemoJob1 is running {0}", I );
}

Logger. Info ("DemoJob1 task running ended ");
}
Catch (Exception ex)
{
Logger. Error ("DemoJob2 running exception", ex );
}

}
}
}

 Step 3: Configure quartz. config and quartz_jobs.xml

  Quartz. config

# You can configure your scheduler in either <quartz> configuration section

# Or in quartz properties file

# Configuration section has precedence

Quartz. schedtest. instanceName = QuartzTest

# Configure thread pool info

Quartz. threadPool. type = Quartz. Simpl. SimpleThreadPool, Quartz

Quartz. threadPool. threadCount = 10

Quartz. threadPool. threadPriority = Normal

# Job initialization plugin handles our xml reading, without it defaults are used

Quartz. plugin. xml. type = Quartz. Plugin. Xml. XMLSchedulingDataProcessorPlugin, Quartz

Quartz. plugin. xml. fileNames = ~ /Quartz_jobs.xml

# Export this server to remoting context

# Quartz. schedporter. exporter. type = Quartz. Simpl. RemotingSchedulerExporter, Quartz

# Quartz. schedporter. exporter. port = 555

# Quartz. schedporter. exporter. bindName = QuartzScheduler

# Quartz. schedporter. exporter. channelType = tcp

# Quartz. schedporter. exporter. channelName = httpQuartz

 

  Quartz_jobs.xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<! -- This file contains job definitions in schema version 2.0 format -->
<Job-scheduling-data xmlns = "http://quartznet.sourceforge.net/JobSchedulingData" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" version = "2.0">
<Processing-directives>
<Overwrite-existing-data> true </overwrite-existing-data>
</Processing-directives>
<Schedule>
<! -- Define example Task 1 Job -->
<Job>
<Name> DemoJob1 </name>
<Group> DeomJobGroup </group>
<Description> Quartz. Net example Task 1 </description>
<Job-type> Quartz. Net. Jobs. DemoJob1, Quartz. Net. Jobs </job-type>
<Durable> true </durable>
<Recover> false </recover>
</Job>
<! -- Define example Task 2 Job -->
<Job>
<Name> DemoJob2 </name>
<Group> DeomJobGroup </group>
<Description> Quartz. Net example Task 2 </description>
<Job-type> Quartz. Net. Jobs. DemoJob2, Quartz. Net. Jobs </job-type>
<Durable> true </durable>
<Recover> false </recover>
</Job>
<! -- Define the example Task 1 trigger to execute the DemoJob1 task every 30 seconds -->
<Trigger>
<Cron>
<Name> DemoJob1Trigger </name>
<Group> DeomJobTriggerGroup </group>
<Job-name> DemoJob1 </job-name>
<Job-group> DeomJobGroup </job-group>
<Cron-expression> 0/30 ****? </Cron-expression>
</Cron>
</Trigger>
<! -- Define the example Task 2 trigger to execute the DemoJob2 task once every minute -->
<Trigger>
<Cron>
<Name> DemoJob2Trigger1 </name>
<Group> DeomJobTriggerGroup </group>
<Job-name> DemoJob2 </job-name>
<Job-group> DeomJobGroup </job-group>
<Cron-expression> 0 ****? </Cron-expression>
</Cron>
</Trigger>
<! -- Define the example Task 2 trigger to execute the DemoJob2 task at every day -->
<Trigger>
<Cron>
<Name> DemoJob2Trigger2 </name>
<Group> DeomJobTriggerGroup </group>
<Job-name> DemoJob2 </job-name>
<Job-group> DeomJobGroup </job-group>
<Cron-expression> 0 0 1 **? </Cron-expression>
</Cron>
</Trigger>
</Schedule>
</Job-scheduling-data>

 

  Step 4: Host Program, which can be the window service or the background Console program. How can we use Topshelf to create our window service, please refer to another article titled using Topshelf to develop windows Services.

Namespace Quartz. Net. Console
{
Class Program
{
Private static IScheduler scheduler;
Static void Main (string [] args)
{
ISchedulerFactory schedulerFactory = new StdSchedulerFactory ();
Scheduler = schedulerFactory. GetScheduler ();

Scheduler. Start ();
}
}
}

 

  After running, the effect is as follows:

  

 

Related Article

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.