使用石英2.2.0与tomcat 7

时间:2013-08-19 14:44:24

标签: java java-ee tomcat tomcat7 quartz-scheduler

我正在尝试在tomcat启动时启动石英进程 但它不起作用,这是我的简单代码: 我的ServletContextListener:

package 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时,在服务器上

请帮忙

谢谢,

2 个答案:

答案 0 :(得分:1)

我找到了解决方案, 它看起来像Quartz作业中的一个bug。 为了使其工作,这取决于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,它正常运行

答案 1 :(得分:0)

要运行此示例,您至少需要Quartz中的两个jar文件:

quartz-2.2.x.jar
quartz-jobs-2.2.x.jar