DotNetCore cross-platform ~ The Gospel of Quartz hot deployment ~ Monitors folder changes, dotnetcorequartz

Source: Internet
Author: User

DotNetCore cross-platform ~ The Gospel of Quartz hot deployment ~ Monitors folder changes, dotnetcorequartz

Back to directory

After DotNetCore is released, quartz is also used to design the scheduling center to facilitate deployment in docker. In the previous quartz version, the configuration file method is supported, but now it is not supported. We should think about it. Why not support the configuration file? Of course, uncle also designed support methods for the configuration file, but we should still think about why the author does not support configuration?

Hot swapping, service discovery?

It may have something to do with the above two concepts. Hot swapping is easy to understand. It can be started directly when the dll module is placed in a running project. This function is necessary for the scheduling center, because you may need to design some service jobs according to different functions, and these services have no connection. Each job has a single function. If it is deployed in hot mode, it will be nice! Service Discovery is very eye-catching in the recent <microservice>. The service discovery today is not about it, but the monitoring of folders published by dotnet core, which is actually an event mechanism, when the underlying windows Folder changes (add, delete, and change files), the corresponding event is triggered up, and we only need to subscribe to it, which is very loosely coupled!

Public Events

Var watcher = new FileSystemWatcher (); watcher. path = AppDomain. currentDomain. baseDirectory; watcher. policyfilter = policyfilters. attributes | policyfilters. creationTime | policyfilters. directoryName | policyfilters. fileName | policyfilters. lastAccess | policyfilters. lastWrite | policyfilters. security | policyfilters. size; watcher. filter = "*. dll "; // when running quartz, you can add a new job, but it cannot overwrite, delete, and so on watcher. created + = new FileSystemEventHandler (o, e) => {foreach (var module in Assembly. loadFile (e. fullPath ). getModules () {foreach (var type in module. getTypes (). where (I => I. baseType = typeof (JobBase) {jow.quartz (type) ;}}); // Start monitoring. watcher. enableRaisingEvents = true;

Adding a job to Quartz needs to be used in initialization and folder monitoring. So we abstracted it and mentioned it in a delegate to use it. the Actioin <T> delegation of net4.0 is the most suitable!

Action <Type> jow.quartz = (type) => {var obj = Activator. createInstance (type); string cron = type. getProperty ("Cron "). getValue (obj ). toString (); var jobDetail = JobBuilder. create (type ). withIdentity (type. name ). build (); var jobTrigger = TriggerBuilder. create (). withIdentity (type. name + "Trigger "). startNow (). withCronSchedule (cron ). build (); StdSchedulerFactory. getdefaschscheduler (). result. schedu LeJob (jobDetail, jobTrigger); Console. ForegroundColor = ConsoleColor. Yellow; Console. WriteLine ($ "added a new service {nameof (type)}, automatically loaded through heartbeat Job! ");};

Uncle applies it to the scheduling center. Specifically, when a new module DLL appears in the debugging project, it is automatically added to the quartz list, then, of course, it has its own cron expression to control its own scheduling cycle. Whether you want to control the scheduling cycle at a specified point or after periodic running is completed, you should add a new job if there are new features,Is there a single responsibility and the principle of opening and closing!

JobBase, the Job of all modules must inherit from it. You can implement the cron and business ExcuteJob methods based on your own situation!

[DisallowConcurrentExecution ()] public abstract class JobBase: IJob {# region IJob member public Task Execute (IJobExecutionContext context) {try {Console. foregroundColor = ConsoleColor. red; Console. writeLine (DateTime. now. toString () + "{0} This Job starts execution", context. jobDetail. key. name); ExcuteJob (context); return Task. completedTask;} catch (Exception ex) {LoggerFactory. createLog (). logger_Debug (this. getTy Pe (). name + "error:" + ex. message); throw ;}# endregion /// <summary> /// execution plan, sub-classes can be rewritten /// </summary> public virtual string Cron => "0/5 ****? "; /// <Summary> // The specific Job class to implement its own logic // </summary> protected abstract void ExcuteJob (IJobExecutionContext context );}

The following shows the implementation of one of the Business jobs, which consists of the scheduling plan cron and the business execution method ExcuteJob.

/// <Summary> /// send a message // </summary> public class SendEmailJob: JobBase {public override string Cron => "0/2 ****? "; Protected override void ExcuteJob (IJobExecutionContext context) {Console. writeLine ("Send Email"); LoggerFactory. createLog (). logger_Debug ("Send Email:" + DateTime. now );}}

After we publish this project to dotnet core, we can design a web api/web mvc management UI and view and manage our tasks, now the crystal management tool in quartz2.x is no longer available, so let's do it ourselves! Haha!

You can view the task list and manage their startup and stop statuses!

Thank you for reading this article!

Back to directory

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.