Hello World JBoss Fuse 7 application on JBoss EAP

In this tutorial we will create our first Java EE application which uses a Camel route and then we will deploy in on JBoss EAP with Fuse running on the top of it.

First of all, make sure you have installed JBoss Fuse as discussed in this tutorial:  Getting started with JBoss Fuse 7

Then, let's create a simple Web application which uses a Servlet:

@WebServlet(name = "HttpServiceServlet", urlPatterns = { "/*" }, loadOnStartup = 1)
public class MyServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@Resource(name = "java:jboss/camel/context/spring-context")
	private CamelContext camelContext;

    @Override
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String name = req.getParameter("name");
    	ServletOutputStream out = res.getOutputStream();
        ProducerTemplate producer = camelContext.createProducerTemplate();
        String result = producer.requestBody("direct:start", name, String.class);
    	out.print(result);
    }
}

As you can see, this Servlet gets injected the CamelContext whose name is "java:jboss/camel/context/spring-context". Then, in the doGet method, it starts a Producer, pushing a parameter from the Request into the direct:start component of the Route. The route is defined into the webapp/META-INF folder:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd        http://camel.apache.org/schema/spring https://camel.apache.org/schema/spring/camel-spring.xsd">
    <bean class="org.test.MyBean" id="helloBean" scope="singleton"/>
    <camelContext id="spring-context" xmlns="http://camel.apache.org/schema/spring">
        <route id="_route1">
            <from id="_from1" uri="direct:start"/>
            <bean id="_bean1" ref="helloBean"/>
        </route>
    </camelContext>
</beans>

This route will in turn call a CDI Bean referenced as "helloBean" (actual Bean is "org.test.MyBean") passing the Body produced in the direct:start Component.

Here is the CDI Bean:

public class MyBean {

    public String sayHello(String message) {
        return "Hello " + message;
    }
}

And here is the project final structure:

src
└── main
    ├── java
    │   └── org
    │       └── test
    │           ├── MyBean.java
    │           └── MyServlet.java
    └── webapp
        └── META-INF
            └── jboss-camel-context.xml


To build the project, the following dependencies have been added:

<dependencies>
	  <dependency>
	    <groupId>org.apache.camel</groupId>
	    <artifactId>camel-core</artifactId>
	    <version>3.2.0</version>
	   <scope>provided</scope>
	   </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-cdi</artifactId>
            <version>3.2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.enterprise</groupId>
            <artifactId>jakarta.enterprise.cdi-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.inject</groupId>
            <artifactId>jakarta.inject-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.servlet</groupId>
            <artifactId>jboss-servlet-api_4.0_spec</artifactId>
            <scope>provided</scope>
        </dependency>
</dependencies>

As we have included the WildFly Maven plugin in the pom.xml file, you can deploy it just using:

$ mvn install wildfly:deploy

You can test the Camel route as follows:

$ curl http://localhost:8080/hello-camel?name=frank
Hello frank

You can check from the Hawtio Console that the Camel Route has been correctly activated:

Fuse 7 on EAP Hello World

Conclusion

You have just managed to run your first Hello World CDI Application on JBoss Fuse. You can check the source code for this example at: https://github.com/fmarchioni/masteringintegration/tree/master/fuse/hello-fuse-eap