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:
- Create a project: Create a project with a name SpringScheduler and create a package com.javabeat under the src directory in the created project.
- 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.
- Create source files: Create Java classes SchedulerSample,TaskSample and MainApp under the com.javabeat package.
- 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