【发布时间】: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如何调用调度程序,我将自己实现。
【问题讨论】:
-
使用定时器任务代替弹簧批处理。
-
两者的操作方式相同。这不是我的问题的答案。我的重点是调用时间。有没有一种方法可以在单击按钮或方法调用后启动计时器