Hi,
Let me describe what I want to acheive.
Once a day a cron scheduller will start (run) TimerTask, and couple minutes later will switch if off (cancel), TimerTask must execute periodically a method (operation to perform a particular task). What I managed to achieve is starting/stoping TimerTask via CronTriggerBean class, but this timer task executes only once instead of every five seconds.
configuration looks like :
and java classes:Code:<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!--<bean id="exampleBusinessObject" class="scb.scheduler.Switcher" />--> <!-- lookup the directory and --> <bean id="resourceLoader" class="scb.scheduler.ExternalResourceLoader" /> <bean id="jobStart" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="resourceTimedTask" /> <property name="targetMethod" value="startPolling" /> <property name="concurrent" value="false" /> </bean> <bean id="jobStop" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="resourceTimedTask" /> <property name="targetMethod" value="stopPolling" /> <property name="concurrent" value="false" /> </bean> <bean id="cronTriggerStart" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="jobStart" /> <property name="cronExpression" value="0 21 7 * * ?" /> </bean> <bean id="cronTriggerStop" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="jobStop" /> <property name="cronExpression" value="0 22 7 * * ?" /> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTriggerStart" /> <ref bean="cronTriggerStop" /> </list> </property> </bean> <bean id="resourceTimedTask" class="scb.scheduler.Switcher"> <!-- wait 10 seconds before starting repeated execution --> <property name="delay" value="10000" /> <!-- run every 50 seconds --> <property name="period" value="5000" /> <property name="timerTask" ref="resourceLoader" /> </bean> <!-- Timer start <bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref local="resourceTimedTask" /> </list> </property> </bean> --> </beans>
Code:public class ExternalResourceLoader extends TimerTask { @Override public void run() { System.out.println("Hello World !!"); } }Code:public class Switcher extends ScheduledTimerTask{ private static Logger logger = Logger.getLogger( Switcher.class.getName()); public void startPolling(){ //logger.info("\n\nSytarting process "); System.out.println("STARTING"); getTimerTask().run(); } public void stopPolling(){ //logger.info("\n\nStoping process "); System.out.println("STOPPING"); getTimerTask().cancel(); } }
Thank you for suggestions and ideas
M.


Reply With Quote