Announcement Announcement Module
Collapse
No announcement yet.
ScheduledTimerTask combiled with TimerTask Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ScheduledTimerTask combiled with TimerTask

    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 :
    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>
    and java classes:
    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.

  • #2
    So you want a timer task that starts up for a period of time every day, and within that period runs every five seconds?

    Comment


    • #3
      Yes, this is exaclty what I want

      Comment


      • #4
        Could you accomplish what you want with a single Quartz cron trigger like:

        Code:
        <property name="cronExpression" value="0/5 21 7 * * ?" />
        instead of a start/stop trigger and separate TimerTask?

        Comment


        • #5
          No, I want to execute task periodcaly every 5 sec. within a specific time window. What you propose is executing task throughout the day (unlimited time window).

          Comment


          • #6
            The following cron expression should fire every 5 seconds only between 7:21-7:22 each day:

            Code:
            <property name="cronExpression" value="0/5 21 7 * * ?" />
            Perhaps I'm just misunderstanding something here.
            Last edited by Mike Bingham; Oct 9th, 2007, 11:16 AM.

            Comment

            Working...
            X