Read more : http://www.ehow.com/how_8498918_remove-title-blogger-pages.html
Showing posts with label Spring Batch Working Examples. Show all posts
Showing posts with label Spring Batch Working Examples. Show all posts

Thursday, 14 August 2014

Spring Batch Condition Based Example

Hi,

It is possible to apply if else logic in spring batch, usually we will call this as condition based execution of steps.
In brief if the condition is true go and execute step1 or go and execute step2.
To achieve if - else logic in spring batch we have to use decision tag provided by spring batch.

Following example explains how to achieve if else logic in spring batch.

For More examples on Spring batch and Java .. Please refer

jobs.xml

<!-- *************************************************************************************************************************************** -->
<!-- Condition Based excutions (Same as If/Else logic in Java) -->
*************************************************************************************************************************************** -->

<batch:job id="conditionJob" parent="simpleJob">
<batch:step id="masterBrain" next="decision">
<batch:tasklet ref="dummyJob" />
</batch:step>
<batch:decision decider="decider" id="decision">
<batch:next on="masterMind" to="masterMind" />
<batch:next on="dummyMind" to="dummyMind" />
</batch:decision>

<batch:step id="masterMind">
<batch:tasklet ref="sayHello"/>
</batch:step>
<batch:step id="dummyMind">
 <batch:tasklet ref="sayBye"/>
</batch:step>
</batch:job>

           <!-- Decision maker -->
<bean id="decider" class="com.springbatchdemo.utils.Decider"/>
<bean id="dummyJob" class="com.springbatchdemo.tasklet.Dummy" />
        <bean id="sayHello" class="com.springbatchdemo.tasklet.SayHello" />
        <bean id="sayBye" class="com.springbatchdemo.tasklet.SayBye" />

Decider.java
public class Decider implements JobExecutionDecider{
@Override
public FlowExecutionStatus decide(JobExecution arg0, StepExecution arg1) {
if(true){
return new FlowExecutionStatus("masterMind");
}else{
return new FlowExecutionStatus("dummyMind");
}

}

Dummy.java
public class Dummy implements Tasklet
{
 @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception
{
 return RepeatStatus.FINISHED;
 }
 }


SayHello.java

public class SayHello implements Tasklet 

{
 @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception
{
System.out.println("From Hi");
 return RepeatStatus.FINISHED;
 }
 }

SayBye.java
public class SayBye implements Tasklet 

{
 @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception
{
System.out.println("From Bye");
 return RepeatStatus.FINISHED;
 }
 }

Hope  above small example help you in designing condition based implementation in spring batch. 

Thursday, 1 May 2014

Parallel Step Example

Hi,

Spring batch provides so many features for java batch processing , parallel step process is one of the feature of spring batch frame work.

In the parallel step process we can execute / process independent steps at a time, this will reduce Process time of the job.

Parallel step process is a flavor of multi-threaded process provided by spring batch frame work

Please note this feature will be  used when there is no dependency exists between the steps of a job process.

Following examples explain you how to configure parallel step execution

In this example I am using two tasklets for two steps

1) Tasklet1 prints Hello From Tasklet1
2) Tasklet2  prints Hello from Tasklet2

Job Configuration is as follows:

<batch:job id="parallelStepProcessing">
 <batch:split id="parallelSplit"  task-executor="parallelExecutor">
 <batch:flow>
  <batch:step id="hiTaskelt1">
<batch:tasklet ref="helloTasklet1" />
</batch:step>
</batch:flow>
<batch:flow>
<batch:step id="hiTasklet2">
<batch:tasklet ref="haiTasklet2" />
</batch:step>
</batch:flow>
</batch:split>
</batch:job>
 <bean id="parallelExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>

<bean id="helloTasklet1" class="packagename.Tasklet1"/>

<bean id="haiTasklet2" class="packagename.Tasklet2"/>

Please note that a flow should have atleast one step

class Tasklet1 implements Tasklet{
@Override
public RepeatStatus execute(StepContribution stepContri, ChunkContext context) throws Exception{
                 System.out.println("Hello from tasklet1");
return RepeatStatus.FINISHED;

}
}

class Tasklet2 implements Tasklet{
@Override
public RepeatStatus execute(StepContribution stepContri, ChunkContext context) throws Exception{
                 System.out.println("Hello from tasklet2");
return RepeatStatus.FINISHED;

}
}

Please refer http://docs.spring.io/spring-batch/reference/html/scalability.html for more details
Hope this will help you in designing spring batch parallel step process..