Over a million developers have joined DZone.

Red Hat JBoss Fuse: 3 Ways to Develop Your Fuse Application in JBoss EAP, Java DSL

Trying to develop a Camel web app in JBoss EAP? Here are three different methods.

· Integration Zone

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

In this post, I am going to show the different ways of developing a Camel web application running in JBoss EAP.

  • Spring Framework
  • CDI with Java DSL
  • XML without Spring

To make things clear and easy, I will be using the exact same Camel route for all three different methods. What this Camel route does is it starts up a timer that will log every 5 seconds.

Before we start develop our simple camel route, we need a base WAR project to work on, so first we need to create a WAR file. I am sure you must have a million way to create that, every one sort of have their own best practice for this. For me, in the example, I will create one with Maven, using the webapp-javaee6 artifact. 

A. Create Project, select Maven Project. 

Image title

B. Click Next when you are at New Maven Project, and it will take you to the next step, select the webapp-javaee6 artifact.

Image title

C. Add your project name, and then click Next, you will have a ready to go WAR project in your JBoss Developer Studio. (Note, for my examples later, I am going to give them different project Names).

Image title

D. Add Camel dependency in the pom.xml file (do make sure your Maven repo is configure properly pointing to http://repo.fusesource.com/nexus/content/repositories/releases)

    <!-- camel -->
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
      <version>2.15.1.redhat-621xxx</version>
      <scope>provided</scope>
    </dependency>

E. Optional, change your JDK version accordingly in pom.xml of the WAR project.

Image title

Java DSL

The major difference between Karaf and JBoss EAP is that JBoss EAP supports JavaEE specification. From JavaEE 6, it introduces a new framework, Contexts and Dependency injection, similar to what Spring has done, allows users to defines a type-safe dependency injection as well as provide a well managed "contextual" references lifecycle for Java beans. In many cases, we want to take advantage of this standard and let it manage our bean in Camel. To lookup these beans in Camel route, we need to use the Java DSL.  

Before we start, we are going to need another Camel dependency, 

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-cdi</artifactId>
            <version>2.15.1.redhat-620133</version>
            <scope>provided</scope>
        </dependency>

Enable CDI in your web application by placing an empty beans.xml under WEB-INF in webapp folder. Create folder WEB-INF , and XML file beans.xml

Image title

Image title

This is what your beans.xml should look like:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="all">
</beans>

Now I am going to show you how to create one, create a Java Class that extends org.apache.camel.builder.RouteBuilder, called MyCamelRouteBuilder.java

Image title

Inside the method , add the following Camel Route, 

    from("timer:mytimer?period=5000")
    .log("Java DSL Camel Demo");

Add your camel route to the application scope, register it's context,  and start up by setting the following annotation on top of your class.

    • @Startup
    • @ApplicationScoped
    • @ContextName("cdi-context")

Your Java Class will look like this:

 package com.redhat.mycdidemo;

import org.apache.camel.builder.RouteBuilder;

@Startup
@ApplicationScoped
@ContextName("cdi-context")
public class MyCamelRouteBuilder extends RouteBuilder {

  @Override
  public void configure() throws Exception {
    from("timer:mytimer?period=5000")
    .log("Java DSL Camel Demo");
  }
}

And then we are done, ready to go. Run mvn clean install to generate the war file. 

Image title

Image title

Start up your JBoss EAP with Fuse subsystem enabled. If you are not sure how to do it, please take a look at this post. In side bin folder, start JBoss EAP with 

    • standalone.sh
    • standalone.bat

Go to http://localhost:9990/, login to admin console, under Runtime/

Image title

Click on Add, choose the war generated under the project target folder. Click Next, Next to deploy the application. 

Image title

Start up application by clicking on the En/Disable button. 

Image title

Switch to the log, you will find it's now printing the message "Java DSL Camel Demo" every 5 seconds.

Image title

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

Topics:
java ,spring ,jboss

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

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}