A Simple Camel Route in WildFly Swarm
Integrations using Camel are pretty flexible. When working within a JavaEE environment you will want to use CDI, these instructions will get you up and running.
Join the DZone community and get the full member experience.
Join For FreeTo get started with WildFly Swarm and Camel, we’ll build an application that makes use of some common JavaEE features as part of a simple Camel route.
Camel is quite flexible when it comes to the environments it will integrate with. One of the decisions you need to make is which registry Camel will use to look up POJOs. Camel supports a wide variety of registries including JNDI, Spring ApplicationContext, a simple Map, or CDI. Since we are working in a JavaEE environment, we’ll make use of CDI.
With that in mind, let's take a look at the build script.
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath "org.wildfly.swarm:wildfly-swarm-plugin:1.0.0.Beta2"
}
}
group 'com.matthewcasperson'
version '1.0-SNAPSHOT'
apply plugin: 'war'
apply plugin: 'wildfly-swarm'
sourceCompatibility = 1.8
def swarmVersion = '1.0.0.Beta2'
def camelVersion = '2.17.0'
repositories {
mavenCentral()
mavenLocal()
maven {
url 'http://repository.jboss.org/nexus/content/groups/public-jboss'
}
maven {
url 'https://maven.repository.redhat.com/nexus/content/repositories/public'
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'com.sun.xml.bind:jaxb-core:2.2.11'
compile 'com.sun.xml.bind:jaxb-impl:2.2.11'
compile 'org.wildfly.swarm:bootstrap:' + swarmVersion
compile 'org.wildfly.swarm:undertow:' + swarmVersion
compile 'org.wildfly.swarm:weld:' + swarmVersion
compile 'org.apache.camel:camel-core:' + camelVersion
compile 'org.apache.camel:camel-servletlistener:' + camelVersion
compile 'org.apache.camel:camel-servlet:' + camelVersion
compile 'org.apache.camel:camel-cdi:' + camelVersion
}
// For heroku
task stage {
dependsOn build
}
We have included dependencies on the Swarm CDI library called Weld, as well as Undertow, which is the WildFly web server. Additionally we have pulled in some Camel dependencies that let us interact with servlet requests and reference CDI as a bean registry.
The web.xml file is an amalgamation of the examples provided in the servlet listener and servlet documentation examples. We use these two pieces of the Camel library to allow routes to be called via a Servlet mapping, and to kickstart Camel when the application is started.
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>My Web Application</display-name>
<!-- you can configure any of the properties on CamelContext, eg setName will be configured as below -->
<context-param>
<param-name>name</param-name>
<param-value>MyCamel</param-value>
</context-param>
<!-- the listener that kick-starts Camel -->
<listener>
<listener-class>org.apache.camel.component.servletlistener.SimpleCamelServletContextListener</listener-class>
</listener>
<!-- Camel servlet used in the Camel application -->
<servlet>
<servlet-name>CamelServlet</servlet-name>
<servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Camel servlet mapping -->
<servlet-mapping>
<servlet-name>CamelServlet</servlet-name>
<url-pattern>/camel/*</url-pattern>
</servlet-mapping>
</web-app>
Alongside the web.xml file is a blank file called beans.xml. This is used as a marker file to enable CDI in our application.
This sample application then has two classes. The first is a pretty standard CDI bean. We have declared it as a singleton, given it a custom name of myBean, and defined a single method called sayHello.
The two parameters that are taken by the sayHello method have custom Camel annotations. The first, @Header, is used to reference a value from the Camel message header. The second, @Simple, is a Camel simple language expression which references a system property.
package com.matthewcasperson.swarmdemo;
import org.apache.camel.Header;
import org.apache.camel.language.Simple;
import javax.inject.Named;
import javax.inject.Singleton;
/**
* A CDI bean that can be consumed by Camel
*/
@Singleton
@Named("myBean")
public class HelloBean {
public String sayHello(@Header("name") final String name, @Simple("${sys.user.country}") final String country) {
return "Hello " + name + ", how are you? You are from: " + country;
}
}
The second class is where we define our Camel route. Note that we don’t have to do anything more than define this class, and the Camel CDI integration will auto-detect it. The route we have defined accepts a request from the servlet we defined in the web.xml file, and passes the incoming data to an instance of our HelloBean class. Note that we reference the bean by its custom name of myBean, and not by the class name. Our CDI bean then returns a String, which is passed back to the caller.
package com.matthewcasperson.swarmdemo;
import org.apache.camel.builder.RouteBuilder;
/**
* A camel route that uses our CDI bean
*/
class MyRoute extends RouteBuilder {
@Override
public void configure() {
from("servlet:hello").to("bean:myBean");
}
}
Once compiled, you can call http://localhost:8080/camel/hello?name=Bob, and you will get the response of the myBean bean back.
I have taken the liberty of uploading this sample project to Heroku, so you can click the following link and see this Camel route in action: https://wildfly-swarm-camel-1.herokuapp.com/camel/hello?name=Matt.
The source code for this project can be found at GitHub.
You can find articles in this series here.
Published at DZone with permission of Matthew Casperson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments