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.
Firstly, make sure you have installed Red Hat Fuse as discussed in this tutorial: Getting started with JBoss Fuse 7
Then, we will create a simple Web application that uses the CamelContext to produce some Messages.
Here is our Servlet Class which gets loaded at startup:
@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, we recommend using the Fuse Bill of Materials which are available on GitHub: https://github.com/jboss-fuse/redhat-fuse
In our example, as we are running Red Hat Fuse on EAP, you should use the following BOM:
<dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.redhat-fuse</groupId> <artifactId>fuse-eap-bom</artifactId> <version>${fuse.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Please check at the end of this article for the source code to see the list of dependencies. Build the project and deploy it on JBoss EAP:
$ mvn install
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:
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/masterspringboot/tree/master/camel/fuse/hello-fuse-eap
Found the article helpful? if so please follow us on Socials