Spring Scheduler

Spring Framework provides abstractions for execution and scheduling of tasks. There are many ways to achieve this. Let go through with some of them.

Annotation driven

Perhaps one of the simplest way is just to annotate your method with @Schedule along with one of the three parameters:

  • fixedRate -Execute the annotated method with a fixed period between invocations.
  • fixedDelay – Execute the annotated method with a fixed period between the end of the last invocation and the start of the next.
  • cron – A cron-like expression, extending the usual UNIX definition to include triggers on the second as well as minute, hour, day of month, month and day of week.
  • There is another called initialDelay  which is the number of milliseconds to delay before the first execution of a fixedRate() or fixedDelay() task.

Create a java project with spring and common logging dependencies.

SchedulerUsingAnnotation.java


package mynotes.spring.scheduler;

import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;

public class SchedulerUsingAnnotation {

 @Scheduled(fixedRate=5000)
 public void runThisTask(){
 System.out.println("runThisTask at "+new Date(System.currentTimeMillis()));
 }

}

As you can see we onlu annotated the method with @Schedule

springAnnotation.xml:


<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:task="http://www.springframework.org/schema/task"
 xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.2.xsd
 http://www.springframework.org/schema/jee
 http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
 http://www.springframework.org/schema/util
 http://www.springframework.org/schema/util/spring-util.xsd
 http://www.springframework.org/schema/task
 http://www.springframework.org/schema/task/spring-task-3.2.xsd">

<task:annotation-driven />

<bean id="schedulerUsingAnnotation" class="mynotes.spring.scheduler.SchedulerUsingAnnotation"/>

</beans>

Here just defining the task bean and using aanotation-driven. Lets start the program by creating a context.

SchedulerUsingAnnotationMain.java


package mynotes.spring.main;

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

public class SchedulerUsingAnnotationMain {

 public static void main(String[] args) {
 ApplicationContext context=new ClassPathXmlApplicationContext("springAnnotation.xml");
 }

}

Output:


runThisTask at Fri Mar 28 06:21:21 IST 2014
runThisTask at Fri Mar 28 06:21:26 IST 2014
runThisTask at Fri Mar 28 06:21:31 IST 2014
runThisTask at Fri Mar 28 06:21:36 IST 2014

Notice that fixedRate, fixedDelay, initialDelay take long as value while cron takes string. For eg.

@Scheduled(cron=”0 15 10 ? * *”)  – Fire at 10:15 AM every day

Lets get into some details for cron expression. A cron expression is a string consisting of six or seven subexpressions (fields) that describe individual details of the schedule.

Cron Expressions Allowed Fields and Values

Name Required Allowed Values Allowed Special Characters
Seconds Y 0-59 , – * /
Minutes Y 0-59 , – * /
Hours Y 0-23 , – * /
Day of month Y 1-31 , – * ? / L W C
Month Y 0-11 or JAN-DEC , – * /
Day of week Y 1-7 or SUN-SAT , – * ? / L C #
Year N empty or 1970-2099 , – * /
Expression Means
0 0 12 * * ? Fire at 12:00 PM (noon) every day
0 15 10 ? * * Fire at 10:15 AM every day
0 15 10 * * ? Fire at 10:15 AM every day
0 15 10 * * ? * Fire at 10:15 AM every day
0 15 10 * * ? 2005 Fire at 10:15 AM every day during the year 2005
0 * 14 * * ? Fire every minute starting at 2:00 PM and ending at 2:59 PM, every day
0 0/5 14 * * ? Fire every 5 minutes starting at 2:00 PM and ending at 2:55 PM, every day
0 0/5 14,18 * * ? Fire every 5 minutes starting at 2:00 PM and ending at 2:55 PM, AND fire every 5 minutes starting at 6:00 PM and ending at 6:55 PM, every day
0 0-5 14 * * ? Fire every minute starting at 2:00 PM and ending at 2:05 PM, every day
0 10,44 14 ? 3 WED Fire at 2:10 PM and at 2:44 PM every Wednesday in the month of March
0 15 10 ? * MON-FRI Fire at 10:15 AM every Monday, Tuesday, Wednesday, Thursday and Friday
0 15 10 15 * ? Fire at 10:15 AM on the 15th day of every month
0 15 10 L * ? Fire at 10:15 AM on the last day of every month
0 15 10 ? * 6L Fire at 10:15 AM on the last Friday of every month
0 15 10 ? * 6L Fire at 10:15 AM on the last Friday of every month
0 15 10 ? * 6L 2002-2005 Fire at 10:15 AM on every last friday of every month during the years 2002, 2003, 2004, and 2005
0 15 10 ? * 6#3 Fire at 10:15 AM on the third Friday of every month
0 0 12 1/5 * ? Fire at 12 PM (noon) every 5 days every month, starting on the first day of the month
0 11 11 11 11 ? Fire every November 11 at 11:11 AM

You can get values for your cron using properties files as well. Just add these lines in your spring.xml

<util:properties id=”applicationProps” location=”application.properties” />
<context:property-placeholder properties-ref=”applicationProps” />

and use @Scheduled(cron=”${cron.expression}”) where cron.expression is in yout properties files kept at root.

Scheduler using Xml

SchedulerUsingXml.java – Just a simple class


package mynotes.spring.scheduler;

import java.util.Date;

public class SchedulerUsingXml {

public void runThisTask(){
System.out.println("runThisTask at "+new Date(System.currentTimeMillis()));
}

}

In you spring.xml you need to add follow beans :


<task:scheduled-tasks scheduler="taskScheduler">
<task:scheduled ref="schedulerUsingXml" method="runThisTask" fixed-rate="5000"/>
</task:scheduled-tasks>

<task:scheduler id="taskScheduler" pool-size="1"/>

<bean id="schedulerUsingXml" class="mynotes.spring.scheduler.SchedulerUsingXml"/>

Now run your main class. Output will be the same. It will run every 5 sec.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: