How to add work with a trigger to start an instance of the Quartz.Net scheduler without restarting the server?

Is it possible to add a task with a trigger to start the Quartz.NET scheduler instance without restarting the server?

+4
source share
2 answers

A robust implementation with ADOJobStore is to create a custom table for storing tasks and create a class that inherits from ISchedulerPlugin and IJob to automatically create schedules for your work.

Your configuration will look like this:

<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 / task class might 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 . , , :

//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 .

. plugin/job .

+7

?

... :

( ), "Job Populater".

, XML ADO ( sql).

:

<quartz>

    <!-- 
    This configuration is a way to have jobs defined in xml, but will get them written to the database.
    See /questions/1545323/ramjobstore-quartzjobsxml-to-adojobstore-data-move 
    -->


    <add key="quartz.plugin.xml.type" value="Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz" />
    <add key="quartz.plugin.xml.fileNames" value="~/Quartz_Jobs_001.xml" />
    <!-- 
    <add key="quartz.plugin.xml.ScanInterval" value="10" />
    -->

    <add key="quartz.jobStore.type" value="Quartz.Impl.AdoJobStore.JobStoreTX, Quartz" />
    <add key="quartz.jobStore.driverDelegateType" value="Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz"/>
    <add key="quartz.jobStore.dataSource" value="default"/>
    <add key="quartz.dataSource.default.connectionString" value="Server=MyServer\MyInstance;Database=QuartzDB;Trusted_Connection=True;Application Name='quartz_config';"/>
    <add key="quartz.dataSource.default.provider" value="SqlServer-20"/>

</quartz>

( xml), .

: RAMJobStore (quartz_jobs.xml) AdoJobStore

0

Source: https://habr.com/ru/post/1545322/


All Articles