JobListener问题[JAVA]

我只是将QUARTZ设置为与我们的企业应用程序配合使用。 以下代码片段仅仅是示例,并不是来自真实的Web应用程序。

我的触发器/调度器类看起来像这样:

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();
    }
}



我的Listener类看起来像这样:

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

        }

    }

}



quartz.properties配置如下:

# 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



最后,我的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>



当我运行程序(TriggerXML.java)时,它给了我下面的控制台输出:

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)



我没有得到的是为什么我在Listener类中设置的输出写入日志,但是无论如何它给了我这个例外。 Google搜索量很大,似乎没有很好的记录。 另外,我还没有找到如何在XML配置中设置侦听器。

任何帮助深表感谢!

最好的祝福!


你显然会得到NullPointerException ,即使我没有看到行号,这可能是它的原因:

if (!jobException.getMessage().equals("")) {
    log.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    log.info("Exception thrown by: " + jobName);
    log.info("Exception: " + jobException.getMessage());
    log.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}

如果作业成功执行,则JobExecutionException可能为null 。 所以你需要检查它是这样的:

if (jobException != null) {
    // Job failed
}
链接地址: http://www.djcxy.com/p/23631.html

上一篇: JobListener issue [JAVA]

下一篇: run backgroud task using Quartz scheduler