• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Annotation Support for Scheduling and Asynchronous Execution

May 8, 2013 //  by Manisha Patil//  Leave a Comment

In the previous post Task Execution and Scheduling in Spring, I explained basic interfaces for task execution and scheduling. In this post I shall demonstrate an example for the same using annotations @Scheduled and @Async. Let us have working Eclipse IDE in place and follow the steps to create a scheduled Spring application:

  1. Create a project: Create a project with a name SpringScheduler and create a package com.javabeat under the src directory in the created project.
  2. Add Libraries: Add required Spring libraries using Add External JARs option as explained in the article Customizing callback methods. Along with these libraries also add aopalliance-1.0.jar to the build path.
  3. Create source files: Create Java classes SchedulerSample,TaskSample and MainApp under the com.javabeat package.
  4. Create configuration file: Create XML based configuration file Beans.xml under src directory.

follow us on @twitter and @facebook

  • Spring Framework Books
  • Spring Tutorials ( Collection for Spring reference tutorials)
  • Spring Framework Interview Questions
  • Introduction to Spring LDAP
  • How to write Custom Spring Callback Methods?

The content of SchedulerSample.java file are:

package com.javabeat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SchedulerSample {

	  @Autowired
	  private TaskSample task;

	  @Scheduled(fixedDelay = 3000)
	  public void processScheduledJob() {
	    task.execute();
	  }

	  public TaskSample getTask() {
	    return task;
	  }

	  public void setTask(TaskSample task) {
	    this.task = task;
	  }
}

Here you can note that we have scheduled the task for a time of 3000 milliseconds.

The content of TaskSample.java file are:

package com.javabeat;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class TaskSample {
	@Async
	public void execute() {
		System.out
		.println("Executing asynchronous task for every fixedDelay of 3000millisecond.");
	}
}

The execute() method will be called every 3000 milliseconds.

The content of Beans.xml file are:

<?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:context="http://www.springframework.org/schema/context"
 xmlns:task="http://www.springframework.org/schema/task"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/task
   http://www.springframework.org/schema/task/spring-task-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.javabeat" />

<task:annotation-driven executor="executor" scheduler="scheduler" />

<task:executor id="executor" pool-size="5" />

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

</beans>

Here we need to note few things:

  • To enable the annotation driven scheduling we have added the annotation-driven element from the task namespace.
  • Schema http://www.springframework.org/schema/task, http://www.springframework.org/schema/task/spring-task-3.0.xsd, are added to the above file.
  • task:executor – creates an instance of ThreadPoolTaskExecutor.
  • task:scheduler – creates an instance of ThreadPoolTaskScheduler.

To execute the application the MainApp.java is as follows:

package com.javabeat;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
	public static void main(String[] args) {
	    new ClassPathXmlApplicationContext("Beans.xml");
	  }
}

Execute the code

The final step is run the application. Once all the above code is ready execute and the below output appears on the console:

Executing asynchronous task for every fixedDelay of 3000millisecond.
Executing asynchronous task for every fixedDelay of 3000millisecond.

As you can see the message is printed at a delay of 3000millisecond. You need to stop the application to stop the printing of message.

Summary

In this section I demonstrated an example of scheduling the Spring application using the annotation.If you are interested in receiving the future articles, please subscribe here. follow us on @twitter and @facebook

Category: Spring FrameworkTag: Spring Framework

About Manisha Patil

Manisha S Patil, currently residing at Pune India. She is currently working as freelance writer for websites. She had earlier worked at Caritor Bangalore, TCS Bangalore and Sungard Pune. She has 5 years of experience in Java/J2EE technologies.

Previous Post: « Task Execution and Scheduling in Spring
Next Post: How to create ProgressDialog in android? »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact