1

当tomcat启动但它不工作时,我试图启动一个石英进程,这是我的简单代码:我的ServletContextListener:

包石英。测试;

 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时在服务器上请帮助

谢谢,

4

2 回答 2

1

我找到了解决方案,它看起来像 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 版上运行正常

于 2013-08-20T12:17:32.987 回答
0

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

quartz-2.2.x.jar
quartz-jobs-2.2.x.jar
于 2014-09-16T11:15:07.600 回答