Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz job store is related to active data?

As I understood from Quartz official documentation, AdoStore have to be used for active data storing like JobDataMap and other data. Correct me if I am wrong.

Beside of this clarification I would like to know is there any way to load job and trigger definition from database. Kind of plugin or something like the Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin is for reading from xml file.

If there is not, is it the best solution to implement a custom plugin that will read from database or to use some other approach?

[EDIT] Rastko 8/13/2012 11:16:28 AM

From bellow answers I think that I have not describe you the problem good. I would like to load job and trigger configuration from database. Like it is defined in code or in xml like this :

    <job>
  <name>WriterJob</name>
  <group>CommonGroup</group>
  <description>Test WriteJob</description>
  <job-type>Console.WriteJob, Console</job-type>
</job>
<trigger>
  <simple>
    <name>WriterJobTrigger</name>
    <group>CommonTriggerGroup</group>
    <description>Simple trigger to simply fire sample job</description>
    <job-name>WriterJob</job-name>
    <job-group>CommonGroup</job-group>
    <misfire-instruction>SmartPolicy</misfire-instruction>
    <repeat-count>-1</repeat-count>
    <repeat-interval>10000</repeat-interval>
  </simple>
</trigger>

I want to have this on the same way in DataBase. From generated tables for ADOJobStore I see that these tables are more related for tracking of the currently active jobs - tracking of its state, trigger firing, etc.

I hope that I more clear now. Be free to ask me if you need any additional clarification.

like image 843
Rastko Avatar asked Dec 12 '22 23:12

Rastko


1 Answers

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);
like image 52
sgmoore Avatar answered Dec 30 '22 13:12

sgmoore