In this tutorial, we will show you how to read data from a MySQL database, with JdbcCursorItemReader and JdbcPagingItemReader, and write it into an XML file. Tools and libraries used Maven 3 Eclipse 4.2 JDK 1.6 Spring Core 3.2.2.RELEASE Spring OXM 3.2.2.RELEASE Spring Batch 2.2.0.RELEASE MySQL Java Driver 5.1.25 P.S This example – MySQL jdbc […]

Read more Spring Batch Example – MySQL Database To XML

In Spring batch, there are six “listeners” to intercept the step execution, I believe the class name should be self-explanatory. StepExecutionListener ItemReadListener ItemProcessListener ItemWriteListener ChunkListener SkipListener 1. Listener Example Three listener examples, do nothing but print out a message. CustomStepListener.java package com.mkyong.listeners; import org.springframework.batch.core.ExitStatus; import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.StepExecutionListener; public class CustomStepListener implements StepExecutionListener { @Override […]

Read more Spring Batch listeners example

A quick guide to show you how to run a Spring batch job with CommandLineJobRunner. 1. Spring Batch Job Example A simple job. resources/spring/batch/jobs/job-read-files.xml <?xml version="1.0" encoding="UTF-8"?> <beans … <import resource="../config/context.xml"/> <job id="readJob" xmlns="http://www.springframework.org/schema/batch"> <step id="step1"> <tasklet> <chunk reader="flatFileItemReader" writer="flatFileItemWriter" commit-interval="1" /> </tasklet> </step> </job> <!– … –> </beans> 2. Package Project Use Maven to […]

Read more Run Spring batch job with CommandLineJobRunner

Read following Spring batch job, it reads data from “domain.csv“, and map it to a domain object. job-example.xml <bean class="org.springframework.batch.item.file.FlatFileItemReader" > <property name="resource" value="file:outputs/csv/domain.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, domainName, lastModifiedDate" /> </bean> </property> <property name="fieldSetMapper"> <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper"> <property name="prototypeBeanName" value="domain" /> </bean> </property> </bean> </property> </bean> <bean […]

Read more How to convert Date in BeanWrapperFieldSetMapper

Create a simple Spring batch job to write data to a csv file. The csv file name depends on the pass in job’s parameters, interprets by Spring EL . job-sample.xml <bean id="csvFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter"> <!– write to this csv file –> <property name="resource" value="file:outputs/csv/domain.done.#{jobParameters[‘pid’]}.csv" /> <property name="appendAllowed" value="false" /> <property name="lineAggregator"> <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator"> <property name="delimiter" value="," […]

Read more jobParameters cannot be found on object of type BeanExpressionContext

Following the official Spring batch unit testing guide to create a standard unit test case. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring/batch/jobs/job-abc.xml", "classpath:spring/batch/config/context.xml"}) public class AppTest { @Autowired private JobLauncherTestUtils jobLauncherTestUtils; @Test public void launchJob() throws Exception { JobExecution jobExecution = jobLauncherTestUtils.launchJob(); assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); } } P.S spring-batch-test.jar is added to the classpath. Problem When launches above […]

Read more NoSuchBeanDefinitionException : No qualifying bean of type JobLauncherTestUtils

If jobRepository is created with MapJobRepositoryFactoryBean (metadata in memory), the Spring batch jobs are running successfully. spring-config.xml <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"> <property name="transactionManager" ref="transactionManager" /> </bean> Problem After changing the jobRepository to store metadata into database : spring-config.xml <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="jobLauncher" […]

Read more Spring Batch metadata tables are not created automatically?

Working with Spring Batch 2.2.0.RELEASE, and launches the job with Spring Scheduler. CustomJobLauncher.java package com.mkyong.batch; 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.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class CustomJobLauncher { @Autowired JobLauncher jobLauncher; @Autowired Job job; public void run() { try { JobExecution execution = jobLauncher.run(job, new JobParameters()); System.out.println("Exit Status : " + […]

Read more Spring Batch : A job instance already exists and is complete for parameters={}