Quartz配置两个定时任务
来源:13-4 dao的迁移上
慕圣4425397
2019-04-29
老师,Quartz配置两个定时任务,出现
Field hourJobDetailFactory in com.bjcdt.roadCenter.config.quatrz.QuartzConfiguration required a single bean, but 2 were found:
- &hourJobDetailFactory: defined by method 'createHourJobDetail' in class path resource [com/bjcdt/roadCenter/config/quatrz/QuartzConfiguration.class]
- &historyJobDetailFactory: defined by method 'createHistoryJobDetail' in class path resource [com/bjcdt/roadCenter/config/quatrz/QuartzConfiguration.class]
具体代码如下:
@Configuration
public class QuartzConfiguration {
@Autowired
private HourInsertService hourInsertService;
@Autowired
private HistoryInsertService historyInsertService;
@Autowired
private MethodInvokingJobDetailFactoryBean hourJobDetailFactory;
@Autowired
private MethodInvokingJobDetailFactoryBean historyJobDetailFactory;
@Autowired
private CronTriggerFactoryBean hourTriggerFactory;
@Autowired
private CronTriggerFactoryBean historyTriggerFactory;
/**
* 1、创建hourJobDetailFactory并返回
*
* @return
*/
@Bean(name = "hourJobDetailFactory")
public MethodInvokingJobDetailFactoryBean createHourJobDetail() {
// new出JobDetailFactory对象,此工厂主要用来制作一个jobDetail,即制作一个任务。
// 由于我们所做的定时任务根本上讲其实就是执行一个方法。所以用这个工厂比较方便。
MethodInvokingJobDetailFactoryBean hourJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
// 设置jobDetail的名字
hourJobDetailFactoryBean.setName("hour_trans_flow");
// 设置jobDetail的组名
hourJobDetailFactoryBean.setGroup("hour_trans_flow_group");
// 对于相同的JobDetail,当指定多个Trigger时, 很可能第一个job完成之前,第二个job就开始了。
// 指定concurrent设为false,多个job不会并发运行,第二个job将不会在第一个job完成之前开始。
hourJobDetailFactoryBean.setConcurrent(false);
// 指定运行任务的类
hourJobDetailFactoryBean.setTargetObject(hourInsertService);
// 指定运行任务的方法
hourJobDetailFactoryBean.setTargetMethod("insert");
return hourJobDetailFactoryBean;
}
/**
* 2、创建historyJobDetailFactory并返回
*
* @return
*/
@Bean(name = "historyJobDetailFactory")
public MethodInvokingJobDetailFactoryBean createHistoryJobDetail() {
MethodInvokingJobDetailFactoryBean historyJobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
// 设置jobDetail的名字
historyJobDetailFactoryBean.setName("history_trans_flow");
// 设置jobDetail的组名
historyJobDetailFactoryBean.setGroup("history_trans_flow_group");
// 对于相同的JobDetail,当指定多个Trigger时, 很可能第一个job完成之前,第二个job就开始了。
// 指定concurrent设为false,多个job不会并发运行,第二个job将不会在第一个job完成之前开始。
historyJobDetailFactoryBean.setConcurrent(false);
// 指定运行任务的类
historyJobDetailFactoryBean.setTargetObject(historyInsertService);
// 指定运行任务的方法
historyJobDetailFactoryBean.setTargetMethod("insert");
return historyJobDetailFactoryBean;
}
/**
* 创建cronTriggerFactory并返回
*
* @return
*/
@Primary
@Bean("hourTriggerFactory")
public CronTriggerFactoryBean createHourInsertServiceTrigger() {
// 创建triggerFactory实例,用来创建trigger
CronTriggerFactoryBean hourTriggerFactory = new CronTriggerFactoryBean();
// 绑定jobDetail
hourTriggerFactory.setJobDetail(hourJobDetailFactory.getObject());
// 设定cron表达式
hourTriggerFactory.setName("hour_trans_flow_trigger");
hourTriggerFactory.setGroup("hour_trans_flow_group");
hourTriggerFactory.setJobDetail(hourJobDetailFactory.getObject());
hourTriggerFactory.setCronExpression("0/5 * * * * ? ");
return hourTriggerFactory;
}
/**
* *创建cronTriggerFactory并返回
*
* @return
*/
@Bean("historyTriggerFactory")
public CronTriggerFactoryBean createHistoryInsertServiceTrigger() {
// 创建triggerFactory实例,用来创建trigger
CronTriggerFactoryBean historyTriggerFactory = new CronTriggerFactoryBean();
// 设置triggerFactory的名字
historyTriggerFactory.setName("history_trans_flow_trigger");
// 设置triggerFactory的组名
historyTriggerFactory.setGroup("history_trans_flow_group");
// 绑定jobDetail
historyTriggerFactory.setJobDetail(historyJobDetailFactory.getObject());
// 设定cron表达式
historyTriggerFactory.setCronExpression("0/10 * * * * ? ");
return historyTriggerFactory;
}
/**
* 创建调度工厂并返回
*
* @return
*/
@Bean("hourSchedulerFactory")
public SchedulerFactoryBean createHourSchedulerFactory() {
SchedulerFactoryBean hourSchedulerFactory = new SchedulerFactoryBean();
hourSchedulerFactory.setTriggers(hourTriggerFactory.getObject(),historyTriggerFactory.getObject());
return hourSchedulerFactory;
}
}
写回答
1回答
-
同学好,这里由于和咱们视频不一样,只能给出意见哈,这里同学使用@Autowired注入,但是报了required a single bean, but 2 were found。也就是说hourJobDetailFactory有两个实现,
可以尝试加入@Qualifier("hourJobDetailFactory")标识看看,这里是by type去注入的,看看by name注入试试
012019-05-03
相似问题