Thomas Dittmar Thomas Dittmar - 2 months ago 22
Java Question

Start Quarz Scheduler automatically at startup of Tomcat

I'm playing around with the Quarz library but I can't get the scheduler to execute the job. Here the setup:

I start the scheduler at startup (of Tomcat) using the following entry in the web.xml file:

<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>


and the content of the quartz.properties file:

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


The scheduler starts together with Tomcat as I can see from the log output:

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


The problem is the Quarz job is never fired. Here the content of the 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>




and the content of the TestJob.java class:

public class TestJob implements Job {

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


Can someone tell me why the TestJob is never executed?

Thanks

Answer

After a day and a half of trying all sorts of combination of loggers etc. I found the obvious just by reading the jobs.xml file carefully.

As you can see in my original example I named the job and the job group like this:

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

but I didn't reference the name and the group in the trigger setup:

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

Here the correct setup of the jobs.xml file:

<?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>Sending transactional emails to tenants</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>

I also placed the log4j.properties in the 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

I added the logger to the job file:

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!"); 
  }
}

Everything works fine now. Upvote if you find this information useful.

Thanks, Thomas