我正在尝试在tomcat启动时启动石英过程
但它不起作用,这是我的简单代码:
我的ServletContextListener:
封装quartz.test;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class ApplicationStartup implements ServletContextListener {
private static Logger log = Logger.getLogger(ApplicationStartup.class);
public static final String QUARTZ_FACTORY_KEY = "org.quartz.impl.StdSchedulerFactory.KEY";
private StdSchedulerFactory factory = null;
public void contextDestroyed(ServletContextEvent event) {
try
{
factory.getDefaultScheduler().shutdown();
} catch (SchedulerException ex)
{
log.info("catch");
}
}
public void contextInitialized(ServletContextEvent sce) {
System.out.println("THE APPLICATION STARTED");
ServletContext ctx = sce.getServletContext();
JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("dummyJobName", "group1").build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("dummyTriggerName", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();
try {
factory = new StdSchedulerFactory();
ctx.setAttribute(QUARTZ_FACTORY_KEY, factory);
Scheduler scheduler= factory.getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
我的工作:
package quartz.test;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job
{
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Hello Quartz!");
}
}
web.xml
<context-param>
<param-name>quartz:shutdown-on-unload</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>quartz:wait-on-shutdown</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>quartz:start-scheduler-on-load</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>quartz:config-file</param-name>
<param-value>/WEB-INF/classes/quartz.properties</param-value>
</context-param>
<listener>
<listener-class>
org.quartz.ee.servlet.QuartzInitializerListener
</listener-class>
</listener>
编辑:
现在工作开始了,但出现错误:
20/08/2013 11:18:19 org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
严重:Web应用程序[/ monitor]似乎已启动名为[QuartzTest_Worker-1]的线程,但未能停止它。这很可能造成内存泄漏。
在服务器上将应用程序重新部署到tomcat中时
请帮忙
谢谢,
最佳答案
我找到了解决方案,
看起来好像是Quartz作业中的错误取决于。
为了使其正常工作,请在maven中添加以下内容:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
此错误仅在版本2.2.0,在版本2.1.6上正常运行
关于java - 在tomcat 7上使用 quartz 2.2.0,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18316668/