Steps to learn Spring Batch Service

Spring Batch Service

Spring Batch Service – How does it work?

Overview :

The Spring batch services help automate processing of several jobs in batches.  It is a framework that provides the APIs and services that can be reused to process one or more jobs in batches. From an enterprise viewpoint, Spring offers an opportunity to optimize its resources and save unnecessary costs because the Spring batch services is able to process several jobs quickly. Think of this – an enterprise does not need to employ manpower to execute repetitive jobs. Instead, the jobs will be done by an open source framework that provides ready made APIs and services and also allows the modification of the services as per requirement.








What is Spring batch?

Enterprises need to process a number of business operations such as month-end calculations, periodically applying complex business rules and integrating complex information received from external sources processed repetitively across very large data sets in bulk with minimal or no human intervention. The Spring Batch does exactly that. The Spring batch, which is a part of the Spring portfolio, is an open source framework that allows the development of batch-processing applications. Spring Batch offers capabilities for processing both simple and complex business operations. While it offers reusable functions that can process large volumes of records including transaction management, job processing statistics, logging/tracing,  skip, job restart and resource management, it also offers technical services and features that enable high-volume and high performance batch jobs through optimization and partitioning techniques.

Each component in the Spring batch architecture has a specific role to play. The Batch Application component is the application that allows the software developers to batch process one or more jobs. The Spring Batch Core provides the runtime environment for the Batch Application. Lastly, the Spring Batch Infrastructure provides the classes useful for both building and running batch apps.

Must watch – Spring video tutorials 

Advantages

From the enterprise viewpoint, it is desirable that business operations or jobs are completed with the expected accuracy and within time. Enterprises want to complete as many operations as quickly possible with the least investment of resources which can be time and personnel. The Spring batch service allows the enterprise to achieve this objective. The main advantages of the Spring batch services are given below.

  • Availability of reusable functions that can perform tasks such as processing large volumes of records that includes logging/tracing, transaction, and job processing statistics, job restart, skip, and resource management.
  • Availability of technical services that can perform complex tasks that involve high-volume and high-performance batch jobs.
  • At the moment, Spring batch services are probably the best available solution, as far as processing of batch jobs are concerned.









Creating Spring Batch Services

First create a Java project using Maven

$ mvn archetype:generate -DgroupId=com.techalpine -DartifactId=ExampleSpringBatch

-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Now, go to command prompt and convert it to eclipse project and import in Eclipse IDE. Follow the commands shown below.

cd ExampleSpringBatch/

$ mvn eclipse:eclipse 

Declare all project dependencies in pom.xml as shown below.

Listing 1: Defining pom.xml with dependencies

<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.techalpine</groupId>

<artifactId>ExampleSpringBatch</artifactId>

<packaging>jar</packaging>

<version>1.0-SNAPSHOT</version>

<name>ExampleSpringBatch</name>

<url>http://maven.apache.org</url>

<properties>

<jdk.version>1.6</jdk.version>

<spring.version>3.2.2.RELEASE</spring.version>

<spring.batch.version>2.2.0.RELEASE</spring.batch.version>

<mysql.driver.version>5.1.25</mysql.driver.version>

</properties>

<dependencies>

<!– Spring Core –>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${spring.version}</version>

</dependency>

<!– Spring jdbc, for database –>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${spring.version}</version>

</dependency>

<!– Spring Batch dependencies –>

<dependency>

<groupId>org.springframework.batch</groupId>

<artifactId>spring-batch-core</artifactId>

<version>${spring.batch.version}</version>

</dependency>

<dependency>

<groupId>org.springframework.batch</groupId>

<artifactId>spring-batch-infrastructure</artifactId>

<version>${spring.batch.version}</version>

</dependency>

<!– MySQL database driver –>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>${mysql.driver.version}</version>

</dependency>

</dependencies>

<build>

<finalName>example-spring-batch</finalName>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-eclipse-plugin</artifactId>

<version>2.9</version>

<configuration>

<downloadSources>true</downloadSources>

<downloadJavadocs>false</downloadJavadocs>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>2.3.2</version>

<configuration>

<source>${jdk.version}</source>

<target>${jdk.version}</target>

</configuration>

</plugin>

</plugins>

</build>

</project> 



Now, defining batch jobs. This section defines sequence of batch jobs. Steps are mentioned as ‘step1′,’step2’ etc. Within steps, tasklets define small unit of work to be performed. Each batch chunk defines the work. We can have multiple steps in a batch job.

Listing 2: Describing batch jobs and components

<beans xmlns=”http://www.springframework.org/schema/beans”

xmlns:batch=”http://www.springframework.org/schema/batch”

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”>

<bean id=”samplereport” class=”com.techalpine.spring.batch” scope=”prototype” />

<batch:job id = “batchJobSample”>

<batch:step id = “step1”>

<batch:tasklet>

<batch:chunk reader = “ReaderOne” writer = “WriterOne”

processor = “ProcessorOne” commit-interval = “8”>

</batch:chunk>

</batch:tasklet>

</batch:step>

<batch:step id = “step2”>

<batch:tasklet>

<batch:chunk reader = “ReaderTwo” writer = “WriterTwo”

processor = “ProcessorTwo” commit-interval = “8”>

</batch:chunk>

</batch:tasklet>

</batch:step>

</batch:job>

<bean id=”ReaderOne” class=”org.springframework.batch.item.file.FlatFileItemReader”>

<!– Reading file–>

<property name=”resource” value=”classpath:cvs/sample.1csv” />

<property name=”lineMapper”>

<bean class=”org.springframework.batch.item.file.mapping.DefaultLineMapper”>

<!– splitting it –>

<property name=”lineTokenizer”>

<bean

class=”org.springframework.batch.item.file.transform.DelimitedLineTokenizer”>

<property name=”names” value=”date,impressions,clicks,earning” />

</bean>

</property>

<property name=”fieldSetMapper”>

<bean

class=”org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper”>

<property name=”prototypeBeanName” value=”samplereport” />

</bean>

</property>

</bean>

</property>

</bean>

<bean id = “WriterOne”

class = “org.springframework.batch.item.xml.StaxEventItemWriter”>

<property name = “resource” value = “file:xml/outputs/sample1.xml” />

<property name = “marshaller” ref = “yyy” />

<property name = “rootTagName” value = “xxx” />

</bean>

<bean id=”ReaderTwo” class=”org.springframework.batch.item.file.FlatFileItemReader”>

<!– Reading file–>

<property name=”resource” value=”classpath:cvs/sample2.csv” />

<property name=”lineMapper”>

<bean class=”org.springframework.batch.item.file.mapping.DefaultLineMapper”>

<!– splitting it –>

<property name=”lineTokenizer”>

<bean

class=”org.springframework.batch.item.file.transform.DelimitedLineTokenizer”>

<property name=”names” value=”date,impressions,clicks,earning” />

</bean>

</property>

<property name=”fieldSetMapper”>

<bean

class=”org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper”>

<property name=”prototypeBeanName” value=”samplereport” />

</bean>

</property>

</bean>

</property>

</bean>

<bean id = “WriterTwo”

class = “org.springframework.batch.item.xml.StaxEventItemWriter”>

<property name = “resource” value = “file:xml/outputs/sample2.xml” />

<property name = “marshaller” ref = “zzz” />

<property name = “rootTagName” value = “mmm” />

</bean>

</beans>

Now, we will be writing the main program to execute the batch job.

Listing 3: Main program to run the batch job.

import org.springframework.batch.core.Job;

import org.springframework.batch.core.JobExecution;

import org.springframework.batch.core.JobParameters;

import org.springframework.batch.core.launch.JobLauncher;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BatchAppRun {

public static void main(String[] args) throws Exception {

String[] springAppConfig  =  { “jobs/helloTechAlpine.xml” };

// Create application context

ApplicationContext context = new ClassPathXmlApplicationContext(springAppConfig);

// Create the job launcher

JobLauncher samplejobLauncher = (JobLauncher) context.getBean(“jobLauncher”);

// Crete the job

Job samplejob = (Job) context.getBean(“batchJobSample”);

// Execute the JOB

JobExecution execution = samplejobLauncher.run(samplejob, new JobParameters());

}

}

Executing this file will launch the batch job and do the file processing.

Must read – Interesting articles on Spring framework 








Conclusion

The Spring Batch services solves huge business problems for enterprises because it helps them save resources and get things done quickly. Very importantly, it is open source and available for customization. Batch jobs are important when you want to write some tasks repeatedly and sequentially. You just need to define them once in the configuration file and then run it. That’s all about Spring batch services.


============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share