Spring Batch Hello World Example

Spring Batch is a framework for batch processing – execution of a series of jobs. In Spring Batch, A job consists of many steps and each step consists of a READ-PROCESS-WRITE task or single operation task (tasklet).

  1. For “READ-PROCESS-WRITE” process, it means “read” data from the resources (csv, xml or database), “process” it and “write” it to other resources (csv, xml and database). For example, a step may read data from a CSV file, process it and write it into the database. Spring Batch provides many made Classes to read/write CSV, XML and database.
  2. For “single” operation task (tasklet), it means doing single task only, like clean up the resources after or before a step is started or completed.
  3. And the steps can be chained together to run as a job.

1 Job = Many Steps.
1 Step = 1 READ-PROCESS-WRITE or 1 Tasklet.
Job = {Step 1 -> Step 2 -> Step 3} (Chained together) 

Spring Batch Examples

Consider following batch jobs :

  1. Step 1 – Read CSV files from folder A, process, write it to folder B. “READ-PROCESS-WRITE”
  2. Step 2 – Read CSV files from folder B, process, write it to the database. “READ-PROCESS-WRITE”
  3. Step 3 – Delete the CSB files from folder B. “Tasklet”
  4. Step 4 – Read data from a database, process and generate statistic report in XML format, write it to folder C. “READ-PROCESS-WRITE”
  5. Step 5 – Read the report and send it to manager email. “Tasklet”

In Spring Batch, we can declare like the following :


  <job id="abcJob" xmlns="http://www.springframework.org/schema/batch">
	<step id="step1" next="step2">
	  <tasklet>
		<chunk reader="cvsItemReader" writer="cvsItemWriter"  
                    processor="itemProcesser" commit-interval="1" />
	  </tasklet>
	</step>
	<step id="step2" next="step3">
	  <tasklet>
		<chunk reader="cvsItemReader" writer="databaseItemWriter"  
                    processor="itemProcesser" commit-interval="1" />
	  </tasklet>
	</step>
	<step id="step3" next="step4">
	  <tasklet ref="fileDeletingTasklet" />
	</step>
	<step id="step4" next="step5">
	  <tasklet>
		<chunk reader="databaseItemReader" writer="xmlItemWriter"  
                    processor="itemProcesser" commit-interval="1" />
	  </tasklet>
	</step>
	<step id="step5">
		<tasklet ref="sendingEmailTasklet" />
	</step>
  </job>

The entire jobs and steps execution are stored in database, which make the failed step is able to restart at where it was failed, no need start over the entire job.

1. Tutorial

In this Spring Batch tutorial, we will show you how to create a job, read a CSV file, process it, write the output to an XML file.

Tools and libraries used

  1. Maven 3
  2. Eclipse 4.2
  3. JDK 1.6
  4. Spring Core 3.2.2.RELEASE
  5. Spring OXM 3.2.2.RELEASE
  6. Spring JDBC 3.2.2.RELEASE
  7. Spring Batch 2.2.0.RELEASE

2. Project Directory

Review final project directory, a standard Maven project.

spring-batch-hello-world-directory

3. Project Dependencies

They must have dependencies are just Spring Core, Spring Batch and JDK 1.5. Read comments for self-explanatory.

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.mkyong</groupId>
	<artifactId>SpringBatchExample</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>SpringBatchExample</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>
		<junit.version>4.11</junit.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 XML to/back object -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<!-- MySQL database driver -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.driver.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>

		<!-- Spring Batch unit test -->
		<dependency>
			<groupId>org.springframework.batch</groupId>
			<artifactId>spring-batch-test</artifactId>
			<version>${spring.batch.version}</version>
		</dependency>
		
		<!-- Junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>

	</dependencies>
	<build>
		<finalName>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>

4. Spring Batch Jobs

A CSV file.

report.csv

1001,"213,100",980,"mkyong", 29/7/2013
1002,"320,200",1080,"staff 1", 30/7/2013
1003,"342,197",1200,"staff 2", 31/7/2013

A Spring batch job, to read above csv file with FlatFileItemReader, process the data with itemProcessor and write it to an XML file
with StaxEventItemWriter.

job-hello-world.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:batch="http://www.springframework.org/schema/batch" 
	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
	">

	<import resource="../config/context.xml" />
	<import resource="../config/database.xml" />

	<bean id="report" class="com.mkyong.model.Report" scope="prototype" />
	<bean id="itemProcessor" class="com.mkyong.CustomItemProcessor" />

	<batch:job id="helloWorldJob">
	  <batch:step id="step1">
		<batch:tasklet>
			<batch:chunk reader="cvsFileItemReader" writer="xmlItemWriter" 
                              processor="itemProcessor" commit-interval="10">
			</batch:chunk>
		</batch:tasklet>
	  </batch:step>
	</batch:job>

	<bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">

		<property name="resource" value="classpath:cvs/input/report.csv" />

		<property name="lineMapper">
		    <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
			<property name="lineTokenizer">
				<bean
					class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
					<property name="names" value="id,sales,qty,staffName,date" />
				</bean>
			</property>
			<property name="fieldSetMapper">
				<bean class="com.mkyong.ReportFieldSetMapper" />
				    
				 <!-- if no data type conversion, use BeanWrapperFieldSetMapper to map by name
				<bean
					class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
					<property name="prototypeBeanName" value="report" />
				</bean>
				 -->
			</property>
		    </bean>
		</property>

	</bean>

	<bean id="xmlItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
		<property name="resource" value="file:xml/outputs/report.xml" />
		<property name="marshaller" ref="reportMarshaller" />
		<property name="rootTagName" value="report" />
	</bean>

	<bean id="reportMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
	   <property name="classesToBeBound">
		<list>
			<value>com.mkyong.model.Report</value>
		</list>
	    </property>
	</bean>

</beans>

Map CSV value to Report object and write it to XML file (via jaxb annotations).

Report.java

package com.mkyong.model;

import java.math.BigDecimal;
import java.util.Date;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "record")
public class Report {

	private int id;
	private BigDecimal sales;
	private int qty;
	private String staffName;
	private Date date;

	@XmlAttribute(name = "id")
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@XmlElement(name = "sales")
	public BigDecimal getSales() {
		return sales;
	}

	public void setSales(BigDecimal sales) {
		this.sales = sales;
	}

	@XmlElement(name = "qty")
	public int getQty() {
		return qty;
	}

	public void setQty(int qty) {
		this.qty = qty;
	}

	@XmlElement(name = "staffName")
	public String getStaffName() {
		return staffName;
	}

	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	@Override
	public String toString() {
		return "Report [id=" + id + ", sales=" + sales 
                    + ", qty=" + qty + ", staffName=" + staffName + "]";
	}

}

To convert a Date, you need a custom FieldSetMapper. If no data type conversion, just use BeanWrapperFieldSetMapper to map the values by name automatically.

ReportFieldSetMapper.java

package com.mkyong;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

import com.mkyong.model.Report;

public class ReportFieldSetMapper implements FieldSetMapper<Report> {

	private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	
	@Override
	public Report mapFieldSet(FieldSet fieldSet) throws BindException {
		
		Report report = new Report();
		report.setId(fieldSet.readInt(0));
		report.setSales(fieldSet.readBigDecimal(1));
		report.setQty(fieldSet.readInt(2));
		report.setStaffName(fieldSet.readString(3));
		
		//default format yyyy-MM-dd
		//fieldSet.readDate(4);
		String date = fieldSet.readString(4);
		try {
			report.setDate(dateFormat.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return report;
		
	}

}

A itemProcessor will be fired before itemWriter.

CustomItemProcessor.java

package com.mkyong;

import org.springframework.batch.item.ItemProcessor;
import com.mkyong.model.Report;

public class CustomItemProcessor implements ItemProcessor<Report, Report> {

	@Override
	public Report process(Report item) throws Exception {
		
		System.out.println("Processing..." + item);
		return item;
	}

}

Spring context and database configuration.

context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

	<!-- stored job-meta in memory -->
	<!--  
	<bean id="jobRepository"
		class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
		<property name="transactionManager" ref="transactionManager" />
	</bean>
 	 -->
 	 
 	 <!-- stored job-meta in database -->
	<bean id="jobRepository"
		class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="transactionManager" ref="transactionManager" />
		<property name="databaseType" value="mysql" />
	</bean>
	
	<bean id="transactionManager"
		class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
	 
	<bean id="jobLauncher"
		class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
		<property name="jobRepository" ref="jobRepository" />
	</bean>

</beans>
database.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/jdbc 
		http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">

        <!-- connect to MySQL database -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/test" />
		<property name="username" value="root" />
		<property name="password" value="" />
	</bean>

	<bean id="transactionManager"
		class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
	
	<!-- create job-meta tables automatically -->
	<jdbc:initialize-database data-source="dataSource">
		<jdbc:script location="org/springframework/batch/core/schema-drop-mysql.sql" />
		<jdbc:script location="org/springframework/batch/core/schema-mysql.sql" />
	</jdbc:initialize-database>
	
</beans>

5. Run It

The most simplest way to run a batch job.


package com.mkyong;

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 App {
  public static void main(String[] args) {

	String[] springConfig  = 
		{	
			"spring/batch/jobs/job-hello-world.xml" 
		};
		
	ApplicationContext context = 
			new ClassPathXmlApplicationContext(springConfig);
		
	JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
	Job job = (Job) context.getBean("helloWorldJob");

	try {

		JobExecution execution = jobLauncher.run(job, new JobParameters());
		System.out.println("Exit Status : " + execution.getStatus());

	} catch (Exception e) {
		e.printStackTrace();
	}

	System.out.println("Done");

  }
}

Output

report.xml

<?xml version="1.0" encoding="UTF-8"?>
<report>
	<record id="1001">
		<date>2013-07-29T00:00:00+08:00</date>
		<qty>980</qty>
		<sales>213100</sales>
		<staffName>mkyong</staffName>
	</record>
	<record id="1002">
		<date>2013-07-30T00:00:00+08:00</date>
		<qty>1080</qty>
		<sales>320200</sales>
		<staffName>staff 1</staffName>
	</record>
	<record id="1003">
		<date>2013-07-31T00:00:00+08:00</date>
		<qty>1200</qty>
		<sales>342197</sales>
		<staffName>staff 2</staffName>
	</record>
</report>

In console


Jul 30, 2013 11:52:00 PM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFO: Job: [FlowJob: [name=helloWorldJob]] launched with the following parameters: [{}]
Jul 30, 2013 11:52:00 PM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [step1]
Processing...Report [id=1001, sales=213100, qty=980, staffName=mkyong]
Processing...Report [id=1002, sales=320200, qty=1080, staffName=staff 1]
Processing...Report [id=1003, sales=342197, qty=1200, staffName=staff 2]
Jul 30, 2013 11:52:00 PM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFO: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
Exit Status : COMPLETED
Done

Download Source Code

Download it – SpringBatch-Hello-World-Example.zip (27 kb)

References

  1. What is Batch Processing
  2. Spring Batch Frameworks
  3. Spring Batch – Reference Documentation

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
30 Comment threads
11 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
40 Comment authors
BartArpit BhargavaVinothKarenBeth Recent comment authors
newest oldest most voted
magesh
Guest
magesh

Hi i want to read the the data from websphere mq and write it into oracle database using batch how can i write the batch job for it?

Please let me know how can i write for it .If possible provide me with a template

netbeans
Guest
netbeans

Hi I am new to spring batch. I run the application and got this error as Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.springframework.jdbc.datasource.init.DataSourceInitializer#0’: Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1488) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) at… Read more »

Piero
Guest
Piero

Good Tutorial….

But I can not execute it. I’m asking you if I need the database server on the localhost according database.xml file

This is the stacktrace when I try run main into App.java
Best Regards

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1488)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93)
at com.mkyong.App.main(App.java:19)
Caused by: org.springframework.dao.DataAccessResourceFailureException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

diego
Guest
diego

hihello you have tried to see if the server is run, and if the login username and password are correct with those defined in the file database.xml,

lakshmi
Guest
lakshmi

Hi,
I faced a couple of problems from the db side(using postgres). When executing first time comment the drop schema, then uncomment from consecutive executions. Create proper role(required in postgres), user and password(not mandatory).

From Maven side got deploy issues(like Schema files were not resolved). Solved them by using assembly plugins.

Tutorial executed just fine. Thanks. Got to know a lot.

Anand
Guest
Anand

Hi Piero, did you get the answer to your problem because I’m also getting same error when I build the project “maven clean install”. Do we need to change anything in database.xml or do I need to install mysql on my local machine(mac laptop) ? Error ================================================== SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@342075b] to prepare test instance [com.mkyong.AppTest@63f0e908] java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:313) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:309)… Read more »

Anand
Guest
Anand

Got it working, downloaded MYSQL database and it worked with its default test schema.It was not working on my mac machine because mysql db on mac had some issue.

nguoi saigon
Guest
nguoi saigon

it works well, you should downloaded mysql and re-check with pass

sds
Guest
sds

kkk

Bart
Guest
Bart

Nice tutorial and introduction into spring batch. Not evryone has mysql installed. Could you use an in memory h2 database. That will work for evryone.

Arpit Bhargava
Guest
Arpit Bhargava

If someone is getting connection error, please use latest version of mysql. 8.0.13

Karen
Guest
Karen

Hi Thank you for the wonderful turotial!. I want to create a jar file of this code and then execute it on unix box. E.g java -cp spring-batch.jar com.mkyong.App But i am getting below mentioned exception Error: A JNI error has occurred, please check your installation and try again Exception in thread “main” java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at java.lang.Class.privateGetMethodRecursive(Class.java:3048) at java.lang.Class.getMethod0(Class.java:3018) at java.lang.Class.getMethod(Class.java:1784) at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526) Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) … 7 more How do i make it work? The unix box had java 8 installed.

Vinoth
Guest
Vinoth

Hi, Same error for me also. how to resolve it.?

Beth
Guest
Beth

Good tutorial, this requires MySql DB as a prerequisite and you may need to change the database connection to to match the default schema

Malcolm
Guest
Malcolm

Hi mkyong, I have a problem, I must to read a CSV file like:
Key1 Key2 Key3 ….more key
value1 value2 value3 ….
value11 value22 value33 ….

Help me solve it!!! Please…

Jeelan Yelidandla
Guest
Jeelan Yelidandla

Hello, Spring Batch StaxEventItemWriter doesnot provide formatted xml file in your example. So can you please let us know how can we generate formatted xml file.

Sachin
Guest
Sachin

The following files are missing from download zip code

Kindly update new ZIP code.

Steve Cook
Guest
Steve Cook

This is a “simple” example? I knew spring over engineered their crap, but I could have written it faster in assembler. (Ok, maybe that is a small exageration.)

Savani
Guest
Savani

Hi Mkyong – Could you please developed a code another way around? Please developed Spring Batch MongoDB to XML example. Also could you please guide on http://stackoverflow.com/questions/41500945/cannot-convert-value-of-type-org-springframework-batch-item-xml-staxeventitemwr

Imran Rajjad
Guest
Imran Rajjad

can we read jobs from a database instead of the xml configuration file? is there an annotation version of this example also available?

naveen
Guest
naveen

I am having the configuration like

even thought the job1 failed step is getting completed and going to the next step.

Prasanth Joy
Guest
Prasanth Joy

hi i am new to spring technology.i have an one query.i want to retrieve 15 millions records then i need validate as per my requirement and finally i need to insert into appropriate tables.So please suggest me what is solution for this task and which technology its suitable for this task. please suggest some example…. Advance ,Thanks.

Le Hoai
Guest
Le Hoai

Why we need a jobRepository is mysql while don’t save anything into database? (in this tutorial)

Osama Abdulsattar
Guest
Osama Abdulsattar

This is for SpringBatch itself to manage it’s jobs and restart failed Jobs if required

krunal
Guest
krunal

what kind of Program u made it is showing java.lang.NoClassDefFoundError: org/springframework/xml/transform/StaxResult

user12345
Guest
user12345

Hi I am new to spring batch and trying to run the application but getting error as

Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘step1’: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/retry/policy/RetryContextCache

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1037)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:983)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:807)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:737)

at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:529)

at org.springframework.batch.core.configuration.xml.CoreNamespacePostProcessor.injectJobRepositoryIntoSteps(CoreNamespacePostProcessor.java:71)

at org.springframework.batch.core.configuration.xml.CoreNamespacePostProcessor.postProcessBeanFactory(CoreNamespacePostProcessor.java:55)

at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)

at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:684)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)

at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)

at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:93)

at com.mkyong.App.main(App.java:18)

Caused by: java.lang.NoClassDefFoundError: org/springframework/retry/policy/RetryContextCache

at java.lang.Class.getDeclaredConstructors0(Native Method)

at java.lang.Class.privateGetDeclaredConstructors(Class.java:2493)

at java.lang.Class.getConstructor0(Class.java:2803)

at java.lang.Class.getDeclaredConstructor(Class.java:2053)

at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1030)

… 12 more

I have added all the required jars but not sure why this error. Can someone help me with this…

Surendra
Guest
Surendra

I got the same issue, I added the below jars , it is working fine.
spring-retry-1.0.2.RELEASE.jar
spring-oxm-3.2.0.release.jar

James
Guest
James

Hi

I tried using the example on IntelliJ but the program works fine with exactly same output in the console but I don’t any output in the report.xml. Do you know whats wrong please?

Srikant Das
Guest
Srikant Das

Hi, I am getting Error: Could not find or load main class com.mkyong.App after importing your project into sts and when i execute the App.java. please help me.

Kiran Jonnalagadda
Guest
Kiran Jonnalagadda

Me too having the same issue. Can anyone help?

umut
Guest
umut

where do you send the email?

abh
Guest
abh

Please write these tutorials with Java EE 7 batch also

Ashok
Guest
Ashok

Hi , I am getting the following exception while executing the code. Please let me know how i could resove the issue. SEVERE: Encountered an error executing the step

java.lang.IllegalStateExceptionjava.lang.IllegalStateException

: Marshaller must support the class of the marshalled object

at org.springframework.util.Assert.state(

at org.springframework.util.Assert.state(

Assert.java:385)

Erling
Guest
Erling

Late answer, but for reference you should check the Report class annotation:
@XmlRootElement(name = “record”)

Jorge
Guest
Jorge

Sorry I would like to know how I can do this

I want handling a transaction rollback esterna but falls in the second batch

Mohammed Qurashi
Guest
Mohammed Qurashi

Excellent tutorial. I was very easily able to implement it and later augment with my own code.What stand out is the professional,crisps explanation without getting bogged down to details. Great job.

Joy
Guest
Joy

Why every time it gives the result like this:

Exit Status : FAILED
Done

kyu
Guest
kyu

Thanks, This is very good post

Hardik Thakkar
Guest
Hardik Thakkar

I am getting below exception. Please help. org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [INSERT into BATCH_JOB_EXECUTION(JOB_EXECUTION_ID, JOB_INSTANCE_ID, START_TIME, END_TIME, STATUS, EXIT_CODE, EXIT_MESSAGE, VERSION, CREATE_TIME, LAST_UPDATED) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]; Data truncation: Incorrect datetime value: ” for column ‘CREATE_TIME’ at row 1; nested exception is com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: ” for column ‘CREATE_TIME’ at row 1 at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80) at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:811) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:867) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:871) at org.springframework.batch.core.repository.dao.JdbcJobExecutionDao.saveJobExecution(JdbcJobExecutionDao.java:138) at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:141) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)… Read more »

Girish
Guest
Girish

Is it possible to get more details about how to handle the exit codes while processing the batch application?

Thanks

Krishna
Guest
Krishna

thanks, I am really waiting for this type of information. Keep Posting..