У меня есть некоторые запланированные функции в моем веб-приложении, которое работает на Glassfish.
Функции планируется запускать каждый день, каждую неделю, каждый месяц и каждый год. Но запускается только ежедневная запланированная функция. Пожалуйста, посмотрите на код:
public class SchedulerServlet extends GenericServlet {
private static Boolean isLoaded = Boolean.FALSE;
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
}
@Override
public String getServletInfo() {
return null;
}
private void scheduleCronJob(String ejbName, String methodName, String cronExpr) {
final String jobName = "quartz job: " + ejbName + "." + methodName;
System.out.println("SchedulerServlet.scheduleCronJob: ejbName: "+ ejbName +" methodname: "+ methodName +" cronExpr: "+ cronExpr ); //asd
CronTrigger cronTrigger = new CronTrigger(jobName, "Quartz Trigger");
try {
cronTrigger.setCronExpression(cronExpr);
scheduleJob(ejbName, methodName, cronTrigger, "-cron");
} catch (Exception ex) {
System.out.println("STATE : " + "scheduleCronJob fonksiyonunda hata var.") ;
Logger.getLogger(SchedulerServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void scheduleSimpleJob(String ejbName, String methodName, Date time) {
System.out.println("SchedulerServlet.scheduleSimpleJob: ejbName: "+ ejbName +" methodname: "+ methodName +" time: "+ time ); //asd
final String jobName = "quartz job: " + ejbName + "." + methodName;
SimpleTrigger simpleTrigger = new SimpleTrigger(jobName, "Quartz Trigger", time);
try {
scheduleJob(ejbName, methodName, simpleTrigger, "-simple");
} catch (Exception ex) {
System.out.println("STATE : " + "scheduleSimpleJob fonksiyonunda hata var.") ;
Logger.getLogger(SchedulerServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void scheduleJob (String ejbName, String methodName, Trigger trigger, String tail) throws SchedulerException {
try {
final String jobName = "quartz job: " + ejbName + "." + methodName + tail;
JobDetail jd = new JobDetail(jobName , "Quartz Job", GFv3EJBInvokerJob.class);
jd.getJobDataMap().put("ejb", "java:module/"+ejbName);
jd.getJobDataMap().put("method", methodName);
System.out.println("SchedulerServlet.scheduleJob: ejbName: "+ ejbName +" methodname: "+ methodName +" trigger: "+ trigger +" tail: "+tail); //asd
Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
sched.scheduleJob(jd, trigger);
Logger.getLogger(SchedulerServlet.class.getName()).log(Level.INFO, null, jobName + " is scheduled...");
} catch ( Exception ex ) {
System.out.println("STATE : " + "scheduleJob fonksiyonunda hata var.") ;
}
}
@Override
public synchronized void init(ServletConfig config) throws ServletException {
if (!isLoaded) {
super.init(config);
System.out.println("SchedulerServlet.init: "+config.toString()); //asd
scheduleCronJob("OFReportTimeoutService", "ofTimeout", "*/15 * * * * ?");//Seconds, Minutes, Hours, Day of month, Month, Day of week, Year
scheduleCronJob("ScheduledReportTimeoutService", "protectionInformationTimeout", "0 0 */3 * * ?");
Calendar now = new GregorianCalendar();
if (!(now.get(Calendar.HOUR)%3 == 2 && now.get(Calendar.MINUTE) > 40)) {
now.add(Calendar.MINUTE, 5);
scheduleSimpleJob("ScheduledReportTimeoutService", "protectionInformationTimeout", now.getTime());
}
scheduleCronJob("ScheduledReportTimeoutService", "dailyTimeout", "0 0 4 * * ?");
scheduleCronJob("ScheduledReportTimeoutService", "weeklyTimeout", "0 15 4 ? * MON");
scheduleCronJob("ScheduledReportTimeoutService", "monthlyTimeout", "0 0 4 1 * ?");
scheduleCronJob("ScheduledReportTimeoutService", "yearlyTimeout", "0 0 4 1 1 ?");
isLoaded = Boolean.TRUE;
}
else {
System.out.println("STATE : " + "synchronized isn't running. isLoaded = TRUE") ;
System.out.println("STATE : " + "OFReportTimeoutService and protectionInformationTimeout services can't run.") ;
System.out.println("STATE : " + "synchronized isn't running. ") ;
Logger.getLogger(SchedulerServlet.class.getName()).log(Level.SEVERE, null, " isn't scheduled...");
}
}
}
Но самое интересное, когда я меняю порядок линий цветения
scheduleCronJob("ScheduledReportTimeoutService", "dailyTimeout", "0 0 4 * * ?");
scheduleCronJob("ScheduledReportTimeoutService", "weeklyTimeout", "0 15 4 ? * MON");
scheduleCronJob("ScheduledReportTimeoutService", "monthlyTimeout", "0 0 4 1 * ?");
scheduleCronJob("ScheduledReportTimeoutService", "yearlyTimeout", "0 0 4 1 1 ?");
Как это
scheduleCronJob("ScheduledReportTimeoutService", "weeklyTimeout", "0 15 4 ? * MON");
scheduleCronJob("ScheduledReportTimeoutService", "dailyTimeout", "0 0 4 * * ?");
scheduleCronJob("ScheduledReportTimeoutService", "monthlyTimeout", "0 0 4 1 * ?");
scheduleCronJob("ScheduledReportTimeoutService", "yearlyTimeout", "0 0 4 1 1 ?");
Все функции срабатывают, когда приходит время. Есть ли в этом смысл. Просто изменив порядок двух функций, вы заставите код работать. Я что-то упустил?