Spring batch task scheduling example using spring boot

Here we will see Spring batch task scheduling example using spring boot.

Spring batch task scheduling example using spring boot

 

Spring batch task scheduling example using spring boot.

prerequisites –

  • JDK 1.8
  • Eclipse
  • maven

We will use the spring boot library (will provide dependency in pom.xml) to make sure all necessary jar/library is easily available for our application. Spring boot will take care of all jar. Let’s start.

Create maven project, Don’t forget to check ‘Create a simple project (skip)’click on next. Fill all details(GroupId – springbatchexample, ArtifactId – springbatchexample and name – springbatchexample) and click on finish. Keep packaging as the jar.

 

Modify the pom.xml with below code.

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>springbatchexample</groupId>
  <artifactId>springbatchexample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>springbatchexample</name>
  
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
 </parent>
  <dependencies>
	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency> -->
        
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0</version>
        </dependency>
   </dependencies>
   
   
   
   
   
   
   <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>com.springbatchexample.SpringBatchExample</start-class>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.springbatchexample.SpringBatchExample</mainClass>
                </configuration>
            </plugin>
			<plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <fork>true</fork>
                <executable>C:\Program Files\Java\jdk1.8.0_131\bin\javac.exe</executable>
            </configuration>
        </plugin>
        </plugins>
    </build>
   
</project>

 

Create SpringBatchExample.java and TaskScheduler.java. Make sure the package name and the class name is correct.


Create SpringBatchExample.java

package com.springbatchexample;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class SpringBatchExample {
	
	
	public static void main(final String[] args) {
		final ConfigurableApplicationContext configurableApplicationContext = SpringApplication
				.run(SpringBatchExample.class, args);
		
		
	}
	
	
	
}

 

TaskScheduler.java


package com.springbatchexample;

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

@Component
public class TaskScheduler {
	@Scheduled(fixedRate= 2000)
	public void performTask() {
		System.out.println("This is simple example using spring batch");
	}
}

 

Step 7 – Create application.properties file. I have changed the port number to 9091 as 8080 port was busy for some other application. It’s not mandatory to change the port number, do only if you are getting any error like address is already in use.

application.properties

 
server.port = 9091

 

Step 8 –  Run the main class SpringBatchExample.java. Tomcat will deploy and performTask() method will execute automatically at every 2 seconds.

Sample output is –

2018-12-21 21:54:17.681 INFO 3072 — [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9091 (http)
2018-12-21 21:54:17.688 INFO 3072 — [ main] c.springbatchexample.SpringBatchExample : Started SpringBatchExample in 9.412 seconds (JVM running for 12.811)
This is simple example using spring batch
This is simple example using spring batch
This is simple example using spring batch
This is simple example using spring batch
This is simple example using spring batch
This is simple example using spring batch

 

Let’s modify TaskScheduler.java logic. Rest of thing should be the same.

package com.springbatchexample;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

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

@Component
public class TaskScheduler {
	@Scheduled(fixedRate= 4000)
	public void exampleOfFoxedrate() {
		 Date date = new Date();
	     String stringDateFormat = "hh:mm:ss a";
	     DateFormat dateFormat = new SimpleDateFormat(stringDateFormat);
	     
	     //get current time
	     String formattedDateAsString= dateFormat.format(date);
	        
		 System.out.println("this method will execute after every 4 sec "+ formattedDateAsString);
	}
	
	
	
}

Output is –

this method will execute after every 4 sec 07:17:27 PM
this method will execute after every 4 sec 07:17:31 PM
this method will execute after every 4 sec 07:17:35 PM
this method will execute after every 4 sec 07:17:39 PM
this method will execute after every 4 sec 07:17:43 PM

That’s all about Spring batch task scheduling example using spring boot.

Spring Transaction management example.

Spring Data JPA Examples.

Spring Batch Docs.