前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。
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/beanshttp://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());@ResourceSchedulerTest test;@ResourceShow 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);@Overridepublic 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());@ResourceSchedulerTest test;@ResourceShow 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. 正常运行: