Multiple triggers of same Job Quartz.NET

巧了我就是萌 提交于 2019-12-18 05:47:11

问题


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?


回答1:


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();


来源:https://stackoverflow.com/questions/35796696/multiple-triggers-of-same-job-quartz-net

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