Spring + JDK Timer scheduler example

Note
Learn the JDK Timer scheduler example without Spring and compare the different with this example.

In this example, you will use Spring’s Scheduler API to schedule a task.

1. Scheduler Task

Create a scheduler task…


package com.mkyong.common;

public class RunMeTask
{
	public void printMe() {
		System.out.println("Run Me ~");
	}
}

<bean id="runMeTask" class="com.mkyong.common.RunMeTask" />

Spring comes with a MethodInvokingTimerTaskFactoryBean as a replacement for the JDK TimerTask. You can define your target scheduler object and method to call here.


<bean id="schedulerTask" 
  class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
	<property name="targetObject" ref="runMeTask" />
	<property name="targetMethod" value="printMe" />
</bean>

Spring comes with a ScheduledTimerTask as a replacement for the JDK Timer. You can pass your scheduler name, delay and period here.


<bean id="timerTask"
	class="org.springframework.scheduling.timer.ScheduledTimerTask">
	<property name="timerTask" ref="schedulerTask" />
	<property name="delay" value="1000" />
	<property name="period" value="60000" />
</bean>

2. TimerFactoryBean

In last, you can configure a TimerFactoryBean bean to start your scheduler task.


<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
	<property name="scheduledTimerTasks">
		<list>
			<ref local="timerTask" />
		</list>
	</property>
</bean>

File : Spring-Scheduler.xml


<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.5.xsd">

<bean id="schedulerTask" 
  class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
	<property name="targetObject" ref="runMeTask" />
	<property name="targetMethod" value="printMe" />
</bean>

<bean id="runMeTask" class="com.mkyong.common.RunMeTask" />
	
<bean id="timerTask"
	class="org.springframework.scheduling.timer.ScheduledTimerTask">
	<property name="timerTask" ref="schedulerTask" />
	<property name="delay" value="1000" />
	<property name="period" value="60000" />
</bean>

<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
	<property name="scheduledTimerTasks">
		<list>
			<ref local="timerTask" />
		</list>
	</property>
</bean>

</beans>

Run it


package com.mkyong.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext context = 
		  new ClassPathXmlApplicationContext("Spring-Scheduler.xml");
    }
}

No code need to call the scheduler task, the TimerFactoryBean will run your schedule task during start up. As result, Spring scheduler will run the printMe() method every 60 seconds, with a 1 second delay for the first time of execution.

Download Source Code

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
16 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
14 Comment authors
Naveen GargZhuindenKShahManishaDJ Recent comment authors
newest oldest most voted
Naveen Garg
Guest
Naveen Garg

can we task two method in schedular

Zhuinden
Guest
Zhuinden

This solution is deprecated since Spring 3.0, and seems to be removed in Spring 4

KShah
Guest
KShah

We are running spring scheduler very similar to explained in this example. However due to some internal error the standalone application which invoked this task died and so this task too. how can we ensure that this task is alive and working normally even the main application failed or died. Also is there any way to check if this bean (task) is active and if not restart externally or from same application context?

Manisha
Guest
Manisha

Hi,

thanks for this great post.
I am new to spring.
I have downloaded this code to eclipse.
Now not sure how to deploy this to Jboss server.
can you please guide me.

Thanks
Manisha

DJ
Guest
DJ

Awesome. Very easy to setup. I already have spring setup on my local. This task took me under 15 min to complete.

abc
Guest
abc

Hi,
I would like to use the factory bean to schedule more than one task, is it possible?
It is running only the first task if I add anotehr bean ref to the list property.

If I create another instance of the factory bean, that still runs the first registered task , not the second bean ref.
Has anyone encountered this?

Thanks!

Vivek Pandey
Guest
Vivek Pandey

Hi , I want yo execute two method in RunMeTask class.
Please let me know how can I configure another method(printMe2() ) for scheduled execution .

Thanks,
Vivek

srinivas
Guest
srinivas

Hi,

I am trying to configure scheduleTimerTask to JMX console. I did configured to JMX console and scheduledTimerTask properties are populated on JMX console. But when i change period value on JMX console it is not effected on my application. please provide some suggestion to fix it

Carlos Oviedo
Guest
Carlos Oviedo

I have a question: If I wanna pass the context object in the main class to the task class (RunMeTask)… how to do it?

krishna
Guest
krishna

Hi in my scenario i want to schedule a method which takes string as argument like printMe(String str)

can any one help me regarding this.

Tulio Domingos
Guest
Tulio Domingos

Great tutorial!

Carlos
Guest
Carlos

Excellent! The TimerFactoryBean was the key part that I was missing. Works like a charm. Thanks you…

Carlos

raza
Guest
raza

hi, this is really fruitful .
But if Jboss is already running then how to use delay as delay works after startup .

how to maintain delay over there?

trackback
Spring Tutorials | Tutorials

[…] Spring + JDK Timer scheduler example Article about how Spring schedule a job with JDK timer. […]

trackback
The Nature of Heaven | Ask Old Coot

[…] Spring + JDK Timer scheduler example | Spring […]

@krishna
Guest
@krishna

this should work