Using JBoss ESB to Access REST services

Important note: JBoss ESB is an archived project and its latest release dates back to Mar 2013. The replacement technology for it is JBoss Fuse which is an open source enterprise integration platform and service bus. You can find here a quickstart tutorial about JBoss Fuse.

JBoss ESB has a lot of pre-defined actions which can be used to proxy request to other services/protocols. One interesting one is the HttpRouter which allows the invocation of external (ESB unaware) Http endpoints from an ESB action pipeline. This action uses Apache Commons HttpClient under the covers.

In this short tutorial we will see, how to proxy your REST service invocations with JBoss ESB.

 

jboss esb httprouter

Supposing you have defined a REST service with the following GET method:

 

package sample;

import javax.ws.rs.*;

@Path("tutorial")
public class HelloWorld
{

   @GET
   @Path("helloworld")
   public String helloworld() {
     return "Hello World!";
   }
}

In order to invoke this REST service we will configure an HttpRouter action in a basic  jboss-esb.xml file:

<?xml version = "1.0" encoding = "UTF-8"?>
<jbossesb xmlns="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.0.1.xsd" parameterReloadSecs="5">

 <providers>
   <jms-provider name="JBossMQ" connection-factory="ConnectionFactory">
     <jms-bus busid="quickstartGwChannel">
        <jms-message-filter dest-type="QUEUE" dest-name="queue/quickstart_helloworld_Request_gw"
 />
      </jms-bus>
      <jms-bus busid="quickstartEsbChannel">
         <jms-message-filter dest-type="QUEUE" dest-name="queue/quickstart_helloworld_Request_esb"
 />
      </jms-bus>

   </jms-provider>
 </providers>
 
 <services>


  <service category="FirstServiceESB" name="SimpleListener" description="Hello World">
    <listeners>
      <jms-listener name="JMS-Gateway" busidref="quickstartGwChannel" is-gateway="true" />
      <jms-listener name="helloWorld" busidref="quickstartEsbChannel" />
 </listeners>
 
     <actions mep="OneWay">

 

<!-- HttpRouter action -->
        <action name="httprouter"  class="org.jboss.soa.esb.actions.routing.http.HttpRouter">
         <property name="endpointUrl" value="http://localhost:8080/rest/tutorial/helloworld">
            <http-client-property name="file" value="/ht.props"/>
        </property>

        <property name="method" value="GET"/>    
        <property name="responseType" value="STRING"/>
 
     </action>     
 
     <action name="action2" class="org.jboss.soa.esb.actions.SystemPrintln">
        <property name="printfull" value="false"/>
     </action>
 
     <action name="testStore" class="org.jboss.soa.esb.actions.TestMessageStore"/>


    </actions>
  </service>
 </services>
 
</jbossesb>

Note: this action requires a file named ht.props at the root of the .esb archive which  contains client's http connection properties. Including a property file is optional, however it s is generally used for fine tuning the http connection. For example, here are two properties you might include in your ht.props file:

 

max-connections-per-host=100

max-total-connections=100

 

You might add a bit more of complexity to your service by passing Header parameters to your REST service:

@Path("callService")
public String callService(@HeaderParam("parameter1") String parameter1) {
...
}

Header parameters will be sent using the "header" element, inside the "property" section:

<action name="httprouter"  class="org.jboss.soa.esb.actions.routing.http.HttpRouter">
 <property name="endpointUrl" value="http://localhost:8080/rest/tutorial/callService">
 <http-client-property name="file" value="/ht.props"/>
 </property>

 <property name="method" value="GET"/>    
 <property name="responseType" value="STRING"/>
 
 <property name="headers">
 <header name="parameter1" value="blahval" ></header>
 </property>

 </action>

For your reference, here's the list of properties supported by the HttpRouter action:

 

Property Description Required
unwrap
Setting this to true (the default) will extract the message payload from the Message object before sending. false will send the serialized Message as either XML or Base64 encoded JavaSerialized object, based on the MessageType.
No
endpointUrl
The endpoint to which the message will be forwarded.
Yes
http-client-property
The HttpRouter uses the HttpClientFactory to create and configure the HttpClient instance. You can specify the configuration of the factory by using the file property which will point to a properties file on the local file system, classpath or URI based.
No
method
Currently only supports GET and POST.
Yes
responseType
Specifies in what form the response should be sent back. Either STRING or BYTES. Default value is STRING.
No
headers
To be added to the request. Supports multiple <header name="test" value="testvalue" /> elements.
No
MappedHeaderList
A comma separated list of header names that should be propagated to the target endpoint.
No

Follow us on Twitter