Quartz.Net 学习随手记之02 简单示例
下载Quartz.net 2.1.2,新建控制台应用程序,并添加如下引用
控制台程序代码如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Text; using System.Threading.Tasks; using log4net; using Quartz; using Quartz.Impl; using Quartz.Job; namespace QuartzNET212ConsoleApp { class Program { static void Main(string[] args) { NameValueCollection properties = new NameValueCollection(); properties["quartz.scheduler.instanceName"] = "ConsoleScheduler"; properties["quartz.scheduler.instanceId"] = "instance_one"; properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz"; properties["quartz.threadPool.threadCount"] = "10"; properties["quartz.threadPool.threadPriority"] = "Normal"; properties["quartz.jobStore.misfireThreshold"] = "60000"; properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz"; properties["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz"; properties["quartz.jobStore.useProperties"] = "true"; properties["quartz.jobStore.dataSource"] = "default"; properties["quartz.jobStore.tablePrefix"] = "QRTZ_"; properties["quartz.dataSource.default.connectionString"] = "Server=xx.xx.xx.xx;Database=xx;User Id=xx;Password=xx"; properties["quartz.dataSource.default.provider"] = "SqlServer-20"; ISchedulerFactory sf = new StdSchedulerFactory(properties); IScheduler sched = sf.GetScheduler(); JobDataMap jobDataMap = new JobDataMap(); jobDataMap.Put("msg", "Hello, this is my first job"); IJobDetail job = JobBuilder.Create<HelloJob>() .WithIdentity("HelloJob", "HelloJobGroup") .WithDescription("This is my first job") .UsingJobData(jobDataMap) .RequestRecovery() .Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity("HelloJob", "HelloJobGroup") .WithDescription("This is my first trigger") .WithCronSchedule("0/5 * * * * ?")//run every 5 seconds .Build(); // Validate that the job doesn't already exists if (!sched.CheckExists(job.Key)) { sched.ScheduleJob(job, trigger); } // Start the scheduler if its in standby if (!sched.IsStarted) { sched.Start(); } } } }
对应的作业定义如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using log4net; using Quartz; namespace QuartzNET212ConsoleApp { public class HelloJob : IJob { public void Execute(IJobExecutionContext context) { ILog log4net = LogManager.GetLogger(GetType()); string msg = context.JobDetail.JobDataMap.GetString("msg"); log4net.Info(msg); Console.WriteLine(msg); } } }
App.config配置文件如下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0"?> <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> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <common> <logging> <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net1211"> <arg key="configType" value="INLINE" /> </factoryAdapter> </logging> </common> <log4net> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%d [%t] %-5p %l - %m%n" /> </layout> </appender> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="ApplicationLog.txt" /> <appendToFile value="true" /> <!--Make the rolling file name with the date and size--> <rollingStyle value="Composite" /> <datePattern value="yyyyMM" /> <maxSizeRollBackups value="100" /> <maximumFileSize value="2MB" /> <!--Make the rolling file name like this MyQuartzLog201303.txt, or the deault will be MyQuartzLog.txt201303--> <PreserveLogFileNameExtension value="true" /> <staticLogFileName value="false" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" /> </layout> </appender> <root> <level value="INFO" /> <appender-ref ref="RollingFileAppender" /> </root> </log4net> </configuration>
注意配置文件的版本
产生的Log日志文件如下
数据库中保存的JobDetail和Trigger如下
为了支持数据库,请自行执行Quartz.Net附带的数据库脚本
作者:舍长
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.