解决.quartz.ObjectAlreadyExistsException: Unable to store Job : ‘jyGroup.jyJob‘, because one already

本文分析了SpringBoot项目中因重复定义同名定时任务导致的ObjectAlreadyExistsException异常,并提供了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

1. 报错如题:

定时任务出现异常 : org.quartz.ObjectAlreadyExistsException: Unable to store Job : 'jyGroup.jyJob', because one already exists with this identification.
2018-07-19 09:36:48.002  INFO 6128 --- [pool-1-thread-1] gentle.test.UserSyncTask   

2. 原因:

在配置文件中我已经配置了启动一个定时任务:UserSyncTask ,但在 UserSyncTask 类中我又再次声明了另外一个同名同组的任务(由 Show 类实现业务)。  这样这个任务就开启了2次。

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

    <bean id="userSyncTask" class="gentle.test.UserSyncTask"></bean>

    <task:scheduled-tasks>
        <!--每 2 秒执行一次-->
        <task:scheduled ref="userSyncTask" method="cronDepartmentsAndUsersJob" cron="0/2 * * * * ?" />
    </task:scheduled-tasks>

</beans>
package gentle.test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;


public class UserSyncTask {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    SchedulerTest test;
    @Resource
    Show show;


    public void cronDepartmentsAndUsersJob() {

        logger.info("\n\n 定时--开始,当前时间: " + dateFormat().format(new Date()));
        test.addJob();
        logger.info("\n\n 定时--结束,当前时间:" + dateFormat().format(new Date()));
    }

    private SimpleDateFormat dateFormat() {
        return new SimpleDateFormat("HH:mm:ss");
    }
}
package gentle.test;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author silence
 * @date 2018/7/17 11:37
 */
@Service("show")
public class Show implements Job {


    private static Logger _log = LoggerFactory.getLogger(Show.class);

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {

        _log.info("\n\n-------------------------------\n " +
                "It is running and the time is : " + new Date()+
                "\n-------------------------------\n");
    }

}

3. 解决: 直接在 UserSyncTask 中调用 业务实现类 Show 就可以了,不用再把任务完全声明一次:这样就只有一个任务了 。

   或者就在 UserSyncTask 类中声明,main 方法就可以开启定时任务,不用再加配置文件了。

我是 springboot 工程,所以选择第一种方法:

package gentle.test;

import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UserSyncTask {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    SchedulerTest test;
    @Resource
    Show show;

    public void cronDepartmentsAndUsersJob() {

        logger.info("\n\n 定时--开始,当前时间: " + dateFormat().format(new Date()));
//        test.addJob();
        try {
            show.execute(null);
        } catch (JobExecutionException e) {
            e.printStackTrace();
        }
        logger.info("\n\n 定时--结束,当前时间:" + dateFormat().format(new Date()));
    }

    private SimpleDateFormat dateFormat() {
        return new SimpleDateFormat("HH:mm:ss");
    }
}

4. 正常运行:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值