Results 1 to 6 of 6

Thread: ScheduledTimerTask combiled with TimerTask

  1. #1

    Default 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. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    So you want a timer task that starts up for a period of time every day, and within that period runs every five seconds?
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  3. #3

    Default

    Yes, this is exaclty what I want

  4. #4
    Join Date
    Sep 2007
    Location
    Oceanside, CA
    Posts
    187

    Default

    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?
    Mike Bingham

  5. #5

    Default

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

  6. #6
    Join Date
    Sep 2007
    Location
    Oceanside, CA
    Posts
    187

    Default

    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 at 10:16 AM.
    Mike Bingham

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •