简体   繁体   中英

Spring Batch Scheduler : Timer implementation

I have to build a online exam module which will be providing 10 minutes duration for exam completion.For implementing the timer part I was thinking to go with Spring batch scheduler.Refer to below config file:

<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>

During invocation I am invoking from a main method:

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

Everything is fine except,timer will be strted as soon as the container is loaded. But my aim is to start the scheduler as soon as user start the exam[eg:after a method invocation/button click] and not on application startup.

Can anyone provide any clue in configuration for implementing such scenario. I know about trigger attribute but it generally works with cron expression and in my case trigger won't be based on time rather based on an event. so,I cann't go via cron.

Can anyone provide any suitable solution to this? only how to invoke the scheduler based on a particualar method call rest I will implement by myself.

There is no such thing Spring batch scheduler , you are referring to Spring Framework's task scheduling.

Scheduling is incompatible with your requirement of "eg:after a method invocation/button click". Scheduling implies knowing the schedule upfront, but in your case, you don't know upfront when the user will click the button. What you actually need is to start a count down timer upon a user action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM