2016-09-21 75 views
1

我正在玩Quarz library,但我無法讓調度程序執行該作業。這裏的設置:在Tomcat啓動時自動啓動Quarz Scheduler

<servlet> 
    <servlet-name>QuartzInitializer</servlet-name> 
    <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class> 
    <init-param> 
    <param-name>config-file</param-name> 
    <param-value>quartz/quartz.properties</param-value> 
    </init-param> 
    <init-param> 
    <param-name>shutdown-on-unload</param-name> 
    <param-value>true</param-value> 
    </init-param> 
    <load-on-startup>2</load-on-startup> 
</servlet> 

quartz.properties文件的內容:

我在使用中的web.xml文件以下項(Tomcat的)啓動時啓動調度程序:

org.quartz.scheduler.interruptJobsOnShutdownWithWait=true 

#=================================================== 
# Configure the ThreadPool 
#=================================================== 

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool 
org.quartz.threadPool.threadCount = 10 
org.quartz.threadPool.threadPriority = 5 
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true 

#=================================================== 
# Configure the Job 
#=================================================== 

org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin 
org.quartz.plugin.jobInitializer.fileNames = quartz/jobs.xml 
org.quartz.plugin.jobInitializer.failOnFileNotFound = true 
org.quartz.plugin.jobInitializer.scanInterval = 10 
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false 

調度和Tomcat一起開始,我可以從日誌輸出看到:

INFO: QuartzInitializer: Scheduler has been started... 
Sep 21, 2016 12:50:49 PM org.apache.catalina.core.ApplicationContext log 
INFO: QuartzInitializer: Storing the Quartz Scheduler Factory in the servlet context at key: org.quartz.impl.StdSchedulerFactory.KEY 
Sep 21, 2016 12:50:49 PM org.apache.coyote.AbstractProtocol start 
INFO: Starting ProtocolHandler ["http-bio-8080"] 
Sep 21, 2016 12:50:49 PM org.apache.catalina.startup.Catalina start 
INFO: Server startup in 2373 ms 

問題是,石器工作從未解僱。在這裏,jobs.xml內容:

<?xml version='1.0' encoding='utf-8'?> 
<job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd" version="1.8"> 
    <schedule> 
    <job> 
     <name>Job</name> 
     <group>Job</group> 
     <description>Testing the job</description> 
     <job-class>dev.dit.scheduler.TestJob</job-class> 
    </job> 
    <trigger> 
     <cron> 
      <name>Trigger</name> 
      <job-name>Trigger</job-name> 
      <job-group>TriggerGroup</job-group> 
      <cron-expression>0 0/5 * * * ?</cron-expression> 
     </cron> 
    </trigger> 
</schedule> 

和TestJob.java類的內容:

public class TestJob implements Job { 

    @Override 
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { 
     System.out.println("Job executed!"); 
    } 
} 

有人能告訴我爲什麼從不執行TestJob?

感謝

回答

1

了一天之後和嘗試各種記錄器等相結合的一半,我發現了明顯的只是仔細閱讀jobs.xml文件。

正如你可以在我原來的例子看,我命名爲這樣的作業和作業組:

<name>Job</name> 
<group>Job</group> 

,但我並沒有提及名字,並在觸發設置羣組:

<job-name>Trigger</job-name> 
<job-group>TriggerGroup</job-group> 

這裏jobs.xml文件的正確設置:

<?xml version='1.0' encoding='utf-8'?> 

<job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_2_0.xsd" 
    version="2.0"> 

<pre-processing-commands> 
    <delete-jobs-in-group>PROCESS_LEAD_JOB_GROUP</delete-jobs-in-group> 
    <delete-triggers-in-group>PROCESS_LEAD_TRIGGER_GROUP</delete-triggers-in-group> 
</pre-processing-commands> 

<schedule> 

    <job> 
     <name>Job</name> 
     <group>Jobs</group> 
     <description>Testing the job</description> 
     <job-class>dev.dit.scheduler.TestJob </job-class> 
    </job> 

    <trigger> 
     <cron> 
      <name>Trigger</name> 
      <job-name>Job</job-name> 
      <job-group>Jobs</job-group> 
      <cron-expression>0/10 * * * * ?</cron-expression> 
     </cron> 
    </trigger> 

</schedule> 

我也被放置在log4j.properties在classpath:

log4j.rootLogger = INFO, STDOUT, FILE 

log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender 
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout 
log4j.appender.STDOUT.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n 

我添加了記錄到工作文件:

package dev.dit.scheduler; 

import org.apache.log4j.Logger; 
import org.quartz.Job; 
import org.quartz.JobExecutionContext; 
import org.quartz.JobExecutionException; 

public class TestJob implements Job { 

    final static Logger logger = Logger.getLogger(TestJob.class); 

    @Override 
    public void execute(JobExecutionContext context) throws JobExecutionException { 

    logger.info("Job executed!"); 
    } 
} 

現在一切工作正常。 Upvote,如果你覺得這個信息有用。

謝謝, Thomas