如何添加作业与触发器运行Quartz.Net调度程序实例而不重新启动服务器? [英] How to add job with trigger for running Quartz.Net scheduler instance without restarting server?

查看:455
本文介绍了如何添加作业与触发器运行Quartz.Net调度程序实例而不重新启动服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不重新启动服务器的情况下为运行Quartz.NET调度程序实例添加具有触发器的作业?

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

推荐答案

使用ADOJobStore的实现是使用一个自定义表来存储作业,并创建一个继承自ISchedulerPlugin和IJob的类,以自动为作业创建计划。

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.

您的配置将如下所示:

<add key="quartz.plugin.sqlquartzjobs.type" value="(JobSchedulerPlugin assembly path)" />
<add key="quartz.plugin.sqlquartzjobs.RescanCronExpression" value="0 0/5 * * * ?" /> //plugin should fire every five minutes
<add key="quartz.plugin.sqlquartzjobs.ConnectionString" value="(your connection string)" />

您的插件/作业类可能如下所示:

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);
        }
}



现在JobSchedulerPlugin已经输入了一个触发器QRTZ_TRIGGERS每五分钟有最高优先级。您可以使用它从您的自定义表(我们称之为QUARTZJOBS)加载作业。 QUARTZJOBS可以包含诸如作业名,装配路径,日期,状态等信息,可以用于帮助您有效地创建触发器。它还应包含作业的cron表达式。这是触发器触发时可以做的:

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)

在上面的每个类中,您可以添加自定义验证,以确保触发器在状态或cron表达式发生变化时得到适当处理。

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.

这篇关于如何添加作业与触发器运行Quartz.Net调度程序实例而不重新启动服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆