Red Hat JBoss Fuse: Integrating Database, Java Bean, and Restful Services in EAP, Spring DSL

DZone 's Guide to

Red Hat JBoss Fuse: Integrating Database, Java Bean, and Restful Services in EAP, Spring DSL

Trying to integrate database, Java Bean, and Restful services into EAP? Here's an awesome JBoss Fust tutorial.

· Integration Zone ·
Free Resource

We have talked about how to start develop application in EAP with Spring DSL, out of all the three ways of developing Camel in EAP, this is probably easiest to do to. And in this post we are going to take that example a bit more further, show you how to integrate with Databases, Java beans in the application, creating a servlet and lastly setup a Restful webservice. 

For this currency exchange project, I have store the exchange rate in the h2 database. What we are going to do is to display the entire exchange rate on the webpage provided by a restful web service and provide a servlet that takes in the amount of money and the currency to exchange to, send them back to server, calculated and return the result. So here are the things I am going to do

  • Database
  • Java Bean
  • Servlet
  • Restful Web Services


Of course we want to use the datasources in EAP. So first thing first, create the datasource in EAP. Make sure you started the JBoss EAP. Go to Configuration, select Connector and then to datasources.

  • Name :MyCamelDS
  • JNDI Name: java:jboss/datasources/MyCamelDS

 Select the Driver we are going to use, and it's h2 this case,

Enter the

  • Connection URL: jdbc:h2:file:~/h2/fuseoneap;AUTO_SERVER=TRUE
  • Username: sa
  • Password: 

Change the min and max pool size to 1 and 10. And then enable the MyCamelDS datasource.

Once we get the datasource ready, we can then use it by looking it up in our Spring Camel DSL.

Add the new jee namespace in the Spring XML file,

<beans xmlns="http://www.springframework.org/schema/beans"
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd

       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">

Lookup the datasource in JNDI, by using jndi-lookup provided by jee. 

<jee:jndi-lookup id="MyCamelDS" jndi-name="java:jboss/datasources/MyCamelDS"/>

And then we can go on using the datasource with our SQL component, the same as we would use it in other container.

  <route id="allcurrencies">
    <from uri="direct:getCurrencies"/>
    <to uri="sql:select * from currencyexchange?dataSource=MyCamelDS"/>
      <json library="Jackson"/>

Java Bean

Create a Java class that has a method takes in amount of money and currency to exchange.

  public class CurrencyConvertor {
    public double convertUSD(double amt, ArrayList<Map<String, Object>> data){
      Double rate = (Double)data.get(0).get("rate");
      return amt*rate;

In Spring Camel Route, the same as in normal Fuse Application, binding the Java bean we created in the context.

   <bean id="currencyconvertor" class="com.redhat.demo01.CurrencyConvertor"/>

And call the bind bean using the Bean Component. 

  <route id="covertcurrency">
    <from uri="direct:getCurrency"/>
    <log message="Got currency: ${headers.amt} and amt${headers.currency}"/>
        <to uri="sql:select * from currencyexchange where currencycode= :#currency ?dataSource=MyCamelDS"/>
        <log message="exchange rate ====&gt; ${body[0][rate]}"/>
        <bean ref="currencyconvertor" method="convertUSD(${headers.amt},${body})"/>
        <log message="nothing to lookup"/>
          <constant>nothing to lookup</constant>


We need to setup the Servlet, under webapp, create the WEB-INF/web.xml file. Right click on Deployment Descriptor and choose Generate Deployment Descriptor Stub. It will create an empty web.xml file for you.

Add the servlet setting in the web.xml and publish it 



Once it's done, we can then use the Servlet component in Camel, it will publish a servlet endpoint through the Servlet we set earlier.

  <route id="servletCurrencies">
    <from uri="servlet:/currencies?servletName=camel&amp;matchOnUriPrefix=true"/>
    <to uri="direct:getCurrencies"/>

Restful Web Service

The Restful web services needs to have the CamelHttpTransportServlet setup too, since we have already done that in Servlet, all we have to do is to builds Rest endpoints as consumers in Rest DSL. And then configure it to servlet.

  <restConfiguration component="servlet" bindingMode="json" contextPath="/camel">
      <dataFormatProperty key="prettyPrint" value="true"/>
    <rest path="/currenciesrest">
      <get uri="/">
        <to uri="direct:getCurrencies"/>

That's all you have to do. 

camel, enterprise java beans, rest, spring

Published at DZone with permission of Christina Lin , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}