Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple triggers of same Job Quartz.NET

Tags:

quartz.net

I have the following code:

 IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
            scheduler.Start();
            IJobDetail job = JobBuilder.Create<EmailJob>().StoreDurably().WithIdentity("J_Email", "J_Mailing").Build();         
            ITrigger trigger = TriggerBuilder.Create()
                                .WithIdentity("MailTrigger1", "T_Mail1")
                                .StartNow()                                
                                .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionIgnoreMisfires()
                                    .WithIntervalInSeconds(3)
                                    .RepeatForever())
                                .Build();       


            ITrigger triggernew = TriggerBuilder.Create()
                               .WithIdentity("MailTrigger", "T_Mail")
                               .StartNow()                               
                               .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionIgnoreMisfires()
                                   .WithIntervalInSeconds(5)
                                   .RepeatForever())
                               .Build();        
            scheduler.ScheduleJob(job,triggernew);
            scheduler.ScheduleJob(job,trigger);

I am getting the following exception:

An unhandled exception of type 'Quartz.ObjectAlreadyExistsException' occurred in Quartz.dll

Additional information: Unable to store Job: 'J_Mailing.J_Email', because one already exists with this identification.

But I have been told that you can have multiple triggers of the same JOB. Maybe I am doing something wrong?

like image 530
Waqar Ahmed Avatar asked Mar 04 '16 12:03

Waqar Ahmed


1 Answers

Add the Job to the Scheduler.

Then on the creation of the triggers, use ForJob.

The code below is tested.

IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();


//// scheduler.DeleteJob(new JobKey("J_Email"));


IJobDetail job = JobBuilder.Create<MyConcreteJob>().StoreDurably().WithIdentity("J_Email", "J_Mailing").Build();

scheduler.AddJob(job, true /* bool replace */ ); /* Add the given IJob to the Scheduler - with no associated ITrigger.  */


ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity("MailTrigger1", "T_Mail1")
                    .StartNow()
                    .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionIgnoreMisfires()
                        .WithIntervalInSeconds(3)
                        .RepeatForever())
                    .ForJob(job)
                    .Build();


ITrigger triggernew = TriggerBuilder.Create()
                   .WithIdentity("MailTrigger", "T_Mail")
                   .StartNow()
                   .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionIgnoreMisfires()
                       .WithIntervalInSeconds(5)
                       .RepeatForever())
                   .ForJob(job)
                   .Build();


scheduler.ScheduleJob(triggernew);
scheduler.ScheduleJob(trigger);

scheduler.Start();
like image 184
granadaCoder Avatar answered Sep 17 '22 19:09

granadaCoder