How to add job with trigger for running Quartz.NET scheduler instance without restarting server?

前端 未结 2 1379
情深已故
情深已故 2021-01-01 04:03

Is it possible to add job with trigger for running Quartz.NET scheduler instance without restarting server?

2条回答
  •  情话喂你
    2021-01-01 04:47

    A fairly robust implementation with ADOJobStore is to have a custom table to store jobs and create a class that inherits from ISchedulerPlugin and IJob to create schedules for your job automatically.

    Your config will look like this:

    
     //plugin should fire every five minutes
    
    

    Your plugin/job class can look like this:

    public class JobSchedulerPlugin : ISchedulerPlugin, IJob
    {
            //Entry point for plugin, quartz server runs when it starts
            public void Initialize(string pluginName, IScheduler sched)
            {
                Name = pluginName;
                Scheduler = sched;
            }
    
            //Runs after Initialize()
            public void Start()
            {
                    //schedule plugin as a job
                JobDataMap jobData = new JobDataMap();
                jobData["ConnectionString"] = ConnectionString;
    
                IJobDetail job = JobBuilder.Create(this.GetType())
                    .WithDescription("Job to rescan jobs from SQL db")
                    .WithIdentity(new JobKey(JobInitializationPluginJobName, JobInitializationPluginGroup))
                    .UsingJobData(jobData)
                    .Build();
    
                 TriggerKey triggerKey = new TriggerKey(JobInitializationPluginJobTriggerName, JobInitializationPluginGroup);
    
                 ITrigger trigger = TriggerBuilder.Create()
                     .WithCronSchedule(ConfigFileCronExpression)
                     .StartNow()
                     .WithDescription("trigger for sql job loader")
                     .WithIdentity(triggerKey)
                     .WithPriority(1)
                     .Build();
    
                 Scheduler.ScheduleJob(job, trigger);
            }
    }
    

    Now JobSchedulerPlugin has entered a trigger into QRTZ_TRIGGERS that will fire every five minutes with highest priority. You can use it to load jobs from your custom table (let's call it QUARTZJOBS). QUARTZJOBS can contain information such as jobnames, assembly paths, dates, status, etc, anything that can be used to help you create triggers efficiently. It should also contain the cron expression to the job. This is what you can do when the trigger fires:

    //Entry point of every job
    public void Execute(IJobExecutionContext context)
    {
        Scheduler = context.Scheduler;
    
        JobCollection jobs = LoadJobs(context.JobDetail.JobDataMap["ConnectionString"].ToString());
        JobsWithTriggers jobTriggers = CreateTriggers(jobs);
        SchedulerJob(jobTriggers);
    }
    
    //You can use ADO.NET or an ORM here to load job information from the the table
    //and push it into a class. 
    protected JobCollection LoadJobs(string connectionString);
    
    //In this class you can create JobDetails and ITriggers for each job
    //and push them into a custom class
    protected JobsWithTriggers CreateTriggers(jobs);
    
    //Finally here you can schedule the jobs
    protected void ScheduleJobs(jobstriggers)
    

    In each of the classes above you can add custom validation for making sure triggers are handled appropriately if status or cron expression changes.

    With this solution the server will never need to be restarted. The plugin/job class will scan the table and act accordingly.

提交回复
热议问题