What is Spring Batch ?
Spring Batch is an open source framework for batch processing. It is a lightweight, comprehensive solution designed to enable the development of robust batch applications, which are often found in modern enterprise systems. Spring Batch builds upon the POJO-based development approach of the Spring Framework.
So now lets set up out pom.xml file so basically we need 4 tyepe of dependencies to execute our spring job.
What is Spring BOM ?
If you have worked on maven in your projects for dependency management, then you must have faced one problem at least once or may be more than that. And the problem is version mismatch. It generally happens when you got some dependencies which bring it’s related dependencies together with certain version. And if you have included those dependencies with different version numbers already, they can face undesired results in compile time as well as runtime also. Ideally to avoid above issue you need to explicitly exclude the related dependency, but it is quite possible that you can forget to do so.
will add all those dependencies in 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.spring.batch</groupId>
<artifactId>SpringBatchAuto</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBatchAuto Maven Webapp</name>
<repositories>
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<properties>
<spring.version>4.0.1.RELEASE</spring.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- Spring Batch dependency -->
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-infrastructure</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>SpringBatchAuto</finalName>
</build>
</project>
Here just a basic configuration of Spring MVC in web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Archetype Created Web Application</display-name>
<!-- Defines the Spring Context loader listener. -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- Sets up the Spring Dispatcher Servlet and fires up the applicationContext -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- For Session -->
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
in applicationContext.xml will create two types of bean one is for execute using bean and another is for executing using annotation
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<!-- Using Annotation only task:annotation-driven is required -->
<bean id="executeUsingAnnotation" class="com.spring.batch.ExecuteUsingAnnotation"></bean>
<task:annotation-driven />
<!-- Using Bean -->
<bean id="executeUsingBean" class="com.spring.batch.ExecuteUsingBean"></bean>
<task:scheduled-tasks>
<task:scheduled ref="executeUsingBean" method="run" cron="*/5 * * * * *"/>
</task:scheduled-tasks>
</beans>
so ExecuteUsingBean.java will execute using task:scheduled-tasks configuration
package com.spring.batch;
import java.util.Date;
public class ExecuteUsingBean {
public void run() {
System.out.println("Execute using bean in every 5 seconds -"+ new Date());
}
}
ExecuteUsingAnnotation.java will execute based on task:annotation-driven
package com.spring.batch;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
public class ExecuteUsingAnnotation {
@Scheduled(cron = "*/10 * * * * *")
public void run() {
System.out.println("Execute using annotation in every 10 seconds -"+ new Date());
}
}
Here is the output -
You can download code from GitHub Here
Hope you like it, thanks to read this blog Please share you comments so will update ASAP.