【问题标题】:Spring Batch Scheduler : Timer implementationSpring Batch Scheduler:定时器实现
【发布时间】:2018-11-27 08:50:52
【问题描述】:

我必须构建一个在线考试模块,该模块将为考试完成提供 10 分钟的持续时间。为了实现计时器部分,我正在考虑使用 Spring 批处理调度程序。请参阅下面的配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<context:component-scan base-package="mypackage" />

<bean id="timerService" class="mypackage.TimerService" />


<task:scheduler id="timerScheduler" pool-size="10" />

  <task:scheduled-tasks scheduler="timerScheduler">
     <task:scheduled ref="timerService" method="displayTimer"
         fixed-rate="1000" />
  </task:scheduled-tasks>
</beans>

在调用过程中,我从一个主方法调用:

ClassPathXmlApplicationContext context = new 
ClassPathXmlApplicationContext("spring/applicationContext.xml");

一切都很好,除了,一旦容器被加载,计时器就会被启动。但我的目标是在用户开始考试后立即启动调度程序[例如:在方法调用/按钮单击之后]和不在应用程序启动时。

任何人都可以在配置中提供任何线索来实现这种场景。 我知道trigger 属性,但它通常适用于cron 表达式,在我的情况下,触发器不会基于时间而是基于事件。 所以,我不能通过 cron。

谁能为此提供任何合适的解决方案? 仅如何基于特定方法调用rest如何调用调度程序,我将自己实现。

【问题讨论】:

  • 使用定时器任务代替弹簧批处理。
  • 两者的操作方式相同。这不是我的问题的答案。我的重点是调用时间。有没有一种方法可以在单击按钮或方法调用后启动计时器

标签: java spring


【解决方案1】:

没有Spring batch scheduler这个东西,你指的是Spring Framework的任务调度。

调度与您对“例如:在方法调用/按钮单击之后”的要求不兼容。计划意味着预先知道计划,但在您的情况下,您不知道用户何时会单击按钮。您真正需要的是在用户操作时start a count down timer

【讨论】:

    猜你喜欢
    • 2012-09-19
    • 2016-01-02
    • 2018-09-19
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 2023-03-21
    相关资源
    最近更新 更多