Quartz 调度程序无法启动 cron 作业
您好,我正在使用 Quartz 调度程序来触发需要执行大量活动的 cron。 我的代码如下:
在我的 InitServlet 类的 init() 方法中,我定义了我的 TimerServer
public class InitServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
try {
System.out.println("Starting the CRON");
//Set the DSO Handler CRON
TimerServer task = TimerServer.getInstance();
task.setTask();
} catch (Exception ex) {
System.out.println("Failed to start the cron");
ex.printStackTrace();
}
}
在我的 TimerServer 类中,我有以下方法
public void setTask() {
try{
this.setSubscriptionDailyJob();
} catch(SchedulerException ex) {
log.error("SchedulerException: "+ex.getMessage(), ex);
}
private void setSubscriptionDailyJob() throws SchedulerException {
log.info("Step 1 ");
Scheduler scheduler = schedulerFactory.getScheduler();
log.info("Step 2 ");
JobDetail subscriptionJob = new JobDetail("subscription", "subscriptiongroup", SubscriptionDaily.class);
log.info("Step 3 ");
// Initiate CronTrigger with its name and group name
CronTrigger subscriptionCronTrigger = new CronTrigger("subscriptionCronTrigger", "subscriptionTriggerGroup");
try {
log.info("Subscription cron: "+Constants.SUBSCRIPTION_CRON);
// setup CronExpression
CronExpression cexp = new CronExpression(Constants.SUBSCRIPTION_CRON);
// Assign the CronExpression to CronTrigger
subscriptionCronTrigger.setCronExpression(cexp);
} catch (Exception ex) {
log.warn("Exception: "+ex.getMessage(), ex);
}
scheduler.scheduleJob(subscriptionJob, subscriptionCronTrigger);
scheduler.start();
}
在我的 SubscriptionDaily 类中:
public class SubscriptionDaily implements Job {
public void execute(JobExecutionContext arg0) throws JobExecutionException {
//Actions to be performed
}
}
现在检查我的日志,我得到了步骤 1,第 2 步,但不再进一步。
我的代码被困在 TimerServer 类本身。 发送到调度程序的日志是:
17:24:43 INFO [TimerServer]: Step 1
17:24:43 INFO [SimpleThreadPool]: Job execution threads will use class loader of thread: http-8080-1
17:24:43 INFO [SchedulerSignalerImpl]: Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
17:24:43 INFO [QuartzScheduler]: Quartz Scheduler v.1.6.5 created.
17:24:43 INFO [RAMJobStore]: RAMJobStore initialized.
17:24:43 INFO [StdSchedulerFactory]: Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
17:24:43 INFO [StdSchedulerFactory]: Quartz scheduler version: 1.6.5 17:24:43 INFO [TimerServer]: Step 2
我认为缺少日志条目: [QuartzScheduler]:调度程序 DefaultQuartzScheduler_$_NON_CLUSTERED 已启动。
请帮忙。
Hi I am using Quartz scheduler to trigger a cron which needs to perform a host of activities.
My Code for the same is as follow:
In the init() method of my InitServlet class, I am defining my TimerServer
public class InitServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
try {
System.out.println("Starting the CRON");
//Set the DSO Handler CRON
TimerServer task = TimerServer.getInstance();
task.setTask();
} catch (Exception ex) {
System.out.println("Failed to start the cron");
ex.printStackTrace();
}
}
In my TimerServer class I have the following methods
public void setTask() {
try{
this.setSubscriptionDailyJob();
} catch(SchedulerException ex) {
log.error("SchedulerException: "+ex.getMessage(), ex);
}
private void setSubscriptionDailyJob() throws SchedulerException {
log.info("Step 1 ");
Scheduler scheduler = schedulerFactory.getScheduler();
log.info("Step 2 ");
JobDetail subscriptionJob = new JobDetail("subscription", "subscriptiongroup", SubscriptionDaily.class);
log.info("Step 3 ");
// Initiate CronTrigger with its name and group name
CronTrigger subscriptionCronTrigger = new CronTrigger("subscriptionCronTrigger", "subscriptionTriggerGroup");
try {
log.info("Subscription cron: "+Constants.SUBSCRIPTION_CRON);
// setup CronExpression
CronExpression cexp = new CronExpression(Constants.SUBSCRIPTION_CRON);
// Assign the CronExpression to CronTrigger
subscriptionCronTrigger.setCronExpression(cexp);
} catch (Exception ex) {
log.warn("Exception: "+ex.getMessage(), ex);
}
scheduler.scheduleJob(subscriptionJob, subscriptionCronTrigger);
scheduler.start();
}
In my SubscriptionDaily class :
public class SubscriptionDaily implements Job {
public void execute(JobExecutionContext arg0) throws JobExecutionException {
//Actions to be performed
}
}
Now checking my logs, I am getting Step 1, Step 2 but not further.
My code is getting stucked at the TimerServer class itself.
Logs wrt to Scheduler are :
17:24:43 INFO [TimerServer]: Step 1
17:24:43 INFO [SimpleThreadPool]: Job execution threads will use class loader of thread: http-8080-1
17:24:43 INFO [SchedulerSignalerImpl]: Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
17:24:43 INFO [QuartzScheduler]: Quartz Scheduler v.1.6.5 created.
17:24:43 INFO [RAMJobStore]: RAMJobStore initialized.
17:24:43 INFO [StdSchedulerFactory]: Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
17:24:43 INFO [StdSchedulerFactory]: Quartz scheduler version: 1.6.5 17:24:43 INFO [TimerServer]: Step 2
I think a log entry is missing :
[QuartzScheduler]: Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有在我的库中包含 common-collections jar,尽管我的应用程序中没有因此而引发任何错误或异常。所以我很茫然!!
在此之前我从未见过 Java 如此愚蠢。这是java的正确行为还是我对它的期望过高?
我还在我的应用程序中使用 Spring,Spring 提供了一种很好且简单的方法来通过 bean 处理 Quartz 和 Java 的 TimerTask 功能。很少有好的和优雅的教程是:
http://static.springsource.org/spring/docs/ 1.2.9/reference/scheduling.html
http://www.javaranch.com/journal/200711/combining_spring_and_quartz.html
虽然使用bean方法的限制是,你必须在spring xml文件中硬编码cron值,所以我们会失去灵活性。
I had not included common-collections jar in my library though there were no errors or exceptions being thrown anywhere in my application because of it. So I was at loss !!
I have never seen Java being so dumb before this. Is this the right behavior of java or am I expecting too much from it ?
I am also using spring in my application and Spring provides a good and simple way to handle Quartz and Java's TimerTask feature through beans. Few good and elegant tutorials are :
http://static.springsource.org/spring/docs/1.2.9/reference/scheduling.html
http://www.javaranch.com/journal/200711/combining_spring_and_quartz.html
Though the restriction in using the bean approach is, you have to hard code the cron values in the spring xml file, and so we will loose the flexibility.