Spring Task Scheduler XML Configuration

By Arvind Rai, March 08, 2023
On this page, we will learn task scheduler XML configuration in our Spring application. Task scheduler methods are configured using <task:scheduled-tasks> XML element. Scheduler method can also be created using @Scheduled annotation that can be enabled by JavaConfig as well as XML configuration. To enable @Scheduled annotation using XML configuration, we need to use <task:annotation-driven> XML element and in JavaConfig we need to use @EnableScheduling annotation.

1. Scheduling tasks using XML configuration
<beans>
 <task:scheduler id="taskScheduler" pool-size="10"/>
 <bean id="bookApp" class="com.concretepage.BookApp"/>
 <task:scheduled-tasks scheduler="taskScheduler">
	<task:scheduled ref="bookApp" method="cleanDustOne" fixed-rate="2000"/>
	<task:scheduled ref="bookApp" method="cleanDustTwo" fixed-rate="3000"/>
 </task:scheduled-tasks>
</beans> 
<task:scheduler> : It defines ThreadPoolTaskScheduler with a given pool size.
<task:scheduled-tasks> : It groups <task:scheduled> element.
<task:scheduled> : It defines method which is scheduled on fixed rate.

2. Enabling @Scheduled Annotation with XML Configuration
<beans>
  <task:scheduler id="taskScheduler" pool-size="10"/>
  <task:annotation-driven scheduler="taskScheduler"/>     
  <bean id="bookApp" class="com.concretepage.BookApp"/>
</beans> 
<task:annotation-driven>: It enables @Scheduled methods to be scheduled on fixed rate.

Now we will discuss here complete example.

Technologies Used

Find the technologies being used in our example.
1. Java
2. Spring
3. Gradle

Gradle Dependencies

dependencies {
    compile 'org.springframework.boot:spring-boot-starter:1.2.7.RELEASE'
}  

Create Scheduler Methods

Create a class with some methods that will be scheduled by XML configuration.
BookApp.java
package com.concretepage;
import org.springframework.scheduling.annotation.Scheduled;
public class BookApp {
	public void cleanDustOne() {
		System.out.println("Executing cleanDustOne method...");
	}
	public void cleanDustTwo() {
		System.out.println("Executing cleanDustTwo method...");
	}	
	@Scheduled(fixedRate=1000)
	public void arrangeBook(){
	   System.out.println("Arranging Book...");
	}	
} 
1. The methods cleanDustOne and cleanDustTwo will be scheduled using <task:scheduled-tasks> XML element.
2. The method arrangeBook will be scheduled using @Scheduled annotation and the @Scheduled annotation will be enabled by <task:annotation-driven/> element.

XML Configuration

Find the scheduler XML configurations.
application-context.xml
<?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-4.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
   <beans>
     <task:scheduler id="taskScheduler" pool-size="10"/>
     <task:annotation-driven scheduler="taskScheduler"/>     
     <bean id="bookApp" class="com.concretepage.BookApp"/>
     <task:scheduled-tasks scheduler="taskScheduler">
     	<task:scheduled ref="bookApp" method="cleanDustOne" fixed-rate="2000"/>
     	<task:scheduled ref="bookApp" method="cleanDustTwo" fixed-rate="3000"/>
     </task:scheduled-tasks>
   </beans>
</beans> 

Run Demo Application


XMLConfigSpringDemo.java
package com.concretepage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class XMLConfigSpringDemo {
	public static void main(String[] args) {
		AbstractApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
	}
} 
Find the output.
Arranging Book...
Executing cleanDustOne method...
Arranging Book...
Executing cleanDustTwo method...
Arranging Book... 

Reference

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us