spring 中 quartz 与 数据库 db 相关操作会出现的问题

本文探讨了Quartz定时任务与Spring框架集成时遇到的问题,包括动态设置cron表达式及Job持久化到数据库的方法。文章分析了使用MethodInvokingJobDetailFactoryBean与JobDetailBean的区别,并解决了在使用过程中出现的异常。

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

与 数据库 db 相关操作有两种:
1.从DB中读cronExpression
2.Job持久化到数据库
对于1:
Quartz 在Spring中动态设置cronExpression
参考 小刀的blog
http://www.blogjava.net/xiaodaoxiaodao/articles/103437.html

通过DB加载cronException

一个类是可以的
2个类会出问题


<bean id="test2" class="cn.edu.hust.mes.service.quartz.Test">
<property name="scheduler" ref="schedulerFactoryBean"/>
</bean>

<bean id="test" class="Test">
<property name="scheduler" ref="schedulerFactoryBean"/>
</bean>

<bean name="schedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTriggerForPLCRead"/>
<ref bean="cronTrigger"/>
</list>
</property>

<property name="configLocation" value="classpath:quartz.properties"/>
</bean>


Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'schedulerFactoryBean': FactoryBean which is currently in creation returned null from getObject

如果写了拦截器的化 还会报这个错 :

Error creating bean with name 'test2': Bean with name 'test2' has been injected into other beans [schedulerJobDetailForPLCRead] in its raw version as part of a circular reference, but has eventually been wrapped (for example as part of auto-proxy creation). This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.


对于2:
MethodInvokingJobDetailFactoryBean 不能持久化
必须用JobDetailBean 需继承QuartzJobBean 不够pojo
使用MethodInvokingJobDetailFactoryBean的化会报:
org.quartz.JobPersistenceException: Couldn't store job: Unable to serialize JobDataMap for insertion into database because the value of property 'methodInvoker' is not serializable: org.springframework.scheduling.quartz.MethodInvoki ngJobDetailFactoryBean [See nested exception: java.io.NotSerializableException: Unable to serialize JobDataMap for insertion into database because the value of property
API上说了
There's a note in the JavaDoc, I'd presume this might be your problem.
JobDetails created via this FactoryBean are not serializable and thus not suitable for persistent job stores. You need to implement your own Quartz Job as a thin wrapper for each case where you want a persistent job to delegate to a specific service method.
http://www.springframework.org/docs/api/org/springframework/scheduling/quartz/MethodInvokingJobDetailFactoryBean.html
用MethodInvokingJobDetailFactoryBean不可持久化 除非加个thin wrapper
但查不到任何资料
但愿spring后续版本有好的方案 现在还是老老实实的用JobDetailBean吧

另:
不可用于web action action 用了session等作用域...
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request? If you are actually operating within a web request and still receive this message,your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值