Quartz job store is related to active data?

柔情痞子 提交于 2019-12-04 09:48:27

I would like to know is there any way to load job and trigger definition from database.

If you are looking to retrieve a list of jobs from the database, you can do something like :

  Quartz.IScheduler scheduler ; 


   ....

  var details = (from groupName in scheduler.GetJobGroupNames()
                 from jobKey in scheduler.GetJobKeys(
                      Quartz.Impl.Matchers.GroupMatcher<Quartz.JobKey>
                      .GroupEquals(groupName))
                select new 
                { 
                    GroupName = groupName, 
                    JobName = jobKey.Name  , 
                    triggers = scheduler.GetTriggersOfJob(jobKey) 
                }
               );

This syntax is for Quartz 2.0.

If you are building a separate program from the program that is actually executing the jobs, then you just create a scheduler with the same details but don't call scheduler.Start()

If you are looking to add new jobs to your database you can do something like :

(where SimpleJob is the C# class name of your job)

string jobName = ...
string triggerName = ...
string cronExpression = ...  
Quartz.IScheduler scheduler = ... 


Quartz.IJobDetail jobDetail = Quartz.JobBuilder.Create<SimpleJob>()
                    .WithIdentity(jobName)
                    .StoreDurably()
                    .Build();

Quartz.ITrigger trigger = Quartz.TriggerBuilder.Create()
                    .WithIdentity(triggerName)
                    .WithSchedule(Quartz.CronScheduleBuilder.CronSchedule(cronExpression)
                    .ForJob(jobName)
                    .Build();

scheduler.ScheduleJob(jobDetail, trigger);

If you are looking to add a job to the database without attaching a trigger

Quartz.IJobDetail jobDetail = Quartz.JobBuilder.Create<SimpleJob>()
                    .WithIdentity(jobName)
                    .StoreDurably()
                    .Build();

scheduler.AddJob(jobDetail, false)

If you are looking to schedule execute a one-off existing job, then

   Quartz.ITrigger trigger1 = Quartz.TriggerBuilder.Create()
                        .WithIdentity(triggerName)
                        .WithSchedule(Quartz.SimpleScheduleBuilder.Create())
                        .ForJob(jobName)
                        .Build();

   scheduler.ScheduleJob(trigger1);

I am using Quartz for java, but since the basic logic is the same I can say that using XML for task schedulers is not a good idea. At every status change of a job, you will need to change the xml file. Using databases (AdoJobStore in your case) might be more efficient.

have a look at XMLSchedulingDataProcessorPlugin implementation. basically you need to implement ISchedulerPlugin and add you logic to load jobs.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!