Spring整合quartz定时任务service注入问题

KingSen_Yale 2018-05-29 04:24:25
在SpringMVC项目整合定时任务quart的时候,需要用到service,直接用@Resource 注入不了。

在网上参考了大量方法,试过了,用到 AdaptableJobFactory这个类。

自定义一个类:

public class JobFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;

@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
// 调用父类的方法
Object jobInstance = super.createJobInstance(bundle);
System.out.println("capableBeanFactory----------" + capableBeanFactory);
// 进行注入
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}


然后在spring中配置:

<!-- 定时任务的factorybean,配置其他config -->
<bean id="jobFactory" class="com.scm.util.common.JobFactory"></bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobFactory" ref="jobFactory"/>
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>

</bean>



这样配置,在自定义类JobFactory里面报错。
我打印出来的capableBeanFactory 是 null
capableBeanFactory----------null
16:06:10.031 [startQuertz_QuartzSchedulerThread] ERROR org.quartz.core.ErrorLogger - An error occured instantiating job to be executed. job= 'BL_MAT_INFO.BL_MAT_INFO'
org.quartz.SchedulerException: Job instantiation failed
at org.springframework.scheduling.quartz.AdaptableJobFactory.newJob(AdaptableJobFactory.java:61) ~[spring-context-support-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.springframework.scheduling.quartz.AdaptableJobFactory.newJob(AdaptableJobFactory.java:48) ~[spring-context-support-4.0.6.RELEASE.jar:4.0.6.RELEASE]
at org.quartz.core.JobRunShell.initialize(JobRunShell.java:127) ~[quartz-2.2.3.jar:na]
at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:375) [quartz-2.2.3.jar:na]
Caused by: java.lang.NullPointerException: null
at com.scm.util.common.JobFactory.createJobInstance(JobFactory.java:18) ~[classes/:na]
at org.springframework.scheduling.quartz.AdaptableJobFactory.newJob(AdaptableJobFactory.java:57) ~[spring-context-support-4.0.6.RELEASE.jar:4.0.6.RELEASE]
... 3 common frames omitted
16:06:10.033 [startQuertz_QuartzSchedulerThread] INFO org.quartz.simpl.RAMJobStore - All triggers of Job BL_MAT_INFO.BL_MAT_INFO set to ERROR state.
16:06:10.033 [startQuertz_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
...全文
1445 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
maradona1984 2018-05-31
  • 打赏
  • 举报
回复
引用 14 楼 KingSen_Yale 的回复:
别告诉我楼主你到现在还没搞定? 实在不行就翻翻源码吧,对框架理解不够很容易犯低级错误的 有些问题要全局考虑的,你提供的信息太少,我也无能为力,最熟悉你项目的人还是你自己 先百度下别人读源码的经验,缩小范围
KingSen_Yale 2018-05-31
  • 打赏
  • 举报
回复
KingSen_Yale 2018-05-30
  • 打赏
  • 举报
回复
引用 10 楼 maradona1984 的回复:
[quote=引用 9 楼 KingSen_Yale 的回复:] [quote=引用 8 楼 maradona1984 的回复:] 注解扫描那个开启没
没听懂你说什么[/quote] 我一般遇到不懂的会去搜索下[/quote] 我意思是这个是肯定开启了。注意审题
maradona1984 2018-05-30
  • 打赏
  • 举报
回复
引用 9 楼 KingSen_Yale 的回复:
[quote=引用 8 楼 maradona1984 的回复:] 注解扫描那个开启没
没听懂你说什么[/quote] 我一般遇到不懂的会去搜索下
KingSen_Yale 2018-05-30
  • 打赏
  • 举报
回复
引用 8 楼 maradona1984 的回复:
注解扫描那个开启没
没听懂你说什么
maradona1984 2018-05-30
  • 打赏
  • 举报
回复
注解扫描那个开启没
KingSen_Yale 2018-05-30
  • 打赏
  • 举报
回复
KingSen_Yale 2018-05-29
  • 打赏
  • 举报
回复
KingSen_Yale 2018-05-29
  • 打赏
  • 举报
回复
引用 4 楼 xwn_2016 的回复:
把那个属性在配置中注入试试
按道理这个属性是不用手动注入的,
KingSen_Yale 2018-05-29
  • 打赏
  • 举报
回复
引用 2 楼 dotnetstudio 的回复:
你试试这个 1. <task:scheduled-tasks> 2. <task:scheduled ref="myJob" method="execute" initial-delay="5000" fixed-delay="3600000"/> 6. </task:scheduled-tasks>
你这个是写死的,我这个是动态的,不一样
非专业IT男 2018-05-29
  • 打赏
  • 举报
回复
https://blog.csdn.net/qq_35556460/article/details/80338001 我写的,之前写过spring+quartz定时任务,遇到了的问题。你可以看看对你有帮助没有。
xwn_2016 2018-05-29
  • 打赏
  • 举报
回复
把那个属性在配置中注入试试
KeepSayingNo 2018-05-29
  • 打赏
  • 举报
回复
你试试这个 1. <task:scheduled-tasks> 2. <task:scheduled ref="myJob" method="execute" initial-delay="5000" fixed-delay="3600000"/> 6. </task:scheduled-tasks>
KingSen_Yale 2018-05-29
  • 打赏
  • 举报
回复

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧