1. 介绍
Quartz为后台工作者提供了得便利,我们下面介绍一下它的配置。本文配置主要针对服务程序的配置。
但是在做下面配置之前,要安装包
Install-Package Quartz
2. Quartz配置有两种方法,第一种是纯XML配置;第二种是抛弃XML文件配置,用纯代码配置。
2.1 Quartz下的纯XML配置
1)首先新建job配置XML文件quartz_jobs.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <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> <!--开始执行一个调度--> <job> <name>TestJob</name> <group>TestJobGroup</group> <description>TestJobDescription</description> <job-type>FeiDuWindowService.Job.TestJob, FeiDuWindowService</job-type> <durable>true</durable> <recover>false</recover> </job> <trigger> <cron> <name>TestJobTrigger</name> <group>TestJobTriggerGroup</group> <job-name>TestJob</job-name> <job-group>TestJobGroup</job-group> <cron-expression>*/5 * * * * ?</cron-expression> </cron> </trigger> <!--结束一个调度--> </schedule> </job-scheduling-data>
说明: job标签下面的name与group随便怎么写。注意job-type标签要写类的具体包路径,与项目名称。
trigger下面的cron-expression标签说明每5秒钟运行一次。
2)新建job类,继承IJob
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FeiDuWindowService.Helper; using Quartz; namespace FeiDuWindowService.Job { public class TestJob:IJob { public void Execute(IJobExecutionContext context) { try { LogHelper.InfoLog.InfoFormat("--执行job-- " + DateTime.Now); } catch (Exception e) { } } } }
3)项目启动时,配置加载xml文件
//加载xml job的配置文件 NameValueCollection properties = new NameValueCollection(); properties["quartz.plugin.xml.type"] = "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz"; properties["quartz.plugin.xml.fileNames"] = "~/Job/quartz_jobs.xml"; //开启job ISchedulerFactory sf = new StdSchedulerFactory(properties); IScheduler sched = sf.GetScheduler(); if(!sched.IsStarted) sched.Start();
2.2 Quartz的纯代码
用纯代码很简单,什么都不用配置,直接上代码
ISchedulerFactory factory = new StdSchedulerFactory(); IScheduler scheduler = factory.GetScheduler(); if (!scheduler.IsStarted) { IJobDetail testjob = JobBuilder.Create<TestJob>().WithIdentity("TestJob", "TestJobGroup").Build(); //将任务与触发器关联起来放到调度器中 ITrigger trigger = TriggerBuilder.Create().StartNow().WithIdentity("TestjobTrigger", "TestjobTriggerGroup").WithCronSchedule("*/5 * * * * ?").Build(); scheduler.ScheduleJob(testjob, trigger); scheduler.Start(); LogHelper.InfoLog.Info("--启动job OnStart--"); }
3. job在App.config的配置
可以不加这个配置,但是为了易于管理,我们一般把下面的JOB配置加到App.config下面
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <sectionGroup name="common"> <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging"/> </sectionGroup> </configSections> <common> <logging> <factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging"> <arg key="showLogName" value="true"/> <arg key="showDataTime" value="true"/> <arg key="level" value="INFO"/> <arg key="dateTimeFormat" value="HH:mm:ss:fff"/> </factoryAdapter> </logging> </common> <quartz> <add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler"/> <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/> <add key="quartz.threadPool.threadCount" value="10"/> <add key="quartz.threadPool.threadPriority" value="2"/> <add key="quartz.jobStore.misfireThreshold" value="60000"/> <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/> <add key="quartz.dbprovider.customProvider.productName" value="Microsoft SQL Server, provider V2.0.0.0 in framework .NET V2.0"/> <add key="quartz.dbprovider.customProvider.assemblyName" value="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <add key="quartz.dbprovider.customProvider.connectionType" value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <add key="quartz.dbprovider.customProvider.commandType" value="System.Data.SqlClient.SqlCommand, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <add key="quartz.dbprovider.customProvider.parameterType" value="System.Data.SqlClient.SqlParameter, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <add key="quartz.dbprovider.customProvider.commandBuilderType" value="System.Data.SqlClient.SqlCommandBuilder, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <add key="quartz.dbprovider.customProvider.parameterDbType" value="System.Data.SqlDbType, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <add key="quartz.dbprovider.customProvider.parameterDbTypePropertyName" value="SqlDbType" /> <add key="quartz.dbprovider.customProvider.parameterNamePrefix" value="@" /> <add key="quartz.dbprovider.customProvider.exceptionType" value="System.Data.SqlClient.SqlException, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <add key="quartz.dbprovider.customProvider.useParameterNamePrefixInParameterCollection" value="true" /> <add key="quartz.dbprovider.customProvider.bindByName" value="true" /> <add key="quartz.dbprovider.customProvider.dbBinaryTypeName" value="Image" /> </quartz> </configuration>
时间: 2024-10-23 04:21:16