1: using Castle.Core;
2: using Quartz.Impl;
3: using Quartz;
4: using Common.Logging;
5: using System.Threading;
6:
7: namespace QuartzComponent
8: {
9: [Transient]
10: public class QuartzStartable : IStartable
11: {
12: private ISchedulerFactory _schedFactory;
13:
14: private static ILog log = LogManager.GetLogger(typeof(QuartzStartable));
15:
16: public QuartzStartable(ISchedulerFactory schedFactory)
17: {
18: _schedFactory = schedFactory;
19: }
20:
21: public void Start()
22: {
23: log.Info("Starting service");
24: IScheduler sched = _schedFactory.GetScheduler();
25:
26: log.Info("------- Scheduling Jobs ----------------");
27:
28: // jobs can be scheduled before sched.start() has been called
29:
30: // get a "nice round" time a few seconds in the future...
31: DateTime ts = TriggerUtils.GetNextGivenSecondDate(null, 15);
32:
33: // job1 will only fire once at date/time "ts"
34: JobDetail job = new JobDetail("job1", "group1", typeof(SimpleQuartzJob));
35: SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1");
36: // set its start up time
37: trigger.StartTime = ts;
38: // set the interval, how often the job should run (10 seconds here)
39: trigger.RepeatInterval = 10000;
40: // set the number of execution of this job, set to 10 times.
41: // It will run 10 time and exhaust.
42: trigger.RepeatCount = 100;
43:
44:
45: // schedule it to run!
46: DateTime ft = sched.ScheduleJob(job, trigger);
47: log.Info(string.Format("{0} will run at: {1} and repeat: {2} times, every {3} seconds",
48: job.FullName, ft.ToString("r"), trigger.RepeatCount, (trigger.RepeatInterval / 1000)));
49: log.Info("------- Waiting five minutes... ------------");
50:
51: sched.Start();
52: try
53: {
54: // wait five minutes to show jobs
55: Thread.Sleep(300 * 1000);
56: // executing...
57: }
58: catch (ThreadInterruptedException)
59: {
60: }
61:
62:
63: }
64:
65: public void Stop()
66: {
67: log.Info("Stopping service");
68: try
69: {
70: IScheduler scheduler = _schedFactory.GetScheduler();
71: scheduler.Shutdown(true);
72: }
73: catch (SchedulerException se)
74: {
75: log.Error("Cannot shutdown scheduler.", se);
76: }
77:
78: }
79: }
80: }
1: namespace QuartzComponent
2: {
3: class ConsoleMain
4: {
5: static ILog log = LogManager.GetLogger(typeof(ConsoleMain));
6:
7: [STAThread]
8: public static void Main(string[] args)
9: {
10: IWindsorContainer container = new WindsorContainer();
11: //添加Facility
12:
13: container.AddFacility("startable", new StartableFacility());
14:
15: container.AddComponent("Scheduler", typeof(ISchedulerFactory), typeof(StdSchedulerFactory));
16:
17: container.AddComponent("QuartzStartable", typeof(QuartzStartable));
18:
19: //Console.Read();
20: }
21: }
22: }
0人
|
了这篇文章 |
点击图片可刷新验证码请点击后输入验证码博客过2级,无需填写验证码
同时赞一个