Read more : http://www.ehow.com/how_8498918_remove-title-blogger-pages.html

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. 

Friday, 1 August 2014

Abstract Factory Design Pattern Example in Java

Hi,

"Abstarct factory " design pattern is used to creating objects of same type(family of same/related objects), without exposing their actual classes to the client or without specifying their classes explicitly.
Here we are mainly concentrating on creating the objects of family of  same type, and hence  this concept comes under creational pattern.

Please refer the below example for better understanding.


I have a factory interface  as follows

public interface FactoryInterface {
void sayHi();
void sayBye();
}

Below two classes implements factory interface

Women Implementation and
public class WomenImplementation implements FactoryInterface{

@Override
public void sayBye() {
System.out.println("Women saying Bye");
}

@Override
public void sayHi() {
System.out.println("Women saying Hi");
}

}

Men Implementation
public class MenImplementation implements FactoryInterface {

@Override
public void sayBye() {
System.out.println("Men saying Bye");
}

@Override
public void sayHi() {
System.out.println("Men Saying Hi");
}

}

If you observe women and men implemenations are belongs to same group or family of type FactoryInterface.

I have a factory controller which actually creates the objects of the classes.

public class FactoryController {
private static FactoryInterface factoryInterface=null;
public static FactoryInterface getInstance(String choice){
if(choice.equalsIgnoreCase("men")){
factoryInterface=new MenImplementation();
}else if(choice.equalsIgnoreCase("women")){
factoryInterface=new WomenImplementation();
}
return factoryInterface;
}

}

And my client class as follows

public class ClientFactory {
public static void main(String[] args) {
FactoryInterface fact1=FactoryController.getInstance("Men");
fact1.sayHi();
fact1.sayBye();
}
}

If you observe , client actually calling controller class by specifieng type of object he wants to create, he is not creating the object of the class, controller will takes care of creating the object of the class based on the client input , and we are completely hiding the data to the client, and only controller is visible to the client.

Please refer the class diagram for the above example.