The Quartz.NET integrated into the Castle

Original: https://cloud.tencent.com/developer/article/1030346

Castle is an open source project for the .NET platform, from data access framework ORM to IOC container, then MVC framework WEB layer, AOP, basically includes everything throughout the development process, building enterprise-class applications for our fast program provides a good service. concrete can be found TerryLee the Castle developed a series of articles . Control can be reversed (Inversion of Control, IoC) through a component called Facility and the third-party dependency injection assembly into the kernel. Startable Facility When a component after meeting certain dependencies, let it run automatically, such as starting a form or start a service. Startable Facility of use can be very simple, as long as our component implements IStartable interfaces on it, on Startable Facility Specific can be found in the practice of the Castle IOC container Startable Facility (a) , the practice of Castle IOC container Startable Facility (II) . Quartz is a Castle to be integrated with large projects, because it only requires you to use Castle's life cycle to start and stop it. This means that, when Castle starts, you want to start Quartz, when the Castle is closed, you want to stop Quartz.

To keep it simple this example, Quartz configured with default values ​​Quartz release came with. These default values ​​are located quartz.properties file, the file is part of dll file. To configure the Quartz to the database for persistence layer, remote scheduling and other advanced options, you must create a custom file quartz.properties.

Quartz Scheduler easy startup and shutdown; only retrieve object by calling the scheduler StdSchedulerFactory.DefaultScheduler. To launch Quartz, execute Scheduler.Start () method. To stop Quartz, execute Scheduler.Shutdown () method. To follow the life cycle of Quartz Castle, the Start () calls into IStartable the Start () method, and Shutdown () call is placed IStartable Stop (method). Listing 3 shows the complete implementation of the code after adding Quartz.

   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: }

    Quartz.net Castle will be integrated into the container, only a few lines of code, it will automatically enable job scheduling Quartz.net Castle when the vessel started.

   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: }

Conclusion

For most open source projects, to achieve a small amount of work can be integrated into the Castle container, the application is similar Quartz.net simple integration of excellent candidates, because it only needs to be started and shut down. There are a lot of help with Quartz.net as simple integration of open source projects.

Download example code: QuartzComponent.zip

Guess you like

Origin www.cnblogs.com/yanglang/p/12072340.html