Quartz.net同一作业在asp.net中同时执行两次
我正在使用Quartz library 2.3.3.0版在每天上午8点和下午4点的规定时间执行电子邮件发送作业。该网站现已上线,在过去两天中一直在正确的时间发送电子邮件。然而,今天早上8点,这项工作被执行了两次,所有的电子邮件也被发送了两次。为此,我设置了一个日志表来监视在正确时间执行的电子邮件作业的状态。在今天的日志中,每条记录被插入两次。我不知道为什么会这样。下面是我为这个功能运行的代码 JobScheduler.csQuartz.net同一作业在asp.net中同时执行两次,asp.net,quartz.net,Asp.net,Quartz.net,我正在使用Quartz library 2.3.3.0版在每天上午8点和下午4点的规定时间执行电子邮件发送作业。该网站现已上线,在过去两天中一直在正确的时间发送电子邮件。然而,今天早上8点,这项工作被执行了两次,所有的电子邮件也被发送了两次。为此,我设置了一个日志表来监视在正确时间执行的电子邮件作业的状态。在今天的日志中,每条记录被插入两次。我不知道为什么会这样。下面是我为这个功能运行的代码 JobScheduler.cs public class JobScheduler {
public class JobScheduler
{
public static void Start()
{
IJobDetail emailJob = JobBuilder.Create<EmailJob>()
.WithIdentity("job1")
.Build();
ITrigger trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule
(s =>
s.WithIntervalInSeconds(30)
.OnEveryDay()
)
.ForJob(emailJob)
.WithIdentity("trigger1")
.StartNow()
.WithCronSchedule("0 0/1 * * * ?") // Time : Every 1 Minutes job execute
.Build();
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sc = sf.GetScheduler();
sc.ScheduleJob(emailJob, trigger);
sc.Start();
}
}
public void Execute(IJobExecutionContext context)
{
//check for date and time of event
//if starttime and date is tomorrow then send reminder email
//if starttime and date is today then send reminder email
string time = DateTime.Now.ToString("h:mm tt");
if (time == "4:00 PM" || time == "8:00 AM")
{
InsertLogMessage("Entring Email Job Execute Function if "+ time);
GetAllBookings();
}
}
private List<int> GetAllBookingsTimes()
{
InsertLogMessage("Getting all booking times when time is " + DateTime.Now.ToShortTimeString());
List<int> lst = new List<int>();
try
{
//Select for upcoming event of today and tomorrow
conn = Database.getInstance();
conn.Open();
cmd = new SqlCommand("ReminderEmails", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "CheckForReminder");
reader = cmd.ExecuteReader();
while (reader.Read())
{
Times t = new Times();
t.iTimesId = Convert.ToInt32(reader["TimesId"]);
if (!lst.Contains(t.iTimesId))
{
lst.Add(t.iTimesId);
}
}
conn.Close();
}
catch (Exception ex)
{
InsertLogMessage(ex.Message);
}
InsertLogMessage("Returning to Bookings after scheduled times");
return lst;
}
private void GetAllBookings()
{
InsertLogMessage("Getting Booking w.r.t times");
Dictionary<int, List<Booking>> dicofbooking = new Dictionary<int, List<Booking>>();
try {
List<int> timesid = GetAllBookingsTimes();
foreach(var item in timesid)
{
//Get email status confirmation
bool status = GetEmailStatus(item.ToString());
if (status == false)
{
List<Booking> bookinglst = new List<Booking>();
bookinglst = CheckForReminder().Where(p => p.tTimes.iTimesId == item).ToList();
dicofbooking.Add(item, bookinglst);
}
}
blist = new List<Booking>();
bcclst = new List<string>();
foreach (var item in dicofbooking)
{
foreach (var item1 in item.Value)
{
if (item1.tTimes.dtDateTime.Date == DateTime.Now.Date || item1.tTimes.dtDateTime.Date == DateTime.Now.Date.AddDays(1))
{
//Send email at particular time
if (bcclst.Contains(item1.mMember.strEmailAddress) == false)
{
bcclst.Add(item1.mMember.strEmailAddress);
blist.Add(item1);
}
}
}
if (blist.Count > 0)
{
InsertLogMessage("Sending Email for "+ blist[0].eEvent.strEventTitle + " " + blist[0].tTimes.iTimesId);
if (SendEmail(blist[0]))
{
InsertLogMessage("Email sent successfully for " + blist[0].eEvent.strEventTitle + " " + blist[0].tTimes.iTimesId);
//Set Reminder Email Status to true
UpdateEmailStatus(blist[0].tTimes.iTimesId.ToString());
}
}
blist = new List<Booking>();
bcclst = new List<string>();
}
}
catch (Exception ex)
{
InsertLogMessage(ex.Message);
}
}
公共类作业调度器
{
公共静态void Start()
{
IJobDetail emailJob=JobBuilder.Create()
.WithIdentity(“工作1”)
.Build();
ITrigger trigger=TriggerBuilder.Create()。带DailyTimeIntervalsSchedule
(s=>
s、 间隔秒(30)
.每天一次
)
.ForJob(emailJob)
.WithIdentity(“触发器1”)
.StartNow()
.WithCronSchedule(“0 0/1***?”)//时间:每1分钟执行一次作业
.Build();
isSchedulerFactory sf=新StdSchedulerFactory();
isScheduler sc=sf.GetScheduler();
sc.ScheduleJob(emailJob,触发器);
sc.Start();
}
}
EmailJob.cs
public class JobScheduler
{
public static void Start()
{
IJobDetail emailJob = JobBuilder.Create<EmailJob>()
.WithIdentity("job1")
.Build();
ITrigger trigger = TriggerBuilder.Create().WithDailyTimeIntervalSchedule
(s =>
s.WithIntervalInSeconds(30)
.OnEveryDay()
)
.ForJob(emailJob)
.WithIdentity("trigger1")
.StartNow()
.WithCronSchedule("0 0/1 * * * ?") // Time : Every 1 Minutes job execute
.Build();
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sc = sf.GetScheduler();
sc.ScheduleJob(emailJob, trigger);
sc.Start();
}
}
public void Execute(IJobExecutionContext context)
{
//check for date and time of event
//if starttime and date is tomorrow then send reminder email
//if starttime and date is today then send reminder email
string time = DateTime.Now.ToString("h:mm tt");
if (time == "4:00 PM" || time == "8:00 AM")
{
InsertLogMessage("Entring Email Job Execute Function if "+ time);
GetAllBookings();
}
}
private List<int> GetAllBookingsTimes()
{
InsertLogMessage("Getting all booking times when time is " + DateTime.Now.ToShortTimeString());
List<int> lst = new List<int>();
try
{
//Select for upcoming event of today and tomorrow
conn = Database.getInstance();
conn.Open();
cmd = new SqlCommand("ReminderEmails", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "CheckForReminder");
reader = cmd.ExecuteReader();
while (reader.Read())
{
Times t = new Times();
t.iTimesId = Convert.ToInt32(reader["TimesId"]);
if (!lst.Contains(t.iTimesId))
{
lst.Add(t.iTimesId);
}
}
conn.Close();
}
catch (Exception ex)
{
InsertLogMessage(ex.Message);
}
InsertLogMessage("Returning to Bookings after scheduled times");
return lst;
}
private void GetAllBookings()
{
InsertLogMessage("Getting Booking w.r.t times");
Dictionary<int, List<Booking>> dicofbooking = new Dictionary<int, List<Booking>>();
try {
List<int> timesid = GetAllBookingsTimes();
foreach(var item in timesid)
{
//Get email status confirmation
bool status = GetEmailStatus(item.ToString());
if (status == false)
{
List<Booking> bookinglst = new List<Booking>();
bookinglst = CheckForReminder().Where(p => p.tTimes.iTimesId == item).ToList();
dicofbooking.Add(item, bookinglst);
}
}
blist = new List<Booking>();
bcclst = new List<string>();
foreach (var item in dicofbooking)
{
foreach (var item1 in item.Value)
{
if (item1.tTimes.dtDateTime.Date == DateTime.Now.Date || item1.tTimes.dtDateTime.Date == DateTime.Now.Date.AddDays(1))
{
//Send email at particular time
if (bcclst.Contains(item1.mMember.strEmailAddress) == false)
{
bcclst.Add(item1.mMember.strEmailAddress);
blist.Add(item1);
}
}
}
if (blist.Count > 0)
{
InsertLogMessage("Sending Email for "+ blist[0].eEvent.strEventTitle + " " + blist[0].tTimes.iTimesId);
if (SendEmail(blist[0]))
{
InsertLogMessage("Email sent successfully for " + blist[0].eEvent.strEventTitle + " " + blist[0].tTimes.iTimesId);
//Set Reminder Email Status to true
UpdateEmailStatus(blist[0].tTimes.iTimesId.ToString());
}
}
blist = new List<Booking>();
bcclst = new List<string>();
}
}
catch (Exception ex)
{
InsertLogMessage(ex.Message);
}
}
public void执行(IJobExecutionContext上下文)
{
//检查事件的日期和时间
//如果开始时间和日期是明天,则发送提醒电子邮件
//如果开始时间和日期为今天,则发送提醒电子邮件
字符串时间=DateTime.Now.ToString(“h:mmtt”);
如果(时间==“下午4:00”|时间==“上午8:00”)
{
InsertLogMessage(“如果“+时间”,则输入电子邮件作业执行功能);
GetAllBookings();
}
}
私有列表GetAllBookingsTimes()
{
InsertLogMessage(“在时间为“+DateTime.Now.ToSortTimeString()”时获取所有预订时间);
List lst=新列表();
尝试
{
//为今天和明天即将举行的活动选择
conn=Database.getInstance();
conn.Open();
cmd=新的SqlCommand(“提醒邮件”,conn);
cmd.CommandType=System.Data.CommandType.StoredProcess;
cmd.Parameters.AddWithValue(“@Action”、“checkforrementer”);
reader=cmd.ExecuteReader();
while(reader.Read())
{
时间t=新时间();
t、 iTimesId=Convert.ToInt32(读卡器[“TimesId]”);
如果(!lst.包含(t.iTimesId))
{
第一次添加(t.iTimesId);
}
}
康涅狄格州关闭();
}
捕获(例外情况除外)
{
插入日志消息(例如消息);
}
InsertLogMessage(“在预定时间后返回预订”);
返回lst;
}
私人无效GetAllBookings()
{
插入LogMessage(“获得预订w.r.t时间”);
Dictionary dicofbooking=新字典();
试一试{
List timesid=GetAllBookingsTimes();
foreach(timesid中的var项)
{
//获取电子邮件状态确认
bool status=GetEmailStatus(item.ToString());
如果(状态==false)
{
List bookinglst=新列表();
bookinglst=checkforrementer().Where(p=>p.tTimes.iTimesId==item.ToList();
添加(项目,预订列表);
}
}
blist=新列表();
bcclst=新列表();
foreach(dicofbooking中的var项目)
{
foreach(item.Value中的var item1)
{
如果(item1.tTimes.dtDateTime.Date==DateTime.Now.Date | | item1.tTimes.dtDateTime.Date==DateTime.Now.Date.AddDays(1))
{
//在特定时间发送电子邮件
if(bcclst.Contains(item1.mMember.strEmailAddress)==false)
{
bcclst.Add(item1.mMember.strEmailAddress);
b增加(第1项);
}
}
}
如果(blist.Count>0)
{
InsertLogMessage(“为“+blist[0].eEvent.streventittle+”“+blist[0].tTimes.iTimesId发送电子邮件”);
如果(发送电子邮件(blist[0]))
{
InsertLogMessage(“为“+blist[0].eEvent.streventittle+”“+blist[0].tTimes.iTimesId成功发送的电子邮件”);
//将提醒电子邮件状态设置为true
UpdateEmailStatus(blist[0].tTimes.iTimesId.ToString());
}
}
blist=新列表();
bcclst=新列表();
}
}
捕获(例外情况除外)
{
插入日志消息(例如消息);
}
}
我猜您加载了两次配置,一次使用ContextLoaderListener,一次使用DispatcherServlet,导致重复。检查您的配置。此问题是由于在30秒和60秒这两种情况下都执行触发器造成的
s、 间隔秒(30)
及
WithCronSchedule(“0 0/1***?”