1 看到QuartzNet好长时间了,一直想测试一下,因为目前从事的项目好多都有涉及到时间调度任务,正好这个周末也没什么事,于是拿来尝尝香了,以下是我的测试代码:
2
3 配置文件(app.config):
4
5 <? xml version = " 1.0 " ?>
6 < configuration >
7 <!-- 关于Quartz.NET的配置 -->
8 < configSections >
9 < section name = " quartz " type = " System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089 " />
10 < sectionGroup name = " common " >
11 < section name = " logging " type = " Common.Logging.ConfigurationSectionHandler, Common.Logging " />
12 </ sectionGroup >
13 </ configSections >
14 < common >
15 < logging >
16 < factoryAdapter type = " Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter, Common.Logging " >
17 < arg key = " showLogName " value = " true " />
18 < arg key = " showDataTime " value = " true " />
19 < arg key = " level " value = " DEBUG " />
20 < arg key = " dateTimeFormat " value = " HH:mm:ss:fff " />
21 </ factoryAdapter >
22 </ logging >
23 </ common >
24 < quartz >
25 < add key = " quartz.scheduler.instanceName " value = " ExampleDefaultQuartzScheduler " />
26 < add key = " quartz.threadPool.type " value = " Quartz.Simpl.SimpleThreadPool, Quartz " />
27 < add key = " quartz.threadPool.threadCount " value = " 10 " />
28 < add key = " quartz.threadPool.threadPriority " value = " 2 " />
29 < add key = " quartz.jobStore.misfireThreshold " value = " 60000 " />
30 < add key = " quartz.jobStore.type " value = " Quartz.Simpl.RAMJobStore, Quartz " />
31 < add key = " cronExpr " value = " 0/10 * * * * ? " />
32 </ quartz >
33 < startup >< supportedRuntime version = " v2.0.50727 " /></ startup ></ configuration >
34 Job类(DBJob):
35
36 using System;
37 using System.Collections.Generic;
38 using System.Linq;
39 using System.Text;
40
41 using System.Windows.Forms;
42 using Quartz;
43 using Common.Logging;
44
45 namespace QuartzNetTest
46 {
47 public class DBJob : IJob
48 {
49 private static ILog _log = LogManager.GetLogger( typeof (DBJob));
50
51 public DBJob()
52 {
53 }
54
55 public void Execute(JobExecutionContext context)
56 {
57 try
58 {
59 // string sql = "INSERT INTO test(nowDate) VALUES ('" + DateTime.Now.ToLocalTime() + "') ";
60 // Common.SqlHelper.ExecuteSql(sql);
61
62 Console.WriteLine( " Job executed! " );
63 _log.Info( string .Format( " Hello World! - {0} " , System.DateTime.Now.ToString( " r " )));
64 }
65 catch (Exception e)
66 {
67 JobExecutionException e2 = new JobExecutionException(e);
68 e2.RefireImmediately = true ;
69 throw e2;
70 }
71 }
72 }
73 }
74
75 应用程序(btn1是启动任务,btn2是关闭任务,在该测试实例中分别测试了SimpleTrigger和CronTrigger两种Trigger,执行了两个job):
76
77 using System;
78 using System.Collections.Generic;
79 using System.ComponentModel;
80 using System.Data;
81 using System.Drawing;
82 using System.Linq;
83 using System.Text;
84 using System.Windows.Forms;
85
86 using Quartz;
87 using Quartz.Impl;
88 using Common.Logging;
89 using System.Configuration;
90 using System.Collections;
91
92 namespace QuartzNetTest
93 {
94 public partial class Form1 : Form
95 {
96 IScheduler sched = null ;
97
98 // ILog log = LogManager.GetLogger("start");
99 ILog log = LogManager.GetLogger( " ---------- " );
100
101 public Form1()
102 {
103 InitializeComponent();
104 // log.Debug("test...");
105 }
106
107 private void btn1_Click( object sender, EventArgs e)
108 {
109 // 在应用程序启动时运行的代码
110 log.Info( " ------- Initializing ------------------- " );
111
112 Quartz.ISchedulerFactory sf = new Quartz.Impl.StdSchedulerFactory();
113 // get a scheduler
114 sched = sf.GetScheduler();
115
116 log.Info( " ------- Initialization Complete -------- " );
117 log.Info( " ------- Scheduling Jobs ---------------- " );
118
119 Quartz.JobDetail job = new Quartz.JobDetail( " job1 " , " group1 " , typeof (DBJob));
120
121 System.Collections.Specialized.NameValueCollection StrQuartz = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection( " quartz " );
122
123 string cronExpr = StrQuartz[ " cronExpr " ].ToString();
124 // MessageBox.Show(StrQuartz.AllKeys[0].ToString() + " " + StrQuartz["cronExpr"]); // 输出Hello World
125
126 Quartz.CronTrigger trigger = new Quartz.CronTrigger( " trigger1 " , " group1 " , " job1 " , " group1 " , cronExpr);
127
128 /// /one method /// /
129 /// /sched.AddJob(job, true);
130 /// /DateTime ft = sched.ScheduleJob(trigger);
131 /// /one method /// /
132
133 /// two method /// /
134 DateTime ft = sched.ScheduleJob(job, trigger);
135 /// two method /// /
136
137
138 /////////////// job2 /////////////// //
139 SimpleTrigger triggerr = new SimpleTrigger( " myTrigger " ,
140 null ,
141 DateTime.UtcNow,
142 null ,
143 SimpleTrigger.RepeatIndefinitely,
144 TimeSpan.FromSeconds( 5 ));
145
146 job = new Quartz.JobDetail( " job2 " , " group1 " , typeof (DBJob));
147
148 ft = sched.ScheduleJob(job, triggerr);
149 log.Info( string .Format( " {0} will run at: {1} and repeat: {2} times, every {3} seconds " ,
150 job.FullName, ft.ToString( " r " ), SimpleTrigger.RepeatIndefinitely, (TimeSpan.FromSeconds( 5 ))));
151
152 log.Info( " ------- Starting Scheduler ---------------- " );
153 sched.Start();
154 log.Info( " ------- Started Scheduler ----------------- " );
155 }
156
157 private void btn2_Click( object sender, EventArgs e)
158 {
159 // 在应用程序关闭时运行的代码
160 sched.Shutdown( true );
161 }
162 }
163 }
164
165 测试版本:QuartzNet: 1.0 . 3 ,在应用程序中引用Quartz.dll,Common.Logging.dll即可。