Spring Task Scheduler
June 23, 2019
On this page we will provide Spring task scheduler example with @Scheduled annotation using XML and JavaConfig. Method can be scheduled using XML configuration as well as JavaConfig. In case of XML, the method to be scheduled should be configured in XML configuration. Method can also be scheduled using @Scheduled annotation using task:annotation-driven tag in XML configuration. In case of JavaConfig, there should be class level annotation @EnableScheduling and method is scheduled using @Scheduled annotated. We can also write @Scheduled annotated method in JavaConfig file, too. Find the example.
Contents
Software Used
Find the software used in demo.1. Java 8
2. Spring 4.1.8.RELEASE
3. Gradle
4. Eclipse
build.gradle
apply plugin: 'java' apply plugin: 'eclipse' archivesBaseName = 'SpringDemo' version = '1' repositories { mavenCentral() } dependencies { compile 'org.springframework.boot:spring-boot-starter:1.2.7.RELEASE' }
1. Spring Task Scheduler using 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..."); } }
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>
Only with 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>: It defines method which is scheduled on fixed rate.
<task:scheduled-tasks>: It groups <task:scheduled> tags.
@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 run the demo.
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"); } }
Arranging Book... Executing cleanDustOne method... Arranging Book... Executing cleanDustTwo method... Arranging Book...
2. Spring Task Scheduler with @Scheduled Annotation using JavaConfig
We can also schedule @Scheduled methods using JavaConfig with the help of @EnableScheduling annotation. Method can be defined in JavaConfig file also.AppConfig.java
package com.concretepage; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; @Configuration @ComponentScan(basePackages="com.concretepage") @EnableScheduling public class AppConfig { @Scheduled(fixedRate=1000) public void doTask() { System.out.println("Do Task..."); } }
StudentUtil.java
package com.concretepage; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class StudentUtil { @Scheduled(fixedRate=2000) public void countStudent(){ System.out.println("Count Student..."); } }
JavaConfigSpringDemo.java
package com.concretepage; import org.springframework.boot.SpringApplication; import org.springframework.context.ApplicationContext; public class JavaConfigSpringDemo { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(AppConfig.class); } }
Do Task... Do Task... Count Student... Do Task...