当前位置: 首页 > 知识库问答 >
问题:

QUARTZ作业调度器-JobListener问题[JAVA]

戴原
2023-03-14

我只是设置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();
    }
}
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>
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)

共有1个答案

邹毅
2023-03-14

您显然得到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
}
 类似资料:
  • 我使用Quartz调度器执行10个作业。所有这些工作都有自己的导火索。因此它们是异步执行的。 然而,现在我需要这些工作中的2个,以更具体的方式执行。假设Job1每偶数分钟执行一次,Job2每奇数分钟执行一次。现在我想让Job2等待Job1完成。示例:Job1在10:02开始执行。10点03分,Job2的触发器被触发。但是在Job2开始执行之前,如果Job1已经完成,它将查看它。 你知道我怎么解决问

  • 有人能帮帮我吗。我对Quartz调度器很陌生。

  • 我想使用Quartz和Oracle作为JobStore来安排作业。我看到了大多数使用RAMjobstore的示例(当然它不是持久的),但几乎没有任何一个可以帮助我满足Spring boot Quartz Oracle开发的需求。 如有任何指向代码示例或github项目的链接或帮助,将不胜感激。

  • 我正在使用一个石英工作执行特定的任务。 如果另一个Main Job实例仍在运行,我想阻止调度器启动第二个Main Job实例...

  • 我希望使用Quartz调度器,以便应用程序的服务器部分使用调度器创建一个作业并将其存储在JDBCStore中,而UI部分(前端)使用调度器的另一个实例(指向相同的数据库模式)为该作业添加触发器。我以为UI知道作业和组的名称就足够了,因为添加触发器类似于: 不幸的是,这会为job类抛出异常。如有任何帮助,我们将不胜感激。谢谢你。