【问题标题】:Spring Task Executor Scheduled too many instances of the taskSpring Task Executor 调度了太多的任务实例
【发布时间】:2011-11-09 10:01:01
【问题描述】:

我有一个简单的 Spring Scheduled Taks 定义如下:

<context:component-scan base-package="com/test"/>
<task:scheduled-tasks>
    <task:scheduled ref="myScheduler" method="doMyTask" fixed-rate="300000"/>
</task:scheduled-tasks>

<task:scheduler id="taskScheduler" pool-size="1"/>

<task:executor id="executorWithPoolSizeRange"
               pool-size="1"
               queue-capacity="100"/>

<bean id="cleanupClass" class="com.test.CleanupClass">
   <property name="myProperty" value="3600"/>
</bean>

我想每 5 分钟同步运行一个线程。但是,我得到的是每 5 分钟连续运行五个任务实例。有谁知道上面的 XML 描述中是否缺少某些内容?

我使用 @Scheduled 注释得到了我想要的行为,但我不想将注释用于固定速率,因为我希望它可以在代码之外进行配置。

谢谢。

【问题讨论】:

    标签: java spring scheduled-tasks threadpool


    【解决方案1】:

    当您将 STS 部署到 tomcat 时,这是您在 STS 中看到的行为吗?如果是这样,您需要取消部署应用程序,重新部署它并重新启动应用程序。

    【讨论】:

    • 感谢您的建议。我会试试的。
    【解决方案2】:

    另一个想法是使用属性文件中的 SPEL 表达式将其与 @Sched 注释一起使用。这样,在使用该注释时它仍然是可配置的。

    【讨论】:

    • 谢谢。我将不得不看看 SpEL 如何处理计划任务。
    【解决方案3】:

    以下对我有用:

      <bean id="task" class="com.foo.MyTask">
      <task:scheduled-tasks scheduler="scheduler">
        <task:scheduled ref="task" method="run" fixed-delay="300000" />
      </task:scheduled-tasks>
      <task:scheduler id="scheduler" pool-size="10" />
    

    您好, 标记

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多