如何使用 spring bean 执行方法
我需要每 5000 毫秒调用一次 MngtImpl 类的 findItemByPIdEndDate() 方法,但似乎没有发生任何事情。我错过了什么吗?
<bean id="findItemByPIdEndDate" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="MngtImpl"/>
<property name="targetMethod" value="findItemByPIdEndDate"/>
<property name="repeatInterval" value="50000"/>
</bean>
@matt b我已经读过其中的一些内容,这里的一切对我来说都是新的..所以我带着这个来了..并且它再次不起作用,这次我错过了什么?
<bean id="findItemByPIdEndDate" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="MngtImpl" />
<property name="targetMethod" value="findItemByPIdEndDate" />
</bean>
<bean id="compareDateTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="findItemByPIdEndDate" />
<property name="startDelay" value="0" />
<property name="repeatInterval" value="50000" />
</bean>
I need findItemByPIdEndDate() method of the MngtImpl class to be invoked every 5000ms, but nothing appears to be happening. Am I missing something?
<bean id="findItemByPIdEndDate" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="MngtImpl"/>
<property name="targetMethod" value="findItemByPIdEndDate"/>
<property name="repeatInterval" value="50000"/>
</bean>
@matt b I've read some of this, everything is new to me here ..so I came with this .. and again its not working, what am I missing this time ?
<bean id="findItemByPIdEndDate" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="MngtImpl" />
<property name="targetMethod" value="findItemByPIdEndDate" />
</bean>
<bean id="compareDateTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="findItemByPIdEndDate" />
<property name="startDelay" value="0" />
<property name="repeatInterval" value="50000" />
</bean>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于此任务,第 23 章. 调度和线程池是你的朋友。也就是说,这是一个简短的总结。
首先,定义您的作业:
现在,您需要使用触发器和 SchedulerFactoryBean 来安排作业。对于触发器,我建议在您的情况下使用
SimpleTriggerBean
:要完成所有操作,请设置
SchedulerFactoryBean
:For this task, the Chapter 23. Scheduling and Thread Pooling is your friend. That said, here is a short summary.
First, define your
Job
:Now, you need to schedule the job using a trigger and a
SchedulerFactoryBean
. For the trigger, I suggest to use aSimpleTriggerBean
in your case:To finalize everything, set up the
SchedulerFactoryBean
:为了让 Quartz 工作,你需要更多的管道。仅单独声明
MethodInvokingJobDetailFactoryBean
不会执行任何操作。然而,Quartz 对此有点大材小用,Java5+ 可以自己做到这一点。我建议阅读 Spring 的 ScheduledExecutorFactoryBean,与 MethodInvokingRunnable,允许您定期调用您的方法。
You need a lot more plumbing than that to make Quartz work. Just declaring the
MethodInvokingJobDetailFactoryBean
on its own will do nothing.However, Quartz is overkill for this, Java5+ can do this on its own. I suggest reading up on Spring's ScheduledExecutorFactoryBean, which in combination with MethodInvokingRunnable, allows you to invoke your method periodically.
到目前为止,您所做的相当于仅实例化 MethodInvokingJobDetailFactoryBean() - 本质上您所做的只是创建了作业。现在您需要对它的计划方式以及触发它的内容进行一些配置。
看一下Spring手册中的部分在石英上。
What you've done so far is the equivalent of only instantiating a MethodInvokingJobDetailFactoryBean() - essentially all you've done is created the Job. Now you need to have some configuration for how it's scheduled, and what triggers it.
Take a look at the section in the Spring manual on Quartz.