我尝试在asp.net MVC项目中每天使用Quartz.net发送电子邮件。但它只工作一次,而且每天都不重复。这是我的代码
public class EmailJob: IJob
{
private static Logger _logger = LogManager.GetCurrentClassLogger();
public void Execute(IJobExecutionContext context)
{
_logger.Info(DateTime.Now.ToString);
}
}
public static void Start()
{
IScheduler _scheduler = StdSchedulerFactory.GetDefaultScheduler();
_scheduler = StdSchedulerFactory.GetDefaultScheduler();
IJobDetail job = JobBuilder.Create<EmailJob>().WithIdentity("theJob_").StoreDurably(true)
.UsingJobData("para", 1).Build();
_scheduler.AddJob(job, true);
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger3")
.ForJob(job)
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(8, 0))
.Build();
_scheduler.ScheduleJob(trigger);
_scheduler.Start();
_logger.Info(DateTime.Now.ToString() + "_scheduler start");
}
我打电话给#34; JobScheduler.Start()&#34;在全球。谁能告诉我我的代码在哪里错了?谢谢提前!
答案 0 :(得分:0)
我认为你遗失了.RepeatForever()
。您的日程安排只会运行一次。
但是,如果邮件以8急剧发送非常重要,那么您应该使用服务器或计划任务等服务器端解决方案,因为无法保证网站可用。
您可以使用在网站上线时每x分钟运行一次的作业。如果apppool回收,网站崩溃或其他什么,当它再次在线时,它会立即发送邮件,并且每10分钟再次发送邮件。
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
IJobDetail job1 = JobBuilder.Create<Job_SendMail>().Build();
ITrigger trigger1 = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInMinutes(10)
.RepeatForever())
.Build();
scheduler.ScheduleJob(job1, trigger1);
public class Job_SendMail: IJob
{
void IJob.Execute(IJobExecutionContext context)
{
//do stuff
}
}
答案 1 :(得分:0)
避免使用.RepeatForever()
,而将.WithDailyTimeIntervalSchedule
与.StartingDailyAt
一起使用
示例:
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
var hour = 12 //Start Time
var minute=15 //Start minute
IJobDetail job = JobBuilder.Create<Job_SendMail>().Build();
var time = DateTime.Now.AddSeconds(10);
ITrigger trigger = TriggerBuilder.Create()
.WithDailyTimeIntervalSchedule
(s =>
s.WithIntervalInHours(24)
.OnEveryDay()
.StartingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(hour, minute, time.Second))
)
.Build();
scheduler.ScheduleJob(job, trigger);