Saturday, February 5, 2011

STS Graph view for Spring Integration Configuration

The Spring Integration Graph View packaged with Spring Source Tool Suite (STS 2.5.2.RELEASE) is brilliant. 
The xml configuration for Spring Integration tends to get a little involved over time, the graphical view helps with visualizing the configuration well. Any high level gaps - no listener on a channel etc(bridge to nowhere?) will stand out in the graphical view -



I have faced one issue though and I have heard of one other in the forums:
  1. If I compose multiple flows into a single Spring configuration file (using import tags), the Graphical view cannot display the composed flow. 
  2. Different Spring Integration components can be created using annotations - these are also not reflected in the Visual view.

Monday, January 24, 2011

org.apache.cxf.service.factory.ServiceConstructionException: Could not resolve a binding for http://schemas.xmlsoap.org/wsdl/soap/

If you see the following at runtime:
org.apache.cxf.service.factory.ServiceConstructionException: Could not resolve a binding for http://schemas.xmlsoap.org/wsdl/soap/
add the following spring resources in your Spring context file:
 <import resource="classpath:META-INF/cxf/cxf.xml" />
 <import resource="classpath:META-INF/cxf/cxf-extension-http.xml" />
 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />

Gradle JDK 1.6 source compatibility

To configure JDK 1.6 compatibility in the generated eclipse JDT preferences file using gradle, add this to the build.gradle file:
eclipseJdt{
 whenConfigured { domain ->
  domain.sourceCompatibility=org.gradle.api.JavaVersion.toVersion("1.6")
  domain.targetCompatibility=org.gradle.api.JavaVersion.toVersion("1.6")
    }
}
Or something simpler, add the following to the build.gradle file:
sourceCompatibility=1.6
targetCompatibility=1.6

Sunday, September 12, 2010

Karate Chop - An Imperative and Functional implementation

Karate Chop is a Kata about implementing binary search five different ways. I could come up with 2 obvious ways - an imperative style with iteration and a functional style based on the solution inspired by this site . So here goes:

Imperative:

package org.bk.karatechop

class KarateChop {
 def chop(aNumber:Int, anArray:Array[Int]):Int = {
  var minIndex=0
  var maxIndex = anArray.length-1
  while(minIndex<=maxIndex){
   var midIndex = (minIndex+maxIndex)/2
   var atMidIndex = anArray(midIndex)
   if (aNumber>atMidIndex){
    minIndex=midIndex+1
   }else if(aNumber<atMidIndex){
    maxIndex=midIndex-1
   }else{
    return midIndex
   }
  }
  
  -1
  
 }
}
Functional: 
package org.bk.karatechop

class KarateChopRecursive {
 def chop(aNumber:Int, anArray:Array[Int]):Int = {
  
  def recurse(low:Int, high:Int):Option[Int] = { 
   (low + high)/2 match {
    case _ if high < low => None
    case mid if aNumber < anArray(mid)  => recurse(low, mid - 1)
    case mid if aNumber > anArray(mid)  => recurse(mid + 1, high)
    case mid => Some(mid)
   }   
  }
  
  recurse(0,anArray.length-1).getOrElse(-1)
  
  
 }
}

Tuesday, September 7, 2010

Perfect Number implementation using Scala Actor, Spring Integration, Java Executors, Kilim based Actor

A Simple non-threaded (and non-optimized) java implementation for figuring out if a number is a perfect number is the following:

public class SimplePerfectNumberUtil implements PerfectNumberUtil{

 @Override
 public boolean isPerfect(int aNumber) {
  return sumOfFactors(aNumber)==2*aNumber;
    }
 
 private int sumOfFactors(int aNumber){
  int sum = 0;
  for (int i=1;i<=aNumber;i++){
   if (aNumber%i==0){
    sum+=i;
   }
  }
  
  return sum;
 }

}



Programming Scala book provides a sample scala actor implementation for this by breaking up the task of determining whether a large number is a perfect number by breaking up the large number into a range of smaller numbers - eg. a number like 3000 can be split up into say 0-1000, 1001-2000, 2001-3000, with a task responsible for summing up the factors in each of these ranges and summing up the result.

A slightly modified implementation based on the Programming Scala book is the following:

class PerfectUtilScala {

  def isPerfect(candidate: Int) = {
    val RANGE = 10000000
    val numberOfPartitions = (candidate.toDouble / RANGE).ceil.toInt
    val caller = self
    
    
    val sumoffactorsActor = actor{
     loop {
      react {
    case msg:FactorsRangeForCandidate =>
     caller ! sumOfFactorsInRange(msg.lower, msg.upper, msg.candidate)
   }  
  } 
    }


    for (i <- 0 until numberOfPartitions) {
      val lower = i * RANGE + 1;
      val upper = candidate min (i + 1) * RANGE
      
      sumoffactorsActor ! new FactorsRangeForCandidate(lower, upper, candidate)
    }

    val sum = (0 /: (0 until numberOfPartitions)) { (partialSum, i) =>
      receive {
        case sumInRange: Int => partialSum + sumInRange
      }
    }

    2 * candidate == sum
  }

  private def sumOfFactorsInRange(lower: Int, upper: Int, number: Int) = {
    (0 /: (lower to upper)) { (sum, i) => if (number % i == 0) sum + i else sum }
  }

}


class FactorsRangeForCandidate(val lower:Int, val upper:Int, val candidate:Int)

A Java Executors based implementation is the following:
public class ThreadPoolPerfectNumberUtil implements PerfectNumberUtil{ 
 private ExecutorService threadpool = Executors.newFixedThreadPool(3);
 @SuppressWarnings("unchecked")
    public boolean isPerfect(int aNumber) {
     int RANGE = Constants.RANGE;
     int numberOfPartitions = new Double(Math.ceil(aNumber * 1.0 / RANGE)).intValue();
     Future[] sumOfFactors = new Future[numberOfPartitions];
     
     for (int i=0;i{

 private final int lower;
 private final int upper;
 private final int anumber;
 
 
 public SumOfFactorsTask(int lower, int upper, int anumber){
  this.lower = lower;
  this.upper = upper;
  this.anumber = anumber;
 }
 
 
 @Override
    public Integer call() {
  int sum=0;
  for (int i=lower;i<=upper;i++){
   if (anumber%i==0){
    sum+=i;
   }
  }  
  
  return sum;
    }

}
And a Kilim based actor implementation:

public class ActorsPerfectNumberUtil extends Task implements PerfectNumberUtil {

 private Mailbox mailbox;
 private Mailbox resultmailbox;
 private SumOfFactorsActor sumOfFactors;

 public ActorsPerfectNumberUtil() {
  mailbox = new Mailbox();
  resultmailbox = new Mailbox();
  sumOfFactors = new SumOfFactorsActor(mailbox, resultmailbox);
  sumOfFactors.start();

 }

 public boolean isPerfect(int aNumber) {
  int RANGE = Constants.RANGE;
  int numberOfPartitions = new Double(Math.ceil(aNumber * 1.0 / RANGE)).intValue();

  for (int i = 0; i < numberOfPartitions; i++) {
   int lower = i * RANGE + 1;
   int upper = (i + 1) * RANGE;
   if (aNumber < upper)
    upper = aNumber;
   mailbox.putnb(new FactorsRange(lower, upper, aNumber));
  }

  int sum = 0;
  for (int i = 0; i < numberOfPartitions; i++) {
    sum += resultmailbox.getb();
  }

  if (sum == 2*aNumber)
   return true;

  return false;
 }
}
The codebase along with the Spring Integration based implementation is available at this Git location:
git://github.com/bijukunjummen/Perfect-Number.git

Saturday, January 30, 2010

Windows 7 and Ubuntu - My perspective

A screenshot of my desktop under Ubuntu:




and, a screenshot of the similar set of applications in Windows 7:




A constant battle that plays out everyday in my mind - to use Ubuntu or Windows as my primary development box
I use both interchangeably in my dual boot laptop, most of my work related files is kept in a shared partition, this way I can use either Ubuntu or Windows any time. I could use a virtual machine hosting Windows or Ubuntu but I don't like the idea of allocating a good chunk of resources to run a Virtual machine in my "not a high end" laptop.


I will continue to use both for the foreseeable future, the following is just an attempt to list down the specific reasons why I like/hate Ubuntu and Windows and prevents me from sticking to one or the other:




What do I really love about Ubuntu:
  1. The super clean desktop, once I have changed the default Ubuntu look by adjusting the DPI settings using this article(https://wiki.ubuntu.com/X/Troubleshooting/HugeFonts) and changed the theme to Murrina Gilouche(http://gnome-look.org/content/show.php/MurrinaGilouche?content=44510)
  2. Terminal - For controlling the small scripts that I have  - to start subversion, confluence, bamboo, run maven scripts
  3. Package Management tools - Synaptic, aptitude and apt-get make software install a cinch.


What do I really hate about Ubuntu:
  1. Lack of a good pdf reader plugin support in Google Chrome/Firefox.  I know Adobe provides a pdf reader for Linux but it does not work well with Google Chrome/Firefox
  2. Lack of ITunes port for Linux. Yes there is Rhythmbox, but it does not for some reason synch my iPod Nano.
  3. Picasa - I know a port using Wine is available, but I would have preferred a native version.
  4. Flash experience is buggy - It works but there are bugs - for eg, I have issues in watching videos in full screen mode at TED, if I go into a fullscreen mode there are times when I hear the sound but no window.
  5. Java applications like Netbeans, JEdit with default look and feel(typically Metal) look very bad in Ubuntu, if the L&F is changed to GTK then it is passable.
  6. Hibernate and Resume is very slow(order of minutes) in Ubuntu.


What do I really love about Windows:
  1. Default Windows Aero based desktop is very clean and I like the new taskbar in Windows 7
  2. Flash, Pdf Reader(and plugin!), Itunes, Picasa work well on Windows
  3. Office suite - some of my work related documents does not open up correctly using Open office, and there is no equivalent of Visio in Open office.
  4. Very fast Hibernate and Resume


What do I really hate about Windows:
  1. The command shell and Powershell are not as good and intuitive as the Linux terminal(tab completion, color support etc). Yes there is Cygwin, but I do have problems in some applications related to the different way paths are handled(forward vs backward slash and the default "Program Files" space resolution)



Tuesday, December 29, 2009

Spring Integration and Apache Camel

Introduction:

Spring Integration and Apache Camel are open source frameworks providing a simpler solution for the Integration problems in the enterprise, to quote from their respective websites:


Apache Camel -

Apache Camel is a powerful open source integration framework based on known Enterprise Integration Patterns with powerful Bean Integration.

Spring Integration -
It provides an extension of the Spring programming model to support the well-known Enterprise Integration Patterns while building on the Spring Framework's existing support for enterprise integration.

Essentially Spring Integration and Apache Camel enable applications to integrate with other systems.

This article seeks to provide an implementation for an integration problem using both Spring Integration and Apache Camel. The objective is to show how easy it is to use these frameworks for a fairly complicated integration problem and to recommend either of these great products for your next Integration challenge.

Problem:

To illustrate the use of these frameworks consider a simple integration scenario, described using EIP terminology:

 

The application needs to get a "Report" by aggregating "Sections" from a Section XML over http service. Each request for Report consists of a set of request for sections – in this specific example there are requests for three sections, the header, body and footer. The XML over http service returns a Section for the Section Request. The responses need to be aggregated into a single report. A sample test for this scenario is of the following type:

ReportGenerator reportGenerator = reportGeneratorFactory.createReportGenerator();
List<SectionRequest> sectionRequests = new ArrayList<SectionRequest>();

String entityId="A Company";

sectionRequests.add(new SectionRequest(entityId,"header"));
sectionRequests.add(new SectionRequest(entityId,"body"));
sectionRequests.add(new SectionRequest(entityId,"footer"));        

ReportRequest reportRequest = new ReportRequest(sectionRequests);

Report report = reportGenerator.generateReport(reportRequest);
List<Section> sectionOfReport = report.getSections();
System.out.println(report);
assertEquals(3, sectionOfReport.size());

The “ReportGenerator” is the messaging gateway, hiding the details of the underlying messaging infrastructure and in this specific case also the integration API – Apache Camel or Spring Integration. To start with, let us implement a solution to this integration problem using Spring Integration as the Framework, followed by Apache Camel. The complete working code using Spring Integration and Apache Camel is also available with the article.

 

Solution Using Spring Integration:

The Gateway component is easily configured using the following entry in the Spring Configuration. Internally Spring Integration uses AOP to hook up a component which routes the requests from an internal input channel and waits for the response in the response channel.

<si:gateway default-reply-channel="exit" default-request-channel="enter" id="reportGenerator" service-interface="org.bk.report.ReportGenerator">
</si:gateway>

The component to Split the Input Report Request to Section Request is fairly straightforward:
public class SectionRequestSplitter {    
public List split(ReportRequest reportRequest){
return reportRequest.getSectionRequests();
}

}
and to hook this splitter with Spring Integration:
<bean class="org.bk.report.common.SectionRequestSplitter" id="sectionRequestSplitterBean">

<si:splitter id="sectionRequestSplitter" input-channel="enter" method="split" output-channel="sectionRequestToXMLChannel" ref="sectionRequestSplitterBean">

</si:splitter></bean>
Next, to transform the Section Request to an XML format - The component is the following:
public class SectionRequestToXMLTransformer {
public String transform(SectionRequest sectionRequest){
//this needs to be optimized...purely for demonstration of the concept
String sectionRequestAsString = "<section><meta><entityId>" + sectionRequest.getEntityId()
+ "</entityId><sectionName>" + sectionRequest.getSectionId()
+ "</sectionName></meta></section>";

return sectionRequestAsString;

}
}
and is hooked up in the Spring Integration configuration file in the following way:
<bean id="sectionRequestToXMLBean" class="org.bk.report.common.SectionRequestToXMLTransformer"/>
<si:transformer input-channel="sectionRequestToXMLChannel" ref="sectionRequestToXMLBean" method="transform" output-channel="sectionRequestChannel"/>
To send an XML over http request using the Section Request XML to a section Service:
<http:outbound-gateway id="httpHeaderGateway"
request-channel="sectionRequestChannel" reply-channel="sectionResponseChannel"
default-url="${sectionBaseURL}/section" extract-request-payload="true"
charset="UTF-8" request-timeout="1200" />

To transform the Section Response XML to a Section Object - The component is the following:
public class SectionResponseXMLToSectionTransformer {
public Section transform(String sectionXML) {
SAXReader saxReader = new SAXReader();
Document document;
String sectionName = "";
String entityId = "";
try {
document = saxReader.read(new StringReader(sectionXML));

sectionName = document
.selectSingleNode("/section/meta/sectionName").getText();
entityId = document.selectSingleNode("/section/meta/entityId")
.getText();
} catch (DocumentException e) {
e.printStackTrace();
}
return new Section(entityId, sectionName, sectionXML);
}
}
and is hooked up in the Spring Integration configuration file in the following way:
<bean id="sectionResponseXMLToSectionBean" class="org.bk.report.common.SectionResponseXMLToSectionTransformer" />
<si:transformer input-channel="sectionXMLResponseChannel"
ref="sectionResponseXMLToSectionBean" method="transform" output-channel="sectionResponseChannel" />


To aggregate the Sections together into a report, the component is the following::
public class SectionResponseAggregator {

public Report aggregate(List<Section> sections) {
return new Report(sections);
}

}

and is hooked up in the Spring Integration configuration file in the following way:
<bean id="sectionResponseAggregator" class="org.bk.report.common.SectionResponseAggregator"/>
<si:aggregator input-channel="sectionResponseChannel" output-channel="exit" ref="sectionResponseAggregator" method="aggregate"/>

This completes the Spring Integration implementation for this Integration Problem. The following is the complete Spring Integration configuration file:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:si="http://www.springframework.org/schema/integration"
xmlns:http="http://www.springframework.org/schema/integration/http"
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.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-1.0.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-1.0.xsd
">

<context:property-placeholder />
<si:channel id="enter" />

<si:channel id="exit" />

<si:gateway id="reportGenerator" default-request-channel="enter"
default-reply-channel="exit" service-interface="org.bk.report.ReportGenerator" />

<si:channel id="sectionRequestToXMLChannel" />
<si:splitter id="sectionRequestSplitter" input-channel="enter"
ref="sectionRequestSplitterBean" method="split" output-channel="sectionRequestToXMLChannel" />

<si:channel id="sectionRequestChannel" />
<si:transformer input-channel="sectionRequestToXMLChannel"
ref="sectionRequestToXMLBean" method="transform" output-channel="sectionRequestChannel" />

<si:channel id="sectionXMLResponseChannel" />
<http:outbound-gateway id="httpHeaderGateway"
request-channel="sectionRequestChannel" reply-channel="sectionXMLResponseChannel"
default-url="${sectionBaseURL}/section" extract-request-payload="true"
charset="UTF-8" request-timeout="1200" />

<si:channel id="sectionResponseChannel" />
<si:transformer input-channel="sectionXMLResponseChannel"
ref="sectionResponseXMLToSectionBean" method="transform" output-channel="sectionResponseChannel" />

<si:aggregator input-channel="sectionResponseChannel"
output-channel="exit" ref="sectionResponseAggregator" method="aggregate" />

<bean id="sectionRequestSplitterBean" class="org.bk.report.common.SectionRequestSplitter" />
<bean id="sectionRequestToXMLBean" class="org.bk.report.common.SectionRequestToXMLTransformer" />
<bean id="sectionResponseXMLToSectionBean" class="org.bk.report.common.SectionResponseXMLToSectionTransformer" />
<bean id="sectionResponseAggregator" class="org.bk.report.common.SectionResponseAggregator" />

</beans>

 

A working sample is provided with the article(Download, extract and run "mvn test")

 

Solution using Apache Camel:

Apache Camel allows the route to be defined using multiple DSL implementations – Java DSL, Scala DSL and an XML based DSL. The recommended approach is to use Spring CamelContext as a runtime and the Java DSL for route development. The following is to build the Spring Camel Context:

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<template id="camelTemplate" />
<routeBuilder ref="routeBuilder"/>
</camelContext>


The route is configured by the Java based DSL:
public class CamelRouteBuilder extends RouteBuilder {
private String serviceURL;

@Override
public void configure() throws Exception {

from("direct:start")
.split().method("sectionRequestSplitterBean", "split")
.aggregationStrategy(new ReportAggregationStrategy())
.transform().method("sectionRequestToXMLBean", "transform")
.to(serviceURL)
.transform().method("sectionResponseXMLToSectionBean", "transform");
}

public void setServiceURL(String serviceURL) {
this.serviceURL = serviceURL;
}
}
Apache Camel does not provide an out of the box Message Gateway feature, however it is fairly easy to create a wrapper component that can hide the underlying details in the following way:
public class CamelReportGenerator implements ReportGenerator {
private ProducerTemplate producer;


@Override
public Report generateReport(ReportRequest reportRequest) {
Report report = (Report)producer.requestBody("direct:start", reportRequest);  
return report;
}

public void setProducer(ProducerTemplate producer) {
this.producer = producer;
}

}
This is a simple wrapper around Apache Camel's Producer Template which is essentially a handle to Apache Camel's route for this specific integration problem. The component to Split the Input Report Request to Section Request is exactly same as Spring Integration component:
public class SectionRequestSplitter {

public List<SectionRequest> split(ReportRequest reportRequest){
return reportRequest.getSectionRequests();
}

}
To hook the component with Apache Camel:
<bean id="sectionRequestSplitterBean" class="org.bk.report.si.SectionRequestSplitter" />

from("direct:start")
.split().method("sectionRequestSplitterBean", "split")
....
Next to transform the Section Request to an XML format, again this is exactly same as the implementation for Spring Integration, with hook being provided in the following manner:
......
.transform().method("sectionRequestToXMLBean", "transform")
......
To send an XML over http request using the Section Request XML to a section Service:
......
.transform().method("sectionRequestToXMLBean", "transform")
.to(serviceURL)
.........
To transform the Section Response XML to a Section object, the component is exactly same as the one used with Spring Integration, with the following highlighted hook in the Camel route:
......
.transform().method("sectionResponseXMLToSectionBean", "transform");

To aggregate the Section responses together into a report, the component is a bit more complicated than Spring Integration. Apache Camel supports a Scatter/Gather pattern using a route of the following type:
......
.split().method("sectionRequestSplitterBean", "split")
.aggregationStrategy(new ReportAggregationStrategy())
with an aggregation strategy being passed on to the Splitter, the aggregation strategy implementation is the following:
public class ReportAggregationStrategy implements AggregationStrategy {
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
if (oldExchange == null) {
Section section = newExchange.getIn().getBody(Section.class);
Report report = new Report();
report.addSection(section);
newExchange.getIn().setBody(report);
return newExchange;
}

Report report = oldExchange.getIn().getBody(Report.class);
Section section = newExchange.getIn().getBody(Section.class);
report.addSection(section);
oldExchange.getIn().setBody(report);
return oldExchange;

}
}
This completes the Apache Camel based implementation. A working sample for Camel is provided with the article - just download, extract and run "mvn test".


Conclusion:

Spring Integration and Apache Camel provide a simple and clean approach for the Integration problems in a typical enterprise. They are lightweight frameworks – Spring Integration builds on top of Spring portfolio and extends the familiar programming model for the Integration domain and is easy to pick up, Apache camel provides a good Java based DSL and integrates well with Spring Core, with a fairly gentle learning curve. The article does not recommend one product over the other but encourages the reader to evaluate and learn from both these frameworks.


References:

Spring Integration Website: http://www.springsource.org/spring-integration
Apache Camel Website: http://camel.apache.org/
Spring Integration Reference: http://static.springsource.org/spring-integration/reference/htmlsingle/spring-integration-reference.html
Apache Camel User Guide: http://camel.apache.org/user-guide.html
Plug for my blog: http://biju-allandsundry.blogspot.com/
Spring Integration Implementation for the Problem: http://sites.google.com/site/bijukunjummen/reportgenerator-springintegration.zip
Apache Camel Implementation for the Problem: http://sites.google.com/site/bijukunjummen/reportgenerator-camel.zip