spring

Spring scheduler quickstart

Today I will guide you how to using spring scheduler to do schedule tasks on your application. In this sample scheduler application I will create a simple job to print out current date time after each minute.
Tools and technique that you need in this sample:

  • Eclipse Luna
  • Maven
  • JDK 7
  • Spring Framework 4

1 – Create maven project

In Eclipse IDE choose File->New->Maven Project

2 – pom.xml

Update pom.xml with following content.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.devjav</groupId>
  <artifactId>springtask</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.1.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.1.5.RELEASE</version>
    </dependency>
  </dependencies>
</project>

3 – TaskRunner class

Create TaskRunner class and update with following content

package com.devjav.taskrunner;

import java.util.Date;

public class TaskRunner {
  public void simpleJob() {
    System.out.println(new Date().toString());
  }
}

4 – Application Main

Create class ScheduleTaskApp and update content as below

/*
 * devjav [http://devjav.com]
 * Copyright (C) 2014-2015 Pham Thai Thinh
 * Contact:phamthaithinh@gmail.com
 * 
 */
package com.devjav.springtask;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ScheduleTaskApp {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    ClassPathXmlApplicationContext ctx = 
            new ClassPathXmlApplicationContext("applicationContext.xml");
  }

}

5 – Spring configuration file

We create applicationContext.xml in resource folder and add below content

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
  <task:scheduled-tasks>
    <task:scheduled ref="taskRunner" method="simpleJob"
      fixed-rate="60000" />
  </task:scheduled-tasks>
  <bean id="taskRunner" class="com.devjav.taskrunner.TaskRunner" />
</beans>

6 – Running application

Run application and we get below sample output

INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Thu Mar 12 02:07:23 ICT 2015
Thu Mar 12 02:08:23 ICT 2015
Thu Mar 12 02:09:23 ICT 2015
Thu Mar 12 02:10:23 ICT 2015
Thu Mar 12 02:11:23 ICT 2015
Thu Mar 12 02:12:23 ICT 2015
Thu Mar 12 02:13:23 ICT 2015
Thu Mar 12 02:14:23 ICT 2015

7 – Source code

You can download source code from this link

Leave a Reply