Home » Camel » Running Apache Camel in OpenESB

Running Apache Camel in OpenESB

Introduction

Apache Camel is a widely used java based integration framework. Apache camel’s strengh lies in its simple,light weight and modular approach as well as its easy-to-use implementation of Gregor Hoppe’s Enterprise Integration Patterns. It is also a very light weight framework that can also be easily embedded with other applications as a library.

In Open ESB, we can utilise Camel for all its strength ranging from routing & mediation, to utilising the camel components which are missing in open esb, to integrating existing Camel applications with Open ESB. We now have a service engine called Camel SE which will act as a bridge between JBI and Camel technologies. By having a Service Engine for Apache Camel, OpenESB can utilize Apache camel by participating in message exchanges. This way we have both the technologies collaborating seamlessly. For example, HTTP BC can receive a message and can send it to Camel SE which can mediate, route, process the message, can invoke OpenESB’s JMS BC and can reply to HTTP BC. The following blog is a tutorial on how exactly we can achieve this.

In this entry I will write a step by step guide on how to use the new Camel Service Engine to utilize Apache Camel in Open ESB. We will use OpenESB’s HTTP BC to receive http soap message, send it to Apache Active MQ’s queue, send it to Camel Processor and then reply to sender.

You need to have both the runtime and design time modules of Camel SE to try this.

Installing Runtime jar

1. Download the runtime service engine jar from here: http://hudson.openesb-dev.org:8080/hudson/job/openesb-components-git/lastSuccessfulBuild/artifact/dist/camelse.jar

2. Go to Servers -> Glassfish v2.x and start the server. Login to admin console

3. Navigate to JBI -> Service Engines. Right click on it and install the runtime jar

4. During installation provide Apache Camel Home and the library jars to include in classpath. For this tutorial, add this entry for library jars: “lib/camel-core-2.11.0.jar,lib/camel-spring-2.11.0.jar,lib/camel-jms-2.11.0.jar”

5. In Additional Libraries, provide comma separated full path of these jars. Note: These are only required for this tutorial. You may need additional jars or may not need all the jars. It depends on your implementation:

org.springframework.core-3.1.4.RELEASE.jar
org.springframework.expression-3.1.4.RELEASE.jar
org.springframework.jms-3.1.4.RELEASE.jar
org.springframework.transaction-3.1.4.RELEASE.jar
slf4j-api-1.7.5.jar
slf4j-nop-1.7.5.jar
activemq-all-5.8.0.jar
commons-logging-1.1.2.jar
org.springframework.aop-3.1.4.RELEASE.jar
org.springframework.asm-3.1.4.RELEASE.jar
org.springframework.beans-3.1.4.RELEASE.jar
org.springframework.context-3.1.4.RELEASE.jar

Installing the Netbeans Plugin

1. At this point of writing, the nbm plugin is not available in OpenESB hudson yet. So I have attached the nbm for convenience: org-openesb-components-camelse-nb-plugin. Extract the zip file to obtain the .nbm plugin

2. Go to Tools -> Plugins -> Downloaded tab. Click on “Add Plugins” and select the .nbm file.

3. Follow the wizard. Restart Netbeans to complete the installation

Once you have the design time plugins install, you should be able to create a new “Camel JBI Module”

1. Go to File -> New Project. Choose SOA and select “Camel JBI Module” from the right pane. Click Next.1

2. Provide a Project Name, Project Location, a package name for the source code and location of Apache Camel Home. Currently the Camel SE is tested with Apache Camel version 2.11.0.

2

3. You can find that a new project is created with a spring context xml file and a AppRouteBuilder.java. camel-context.xml lets you initialize the components, activate endpoints and routing logic. AppRouteBuilder.java lets you write routing logic for the message flow34. You can also find a wsdl file named jbi2camel.wsdl and xsd named jbi2camel.xsd are created. This is the wsdl file camel se uses to declare its endpoint information to other components. The default wsdl can work only for one way message exchange. To utilise camel se in In-Out exchanges, we need to modify the wsdl’s operation with output message. Change the operation as below:

<operation name="twoWay">
  <input name="twoWayIn" message="tns:anyMsg"/>
  <output name="twoWayOut" message="tns:anyMsg"/>
</operation>

5. Since we are going to send message to Apache MQ’s queue, we will use Camel’s JMS component to do this. To use Camel’s JMS component, we need to declare it in camel-context.xml.
Add the below snippet in camel-context.xml:

<bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
  <property name="connectionFactory">
    <bean class="org.apache.activemq.ActiveMQConnectionFactory">
      <property name="brokerURL" value="vm://localhost?broker.persistent=false"/>
    </bean>
  </property>
</bean>

The above snippet declares the JMS component which uses an embedded ActiveMQ message broker

6. Next, we will write the routing logic in AppRouteBuilder.java.  The default logic in configure() shows us a sample routing logic. It reads an xml from a folder which contains a person’s data. If the person’s city is ‘London’, the file is moved to ‘uk’ folder. Otherwise it is moved to ‘others’ folder. You can try this scenario if you like. But I’m going to show you how to utilize OpenESB’s HTTP BC to receive message and utilise it in Camel Se. Lets delete these lines so that we can write our routing logic.

public void configure() {

   from("jms:test.MyQueue").to("file:c:/temp/camel/?noop=true"); // (A)

    String jbiURI = "jbi:http://openesb.org/jbi2camel/CamelJBIModule1/CamelJBIModule1_service/jbi2camel_endpoint"; //(B)
    from(jbiURI).process(new Processor() { //(C)
       public void process(Exchange exchng) throws Exception {

          Message inMessage = exchng.getIn();
          String strInMsg = inMessage.getBody(String.class);
          System.out.println("Received in Processor : " + strInMsg);
          String strOutMsg = strInMsg.replace("My String", "MyReply");

          exchng.getOut().setBody(strOutMsg); // (D)

      }
    }).to(ExchangePattern.InOnly,"jms:test.MyQueue"); //(E)
 }

Code snippet explained:

(A) – Routing any message from “MyQueue” to a file at c:/temp/camel/
(B) – This is the jbi endpoint of our camel service unit. For every camel project, there is a .wsdl file which declares its endpoint information to OpenESB’s NMR. Any component which needs to send a message to Camel SE uses this endpoint to send its message. This URI can be constructed by concatenating jbi:<target namespace>/<service name>/<port name> from the wsdl.
(C) – Routing from jbiURI to an ActiveMQ queue named “MyQueue” and also multicasted to a “org.apache.camel.Processor”.
(D) – The processor processes the message and sets the reply message.
(E) – The input message is then sent to queue. Here it is necessary to mention ExchangePattern.InOnly because the jms component checks the exchange type, if it is InOut, it sets some output message which gets overwritten with the output we set in Processor.

7. Clean and build the project

8. Create a new Composite Application.

9. Double click on “Service Assembly”. It will open the CASA editor.

10. Now drag the project CamelJBIModule1 to “JBI Modules” pane in CASA.

11. Build the composite application

4

12. Drag the “soap” binding from “WSDL Bindings” palette to “WSDL Ports” pane in CASA.

513. Drag the SOAP endpoint to jbi2_camel_endpoint as show in the picture.

614. Clean and build the composite application

15. Start the server and deploy the composite application

16. Create a TestCase by Right Clicking on Test and selecting “New Test Case”

17. Select the wsdl file from composite application project. Select the operation and click on finish.

7

18. Edit your input.xml and provide “My String” as input string. Remember our code looks for “My String” and replaces with “MyReply”?

<soapenv:Body>
  <cam:AnyMessage>My String</cam:AnyMessage>
</soapenv:Body>

19. Run the project. First time it will show as failure because the expected output is blank. Check the output returned to make sure you got the reply and set it as expected output.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*