我对如何正确使用调度程序有疑问。
我正在运行一个调度程序,该调度程序会在工作日的上午10:00触发发送电子邮件列表的作业。
调度程序将每1分钟触发一次该作业。但是,发送电子邮件的执行时间大约需要5秒钟。
因此,如果我有20封电子邮件要发送一天,则需要20x5秒= 100秒。
在完成发送20封电子邮件之前,调度程序将在1分钟后触发。
因此,有些电子邮件将不会发送。
请告诉我如何在下一个触发时间之前完全发送电子邮件?
以下是我的示例代码。
//CalendarReminder.java
// Job 1 & Group 1
JobDetail job = new JobDetail();
job.setName("Job1");
job.setJobClass(SendEmailJob.class);
job.setGroup("group1");
//example of adding an excluded day of the week - This excludes sundays from job
//firing schedule
WeeklyCalendar weeklyOff = new WeeklyCalendar();
weeklyOff.setDayExcluded(Calendar.SUNDAY, true);
// Trigger 1
//configure the scheduler time
SimpleTrigger trigger = new SimpleTrigger();
trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(60000); // Trigger every 1 min
trigger.setGroup("group1");
trigger.setName("trigger1");
try{
//schedule the job
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.addCalendar("weeklyOff", weeklyOff, false, true);
scheduler.start();
scheduler.scheduleJob(job, trigger);
}catch(Exception ex){
}
-----------------------------------------------------------------------------
//SendEmailJob.java
try {
//getting database connection to MySQL server
dbCon = DriverManager. getConnection(dbURL, username, password);
//getting PreparedStatment to execute query
stmt = dbCon.prepareStatement(query);
//Resultset returned by query
rs = stmt.executeQuery(query);
while(rs.next()){
Str_Company = rs.getString("erscheduledemail.company");
Str_Dept = rs.getString("erscheduledemail.dept");
Str_EventType = rs.getString("erscheduledemail.eventtype");
Str_OneTimeDate = rs.getString("erscheduledemail.onetimedate");
Str_TimeSend = rs.getString("erscheduledemail.timesend");
Str_EmailFrom = rs.getString("erscheduledemail.emailfrom");
Str_EmailTo = rs.getString("erscheduledemail.emailto");
Str_EmailCCTo = rs.getString("erscheduledemail.emailccto");
Str_EmailSubject = rs.getString("erscheduledemail.emailsubject");
Str_EmailMessage = rs.getString("erscheduledemail.emailmessage");
Str_DeliveryStatus = rs.getString("erscheduledemail.deliverystatus");
Str_ActiveStatus = rs.getString("erscheduledemail.activestatus");
// Retrieve Scheduled Records Date yyyy-mm-dd & Time hh:mm:ss
Int_Year = Integer.parseInt(Str_OneTimeDate.substring(0, 4));
Int_Month = Integer.parseInt(Str_OneTimeDate.substring(5, 7));
Int_Day = Integer.parseInt(Str_OneTimeDate.substring(8,10));
Int_Hour = Integer.parseInt(Str_TimeSend.substring(0, 2));
Int_Min = Integer.parseInt(Str_TimeSend.substring(3, 5));
Int_Sec = Integer.parseInt(Str_TimeSend.substring(6, 8));
// Retrieve Computer System Date & Time
Calendar SysCalen = Calendar.getInstance();
int sys_calYear = SysCalen.get(Calendar.YEAR);
int sys_calMonth = SysCalen.get(Calendar.MONTH) + 1;
int sys_calDay = SysCalen.get(Calendar.DAY_OF_MONTH);
int sys_calHour = SysCalen.get(Calendar.HOUR_OF_DAY);
int sys_calMinute = SysCalen.get(Calendar.MINUTE);
int sys_calSecond = SysCalen.get(Calendar.SECOND);
// If the time match at 10:00 am
if(sys_calYear == Int_Year && sys_calMonth == Int_Month && sys_calDay == Int_Day &&
sys_calHour == Int_Hour && sys_calMinute == Int_Min /*&& sys_calSecond == Int_Sec*/){
System.out.println( " The time now" + String.valueOf(sys_calHour) + ":" +
String.valueOf(sys_calMinute) + ":" + String.valueOf(sys_calSecond));
System.out.println( " company: " +Str_Company);
SendingEmail(Str_EmailFrom,Str_EmailTo,Str_EmailCCTo, Str_EmailSubject, Str_
EmailMessage);
// Update deliverystatus to Yes and ActiveStatus to No
stm = dbCon.createStatement();
sql = "UPDATE erscheduledemail SET deliverystatus = 'Y', activestatus = 'N' "
+
"WHERE company = '"+Str_Company+"' and dept = '"+Str_Dept+"' and onetimedate =
'"+Str_OneTimeDate+"' and eventtype = '"+Str_EventType+"' ";
//Execute INSERT SQL Statement
stm.executeUpdate(sql) ;
}
}
dbCon.close();
} catch (SQLException ex) {
//System.out.println("SQL Exception thrown :" + ex);
System.out.println("SQL Exception thrown :" + ex);
}
谢谢&问候, 戈申