avr

28

Posted by : admin | On : 28 avril 2012

what is Spring batch ?

Spring batch is a module of Spring Framework that focus more specifically on Batch processing .
The current real time processing (au fils de l’eau) can be used when we want our data to be refresh or treated rapidly by our system.
Meanwhile, when we have huge amount of opertaion to realize or not priorize treatement to be done we will prefer to have a batch processing .

In this article we’ll try to have an overview on how spring batch is working
Basically we need data in entrance of our feed and output this data in a different format regarding the rule defined in our processor
-> reader -> proccessor -> Writer (see example M1)
Spring batch overlay this cycle with a step.
Note that a subtask of that step can be realized by a tasklet (see example M2)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:batch="http://www.springframework.org/schema/batch"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

	<!-- Define and declare your steps to realize by spring batch -->
	<!--Example step M1 -->
	<batch:step id="step1">
		<batch:tasklet>
			<batch:chunk reader="flatReader"
					processor="customProcessor"
					writer="LineWriter"
					commit-interval="10" />
		</batch:tasklet>
	</batch:step> 

	<!--Define a custom tasklet to execute during the step of batch processing -->
	<batch:step id="step2">
			<batch:tasklet ref="getCacheTasklet" />
	</batch:step>

	<!--Example step  M2-->
	<util:map id="cacheReferenceItemMap" />
	<bean id="getCacheTasklet" class="org.example.tasklet.GetReferenceItemTasklet" p:cacheReferenceItemMap-ref="cacheReferenceItemMap" />
</beans>

Java code of the tasklet

 

package org.example.tasklet;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

public class GetReferenceItemTasklet implements Tasklet {
    Map<String, String> cacheReferenceItemMap;
    String fileName;

    /**
     * @param cacheReferenceItemMap
     *  the cacheReferenceItemMap represents our cache structure.
     */
    public void setCobDateMap(final Map<String, String> cacheReferenceItemMap) {
        this.cacheReferenceItemMap = cacheReferenceItemMap;
    }

    /**
     * @param fileName
     *
     */
    public void setScratchFiles(final String fileName) {
        this.fileName = fileName;
    }

    /*
     * (non-Javadoc)
     * example : we have our name file : 0411Myfile405480.csv
	 * we are retrieving 0411 number with this method .
     * @seeorg.springframework.batch.core.step.tasklet.Tasklet#execute(org.
     * springframework.batch.core.StepContribution,
     * org.springframework.batch.core.scope.context.ChunkContext)
     */
    public RepeatStatus execute(final StepContribution contribution, final ChunkContext chunkContext) throws Exception {
        Pattern pattern = Pattern.compile("(\\d{4})MyFile\\d+\\.csv");
         Matcher matcher = pattern.matcher(fileName);
         if (matcher.find()) {
			this.cacheReferenceItemMap.put(fileName, matcher.group(1));
		 } else {
			throw new Exception("ERROR: Can't get file name error " + fileName);
         }
        }
        return RepeatStatus.FINISHED;
    }
}

 

 

ezez