You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring框架下如何在XML配置中为Quartz添加JobListener

问题原因

Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'listeners' of bean class [org.springframework.scheduling.quartz.SchedulerFactoryBean]

出现该报错的核心原因是SchedulerFactoryBean没有名为listeners的可写属性,配置全局生效的JobListener需要使用正确的属性名。

解决方案

1. 修改SchedulerFactoryBean配置

将你原有配置里的listeners属性替换为globalJobListeners,修改后的配置如下:

<bean id="myScheduler"
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="myTrigger" />
        </list>
    </property>
    <!-- 替换原有listeners属性为globalJobListeners -->
    <property name="globalJobListeners">
        <list>
            <ref bean="completionListener" />
        </list>
    </property>
    <property name="quartzProperties">
        <props>
            <prop key="org.quartz.scheduler.skipUpdateCheck">true</prop>
        </props>
    </property>
</bean>
  • 配置globalJobListeners后,你自定义的MyListener会监听该Scheduler下所有Job的执行事件,符合你当前链式触发的需求。
  • 如果你只想让监听器监听指定Job,不需要全局生效,可以使用jobListenerMap属性绑定特定Job即可。

2. 可选优化:避免重复调度报错

你当前代码中每次Job执行完成都会新建同一个jobKey的任务,多次执行会抛出任务已存在的异常,建议在调度前增加存在性判断:

@Override
public void jobWasExecuted(JobExecutionContext jobExecutionContext, JobExecutionException e) {
    log.info("First Job was executed");
    Scheduler scheduler = jobExecutionContext.getScheduler();
    try {
        JobKey jobKey = new JobKey("dummyJobName", "group1");
        // 新增存在性判断,避免重复报错
        if (scheduler.checkExists(jobKey)) {
            scheduler.deleteJob(jobKey);
        }
        JobDetail jobDetail = JobBuilder.newJob(DummyJob.class)
                .withIdentity(jobKey).build();

        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("triggerNextJob", "group1")
                .startNow()
                .build();

        scheduler.scheduleJob(jobDetail, trigger);
        log.info(DummyJob.class.getName() + " has been scheduled executed");
    } catch (Exception ex) {
        log.error("Couldn't chain next Job", ex);
    }
}

内容的提问来源于stack exchange,提问作者shronika

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 20:48:01