JobListener issue [JAVA]
I just set up QUARTZ to use with our enterprise applications. The following code snippets are just examples and not taken from the real web applications.
My Trigger/Scheduler class looks like this:
import javax.annotation.PostConstruct;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;
public class TriggerXML {
@PostConstruct
public static void main(String[] args) throws SchedulerException {
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
scheduler.getListenerManager().addJobListener(new HelloJobListener(),
GroupMatcher.jobGroupEquals("fg_jobgroup_01"));
scheduler.start();
}
}
My Listener class looks like this:
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
public class HelloJobListener implements JobListener {
public static final String LISTENER_NAME = "HELLO JOB LISTENER";
private static final Logger log = Logger.getLogger(HelloJobListener.class);
@Override
public String getName() {
return LISTENER_NAME;
}
@Override
public void jobToBeExecuted(JobExecutionContext context) {
String jobName = context.getJobDetail().getKey().toString();
log.info("###############################################");
log.info("JOB IS STARTING");
log.info("Job: " + jobName);
log.info("###############################################");
}
@Override
public void jobExecutionVetoed(JobExecutionContext context) {
log.info("###############################################");
log.info("JOB EXECUTION VETOED");
log.info("###############################################");
}
@Override
public void jobWasExecuted(JobExecutionContext context,
JobExecutionException jobException) {
String jobName = context.getJobDetail().getKey().toString();
log.info("###############################################");
log.info("JOB WAS EXECUTED");
log.info("Job: " + jobName);
log.info("###############################################");
if (!jobException.getMessage().equals("")) {
log.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
log.info("Exception thrown by: " + jobName);
log.info("Exception: " + jobException.getMessage());
log.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
}
The quartz.properties config is the following:
# Basic config
org.quartz.scheduler.instanceName = DBClusteredScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.skipUpdateCheck = true
org.quartz.scheduler.jobFactory.class = org.quartz.simpl.SimpleJobFactory
# Thread Pool config
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 3
# DataSource config
org.quartz.dataSource.quartzDataSource.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.quartzDataSource.URL = jdbc:mysql://localhost:3306/quartz
org.quartz.dataSource.quartzDataSource.user = <user>
org.quartz.dataSource.quartzDataSource.password = <password>
org.quartz.dataSource.quartzDataSource.maxConnections = 8
# Database config for MySQL JDBC connection
org.quartz.jobStore.dataSource = quartzDataSource
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.isClustered = true
# Load configuration for each trigger
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz-config.xml
And finally, my quartz-config.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="1.8">
<!-- JOB 1 CONFIGURATION -->
<schedule>
<job>
<name>job01</name>
<group>fg_jobgroup_01</group>
<description></description>
<job-class>Job01</job-class>
</job>
<trigger>
<cron>
<name>Job01_TRIGGER</name>
<group>PROCESS_LEAD_TRIGGER_GROUP</group>
<job-name>job01</job-name>
<job-group>fg_jobgroup_01</job-group>
<cron-expression>0/20 * * * * ?</cron-expression>
</cron>
</trigger>
</schedule>
<!-- JOB 2 CONFIGURATION -->
<schedule>
<job>
<name>job02</name>
<group>fg_jobgroup_01</group>
<description></description>
<job-class>Job02</job-class>
</job>
<trigger>
<cron>
<name>Job02_TRIGGER</name>
<group>PROCESS_LEAD_TRIGGER_GROUP</group>
<job-name>job02</job-name>
<job-group>fg_jobgroup_01</job-group>
<cron-expression>15 0/2 * * * ?</cron-expression>
</cron>
</trigger>
</schedule>
</job-scheduling-data>
When I run the program (TriggerXML.java), it gives me the following console output:
2015-01-05 15:04:40,224 INFO (org.quartz.xml.XMLSchedulingDataProcessor.java:471).processFile - Parsing XML file: quartz-config.xml with systemId: quartz-config.xml
2015-01-05 15:04:40,443 INFO (org.quartz.xml.XMLSchedulingDataProcessor.java:996).scheduleJobs - Adding 2 jobs, 2 triggers.
2015-01-05 15:04:40,447 INFO (org.quartz.xml.XMLSchedulingDataProcessor.java:1032).scheduleJobs - Replacing job: fg_jobgroup_01.job01
2015-01-05 15:04:40,505 INFO (org.quartz.xml.XMLSchedulingDataProcessor.java:1032).scheduleJobs - Replacing job: fg_jobgroup_01.job02
2015-01-05 15:04:40,737 INFO (org.quartz.core.QuartzScheduler.java:575).start - Scheduler DBClusteredScheduler_$_US-HB-PC-0011420499079608 started.
2015-01-05 15:04:40,856 INFO (com.freightgate.quartz.listener.HelloJobListener.java:21).jobToBeExecuted - ###############################################
2015-01-05 15:04:40,857 INFO (com.freightgate.quartz.listener.HelloJobListener.java:22).jobToBeExecuted - JOB IS STARTING
2015-01-05 15:04:40,857 INFO (com.freightgate.quartz.listener.HelloJobListener.java:23).jobToBeExecuted - Job: fg_jobgroup_01.job01
2015-01-05 15:04:40,857 INFO (com.freightgate.quartz.listener.HelloJobListener.java:24).jobToBeExecuted - ###############################################
2015-01-05 15:04:40,857 INFO (com.freightgate.quartz.jobs.Job01.java:16).execute - @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
2015-01-05 15:04:40,857 INFO (com.freightgate.quartz.jobs.Job01.java:17).execute - @@@@@@@@@@@@ TEST 01 @@@@@@@@@@@@
2015-01-05 15:04:40,857 INFO (com.freightgate.quartz.jobs.Job01.java:18).execute - @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
2015-01-05 15:04:40,858 INFO (com.freightgate.quartz.listener.HelloJobListener.java:41).jobWasExecuted - ###############################################
2015-01-05 15:04:40,858 INFO (com.freightgate.quartz.listener.HelloJobListener.java:42).jobWasExecuted - JOB WAS EXECUTED
2015-01-05 15:04:40,858 INFO (com.freightgate.quartz.listener.HelloJobListener.java:43).jobWasExecuted - Job: fg_jobgroup_01.job01
2015-01-05 15:04:40,858 INFO (com.freightgate.quartz.listener.HelloJobListener.java:44).jobWasExecuted - ###############################################
2015-01-05 15:04:40,859 ERROR (org.quartz.core.ErrorLogger.java:2425).schedulerError - Unable to notify JobListener(s) of Job that was executed: (error will be ignored). trigger= PROCESS_LEAD_TRIGGER_GROUP.Job01_TRIGGER job= fg_jobgroup_01.job01
org.quartz.SchedulerException: JobListener 'HELLO JOB LISTENER' threw exception: null [See nested exception: java.lang.NullPointerException]
at org.quartz.core.QuartzScheduler.notifyJobListenersWasExecuted(QuartzScheduler.java:1987)
at org.quartz.core.JobRunShell.notifyJobListenersComplete(JobRunShell.java:340)
at org.quartz.core.JobRunShell.run(JobRunShell.java:224)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.NullPointerException
at com.freightgate.quartz.listener.HelloJobListener.jobWasExecuted(HelloJobListener.java:46)
at org.quartz.core.QuartzScheduler.notifyJobListenersWasExecuted(QuartzScheduler.java:1985)
What I don't get is why the output I set within the Listener class is written to the log, but then it gives me that exception anyway. Did a lot of Google searches, doesn't seem to be well documented. Also, I haven't found how to set up a listener in the XML config.
Any help is much appreciated!
Best regards!
You obviously get NullPointerException
and even though I don't see the line numbers, this is likely its cause:
if (!jobException.getMessage().equals("")) {
log.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
log.info("Exception thrown by: " + jobName);
log.info("Exception: " + jobException.getMessage());
log.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
If the job is executed successfully then JobExecutionException
will likely be null
. So you need to check it like this:
if (jobException != null) {
// Job failed
}
链接地址: http://www.djcxy.com/p/23632.html
上一篇: Quartz Scheduler:在事件作业元数据中维护已处理的文件列表
下一篇: JobListener问题[JAVA]