同一个Job Quartz.NET的多个触发器

时间:2016-03-04 12:57:43

标签: quartz.net

我有以下代码:

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

我收到以下异常:

  

未处理的类型&#39; Quartz.ObjectAlreadyExistsException&#39;   发生在Quartz.dll

     

其他信息:无法存储作业:&#39; J_Mailing.J_Email&#39;,   因为已经存在这种识别。

但我被告知你可以拥有同一个JOB的多个触发器。也许我做错了什么?

1 个答案:

答案 0 :(得分:16)

将作业添加到计划程序。

然后在创建触发器时,使用ForJob

以下代码经过测试。

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