How to schedule a job in Spring 4 ?
Spring framework provides support for scheduling of task.Spring scheduler is used to schedule a task to be executed at the given time interval.
In this post, we are going to see how to use Spring scheduler to schedule a job.Annotations are used instead of XML configuration.

@Scheduled - is provided by Spring Framework to schedule a job.
Cron Expression - Cron expression is represented by six fields.
@Scheduled(cron="* * * * * *")
Sample Code:
AppConfiguration:
package com.javainstance.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import com.javainstance.service.Tasks;
/**
* @author javainstance
*
*/
@Configuration
@EnableScheduling
public class AppConfiguration {
@Bean
public Tasks getTask() {
return new Tasks();
}
}
TestTask.java
In this post, we are going to see how to use Spring scheduler to schedule a job.Annotations are used instead of XML configuration.

@Scheduled - is provided by Spring Framework to schedule a job.
Cron Expression - Cron expression is represented by six fields.
@Scheduled(cron="* * * * * *")
second, minute, hour, day of month, month, day(s) of week
(*) means any
*/x means "every x"
Sample Code:
AppConfiguration:
package com.javainstance.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import com.javainstance.service.Tasks;
/**
* @author javainstance
*
*/
@Configuration
@EnableScheduling
public class AppConfiguration {
@Bean
public Tasks getTask() {
return new Tasks();
}
}
Tasks.java
package com.javainstance.service;
import org.springframework.scheduling.annotation.Scheduled;
/**
* @author javainstance
*
*/
public class Tasks {
/**
* To execute Task at regular interval
*/
@Scheduled(fixedRate = 4000)
public void doTask1() {
System.out.println("This get executed at fixed interval");
}
/**
* To execute Task at fixed delay after last execution finishes
*/
@Scheduled(fixedDelay = 4000)
public void doTask2() {
System.out.println("This get executed at fixed delay after last execution finishes");
}
/**
* Using Cron expression This task get executed at the interval of 1 hour
* from 1 AM to 11 PM
*/
@Scheduled(cron = "0 0 1-23 * * *")
public void doTask3() {
System.out.println("This get executed at the interval of 1 hour from 1 AM to 11 PM");
}
/**
* Using Cron expression This task get executed everyday at 1 AM
*/
@Scheduled(cron = "0 0 1 * * *")
public void doTask4() {
System.out.println("This task get executed everyday at 1 AM");
}
/**
* Using Cron expression This task get executed everyday at every 10 sec
*/
@Scheduled(cron = "*/10 * * * * *")
public void doTask5() {
System.out.println("This task get executed at every 10 sec");
}
}
package com.javainstance.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.javainstance.configuration.AppConfiguration;
/**
* @author javainstance
*
*/
public class TestTask {
@SuppressWarnings(value = { "resource", "unused" })
public static void main(String[] args) {
ApplicationContext appContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
}
}
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javainstance</groupId>
<artifactId>SpringScheduler</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringScheduler Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<springframework.version>4.0.6.RELEASE</springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
</dependencies>
<build>
<finalName>SpringScheduler</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Create Android App for your Website or Blog in 5 minutes.
Give your suggestion in comments.
If you like the post Share with your friends on social network.
Follow us: www.facebook.com/javainstance
Follow us: www.facebook.com/javainstance
How to schedule a job in Spring 4 ?
Reviewed by JavaInstance
on
9:49:00 AM
Rating:
Reviewed by JavaInstance
on
9:49:00 AM
Rating:
No comments: