gpt4 book ai didi

java - Spring 批量调度程序 : Timer implementation

转载 作者:行者123 更新时间:2023-12-02 10:33:00 26 4
gpt4 key购买 nike

我必须构建一个在线考试模块,它将提供 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 进行操作。

谁能提供任何合适的解决方案吗?只是如何根据特定的方法来调用调度程序,其余的我会自己实现。

最佳答案

没有这样的东西Spring批调度器,你指的是Spring框架的任务调度。

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

关于java - Spring 批量调度程序 : Timer implementation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53495795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com