Java Quartz计划程序未运行
我想在我的web应用程序中每五秒钟发布一条消息,我正在使用Quatz计划此任务。这是我的密码Java Quartz计划程序未运行,java,quartz-scheduler,Java,Quartz Scheduler,我想在我的web应用程序中每五秒钟发布一条消息,我正在使用Quatz计划此任务。这是我的密码 public class InvoiceGenerationSchedular implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent arg0) { System.out.println("Listener is off"); } @Override pub
public class InvoiceGenerationSchedular implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("Listener is off");
}
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("Listener initialized.");
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("dummyJobName", "group1").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();
//schedule it
Scheduler scheduler;
try {
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
class HelloJob implements Job
{
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("Hello Quartz!");
}
}}
我的web.xml是
<servlet>
<servlet-name>InvoiceGenerationServlet</servlet-name>
<servlet-class>InvoiceGenerationSchedular</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InvoiceGenerationServlet</servlet-name>
<url-pattern>/InvoiceGenerationServlet</url-pattern>
</servlet-mapping>
<listener>
<listener-class>InvoiceGenerationSchedular</listener-class>
</listener>
InvoiceGenerationServlet
发票生成时间表
InvoiceGenerationServlet
/InvoiceGenerationServlet
发票生成时间表
现在的问题是每次我运行代码时,它只给我输出
侦听器已初始化
除此之外,我不想让它每隔5秒打印一次“Hello Quartz”,但它什么也不打印。我不明白为什么,也不给我任何例外。我对石英非常陌生,所以我不知道如何解决这个问题。当然,我首先用谷歌搜索了我的问题,但没有得到太多答案。试着像这样在触发器生成器中添加forJob(job)-
TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName", "group1")
.forJob(job)//<--- this line is the new addition
.withSchedule(
CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();
TriggerBuilder
.newTrigger()
.withIdentity(“dummyTriggerName”、“group1”)
我真不敢相信我无缘无故地浪费了两个小时。
我得到了答案,我只是在另一个文件中创建了一个单独的HelloJob类,然后它工作了,但是当我在我的主类中有这个类时它就不工作了。但我不明白原因,这有什么区别??
有人能回答这个问题吗?另外,为了使使用嵌套类成为可能,您必须将其声明为static谢谢您的回答,但我已经得到了答案。我已经贴了,看到答案了吗?你能告诉我原因吗?谢谢,你的答案也很有效。但我不明白为什么它在5秒后打印我的答案两次,而不是一次??好的,我重新启动了eclipse,问题消失了,虽然我找到了我的答案,但你的答案也是正确的。谢谢你的发现。我也花了一个多小时才弄明白。我相信这与反思有关。因为调度器使用反射来创建作业对象的实例。如果它是一个内部类,那么反射就不起作用了,我猜。如果你的作业类在主类中,它必须在公共范围内,这并不能回答这个问题。一旦你有足够的钱,你将能够;相反-