如何使用 spring bean 执行方法

发布于 2024-08-08 18:58:41 字数 1076 浏览 6 评论 0原文

我需要每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随波逐流 2024-08-15 18:58:41

对于此任务,第 23 章. 调度和线程池是你的朋友。也就是说,这是一个简短的总结。

首先,定义您的作业:

<bean id="findItemByPIdEndDate" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="MngtImpl"/>
  <property name="targetMethod" value="findItemByPIdEndDate"/>
</bean>

现在,您需要使用触发器和 SchedulerFactoryBean 来安排作业。对于触发器,我建议在您的情况下使用 SimpleTriggerBean

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
  <!-- see the example of method invoking job above -->
  <property name="jobDetail" ref="findItemByPIdEndDate" />
  <!-- 10 seconds -->
  <property name="startDelay" value="10000" />
  <!-- repeat every 50 seconds -->
  <property name="repeatInterval" value="50000" />
</bean>

要完成所有操作,请设置 SchedulerFactoryBean

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

For this task, the Chapter 23. Scheduling and Thread Pooling is your friend. That said, here is a short summary.

First, define your Job:

<bean id="findItemByPIdEndDate" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="MngtImpl"/>
  <property name="targetMethod" value="findItemByPIdEndDate"/>
</bean>

Now, you need to schedule the job using a trigger and a SchedulerFactoryBean. For the trigger, I suggest to use a SimpleTriggerBean in your case:

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
  <!-- see the example of method invoking job above -->
  <property name="jobDetail" ref="findItemByPIdEndDate" />
  <!-- 10 seconds -->
  <property name="startDelay" value="10000" />
  <!-- repeat every 50 seconds -->
  <property name="repeatInterval" value="50000" />
</bean>

To finalize everything, set up the SchedulerFactoryBean:

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
    <list>
      <ref bean="simpleTrigger" />
    </list>
  </property>
</bean>
阳光下的泡沫是彩色的 2024-08-15 18:58:41

为了让 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.

此刻的回忆 2024-08-15 18:58:41

到目前为止,您所做的相当于仅实例化 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文