我正在使用Quartz库2.3.3.0版,每天在所需的上午8点和下午4点执行电子邮件发送作业。该网站现已上线,并且过去两天一直在正确的时间发送电子邮件。但是,今天上午8点发生了该作业执行了两次,并且所有电子邮件也发送了两次。为此,我设置了一个日志表来监视在正确时间执行的电子邮件作业的状态。在今天的日志中,每条记录被插入了两次。我不知道为什么会这样。下面是我正在为此功能运行的代码。
JobScheduler.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();
}
}
EmailJob.cs
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);
}
}
答案 0 :(得分:0)
我猜您将两次加载配置,一次是使用ContextLoaderListener,一次是在DispatcherServlet中导致重复。检查您的配置。
答案 1 :(得分:0)
此问题是由于在30s和60s等条件下都执行了触发器。
s.WithIntervalInSeconds(30)
和
WithCronSchedule(“ 0 0/1 * * *?”)
在工作中还提到了“执行”功能,并将其与日期时间进行比较,可能会在30秒内触发相同的时间。 如下更改触发器
trigger = newTrigger()
.withIdentity("trigger3", "group1")
.withSchedule(cronSchedule("0 0/15 8,16 * * ?"))
.forJob("myJob", "group1")
.build();
将作业“执行”功能更改为
public void Execute(IJobExecutionContext context)
{
InsertLogMessage("Entring Email Job Execute Function if "+ time);
GetAllBookings();
}
该触发器每15分钟触发一次,但仅在上午8点和下午4点执行。 无需仔细检查日期时间。