-1

I really don't know if this is possible in Quartz.net but I was developing a class library to be registered in quartz.net as a crontrigger. This class library has two functions:

  1. It will read a table in a sql server db named DBx if the triggergroup is equal to the value "internal". This should occur every 1 minute.

  2. It will read a table in a sql server db named DBy if the triggergroup is equal to the value "external". This should occur every Mon-Fri from 8am to 5pm.

I created 2 jobs (2 records appeared at jobs_detail table) and 2 triggers (2 records appeared at the triggers and cron_triggers table respectively) with different schedules and with the corresponding triggergroup associated but pointing to the same dll.

What happen is that when I started the scheduler, even though they have different trigger names and job names, the "internal" job is also firing the "external" job even though it is not supposed to be fired unil Mon-Fri 8 to 5pm.

Is there a way to accomplish this without the need of create two classes that basically has the same code except for the fact that when they are going to read from a repository is when they will choose one DB or another based on an "internal" or an "external" parameter?

walen
  • 7,103
  • 2
  • 37
  • 58
Ray
  • 483
  • 4
  • 17

1 Answers1

0

I solved this by creating a new VS.NET solution, linked the other projects and implemented only the execute method for that solution. The compilation created two different dlls with the same code that now I can register them separately.

Updated: After analyzing why someone down voted this solution I made a research to better understand triggers and jobs and finally I found what I was looking for. The best, and most elegant answer, is using JobData in the trigger with the key I need to decide if I want to read from table A or B. So the code for the trigger should be the following:

ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
                                                            .WithIdentity(schedulerConfigurationEntity.TriggerName, schedulerConfigurationEntity.TriggerGroup)
                                                            .WithCronSchedule(schedulerConfigurationEntity.Schedule, x => x.WithMisfireHandlingInstructionFireAndProceed())
                                                            .ForJob(schedulerConfigurationEntity.JobName, schedulerConfigurationEntity.JobGroup)
                                                            .WithPriority(schedulerConfigurationEntity.Priority)
                                                            .UsingJobData("TriggerGroup", schedulerConfigurationEntity.TriggerGroup)
                                                            .Build();

And to get the parameter I did the following:

JobDataMap dataMap = context.Trigger.JobDataMap;
string triggerGroup = dataMap.GetString("TriggerGroup");

With the triggerGroup I can, internally, decide to which table I should read from. Obviuosly all that information is in the Job Definition in the Quartz Job table. Now there are two jobs, using the same class library, with different trigger group values defined and using the same code I can read from Table A or B dynamically.

Ray
  • 483
  • 4
  • 17