Red Hat Training

A Red Hat training course is available for Red Hat Fuse

2.3. Working with Camel on JBoss EAP

Here are some basic examples that describe how the Camel subsystem interacts with JBoss EAP configuration files.
Note
For more working examples of the Camel subsystem and JBoss EAP, see the quickstarts provided with JBoss EAP.

2.3.1. Using a Camel Context

The camelContext represents a single Camel routing rulebase. It contains all the routes of your application. You can have as many camelContexts as necessary, provided they have different names.
Camel on EAP allows you to:
  • define a camelContext as a part of the subsystem definition in the standalone.xml and domain.xml files
  • deploy them in a supported deployment artifact that includes the -camel-context.xml suffixed file
  • provide camelContexts along with their routes via a RouteBuilder and the CDI integration
You can configure a camelContext as a part of the subsystem definition this way:
<route>
       <from uri="direct:start"/>
       <transform>
         <simple>Hello #{body}</simple>
       </transform>
</route>
Also, you can consume a defined camelContext two ways:
  • using annotation @Inject via camel-cdi
  • via JNDI tree
    Important
    To inject by CDI a deployed camelContext defined in Spring XML, you need to use the Java @Resource annotation, instead of the @Inject @ContextName annotations in the Camel CDI extension. Using the @Inject @ContextName annotations can result in the creation of a new camelContext instead of injecting the named context, which later causes endpoint lookups to fail.

2.3.1.1. Example of a Context and a Route

The following example, describes a context along with an associated route provided via CDI and a RouteBuilder. It displays an application scoped bean that starts automatically, when you start an application. The @ContextName annotation provides a specific name to the CamelContext.
@ApplicationScoped
@Startup
@ContextName("cdi-context")
public class HelloRouteBuilder extends RouteBuilder {

    @Inject
    HelloBean helloBean;

    @Override
    public void configure() throws Exception {
        from("direct:start").transform(body().prepend(helloBean.sayHello()).append(" user."));
    }
}

2.3.1.2. Configuring Camel Context using CDI Mechanism

Camel CDI automatically deploys and configures a CamelContext bean. After you initialise the CDI container, a CamelContext bean starts and instantiates automatically.
You can inject a CamelContext bean into the application as:
    @Inject
    @ContextName("cdi-context")
    private CamelContext context;

2.3.1.3. Configuring Camel Routes using CDI Mechanism

After you initialise the CDI container, Apache Camel CDI automatically collects all the RouteBuilder beans in the application, instantiates and adds them to the CamelContext bean instance.
For example, you can add a camel route and declare a class in the following way:
class MyRouteBean extends RouteBuilder {
 
    @Override
    public void configure() {
        from("jms:invoices").to("file:/invoices");
   }
  }

2.3.1.4. Customizing Camel Context

Apache Camel CDI provides @ContextName qualifier that allows you to change the name of the default CamelContext bean. For example:
@ApplicationScoped
class CustomCamelContext extends DefaultCamelContext {
 
    @PostConstruct
    void customize() {
        // Set the Camel context name
        setName("custom");
        // Disable JMX
        disableJMX();
    }
 
    @PreDestroy
    void cleanUp() {
        // ...
    }
}
Note
You can use any CamelContext class to declare a custom camel context bean.

2.3.1.5. Supporting Multiple CamelContexts

You can declare any number of CamelContext beans in your application. The CDI qualifiers declared on these CamelContext beans are used to bind the Camel routes and other Camel primitives to the corresponding Camel contexts.
The CDI qualifiers declared on the CamelContext beans are also used to bind the corresponding Camel primitives. For example:
@Inject
@ContextName("foo")
@Uri("direct:inbound")
ProducerTemplate producerTemplate;
 
@Inject
@BarContextQualifier
MockEndpoint outbound; // URI defaults to the member name, i.e. mock:outbound
 
@Inject
@ContextName("baz")
@Uri("direct:inbound")
Endpoint endpoint;

2.3.2. Camel Context Deployment

You can deploy a Camel context to JBoss EAP two ways:
  • Use the -camel-context.xml suffix as a part of another supported deployment, such as a JAR, WAR, or EAR deployment
    This deployment may contain multiple -camel-context.xml files.
  • Use the -camel-context.xml suffix in a standalone XML file deployment by dropping the file into the EAP deployment directory
A deployed Spring-based Camel context is CDI injectable as:
@Resource(name = "java:jboss/camel/context/mycontext")
CamelContext camelContext;
In this example, the string java:jboss/camel/context/mycontext is the name assigned the deployed camel context in the JNDI registry. mycontext is the xml:id of the camelContext element in the Camel context XML file.

2.3.3. Hawtio Web Console

HawtIO is a web application that runs in a JVM. You can start Hawtio on your machine:
  • deploy HawtIO as a WAR file
  • add some users to your management and application realms by using the following command: $ bin/add-user.sh
  • navigate to the http://localhost:8080/hawtio, the HawtIO login page appears
  • Click Camel in the top navigation bar to view all the running Camel Contexts
Apache Camel plugin allows you to browse all the running Camel applications in the current JVM. You can also view the following details:
  • list of all the running camel applications
  • detail information of each CamelContext such as Camel version number, runtime statics
  • list of all the routes and their runtime statistics in each camel application
  • manage the lifecycle of all camel applications and their routes
  • graphical representation of the running routes along with real time metrics
  • live tracing and debugging of running routes
  • profile the running routes with real time runtime statics
  • browse and send messages to camel endpoint

2.3.4. Selecting Components

If you add nested component or component-module XML elements, then instead of the default list of Camel components, only the specified elements will be added to your deployment.
For example:
<jboss umlns="urn:jboss:1.0">
    <jboss-camel xmlns="urn:jboss:jboss-camel:1.0">
        <component name="camel-ftp"/>
        <component-module name="org.apache.camel.component.rss"/>
    </jboss-camel>
</jboss>

2.3.5. Configuring Camel Subsystem

The Camel subsystem configuration may contain static system routes. However, these routes are started automatically.
<route>
    <from uri="direct:start"/>
    <transform>
        <simple>Hello #{body}</simple>
    </transform>
</route>

2.3.6. Configuring Camel Deployment

To make changes in the default configuration of your Camel deployment, you can edit either WEB-INF/jboss-all.xml or META-INF/jboss-all.xml configuration file.
Use a jboss-camel XML element within the jboss-all.xml file, to control the camel configuration.