Tutorials on Apache Camel, Apache Kafka, JBoss Fuse

Search our tutorials

This tutorial will teach you how to design Camel routes using Code Read Studio Camel Route editor. We will also learn other possible ways to design Camel routes, for example installing the IntelliJ Plugin.

To get started you need to download Code Read Studio and install it. Check this resource to learn more about Red Hat Code Ready Studio: Creating JBoss Fuse projects with Red Hat Code Ready Studio

We will use as starting point a simple Camel Spring project we have created using just the shell:

mvn archetype:generate                   \
  -DarchetypeGroupId=org.apache.camel.archetypes  \
  -DarchetypeArtifactId=camel-archetype-spring   \
  -DarchetypeVersion=3.0.0 \
  -DgroupId=com.sample \
  -DartifactId=camel-demo \
  -Dversion=1.0-SNAPSHOT

From Code Ready studio, Import the Project as Maven project. You will see that, by selecting the camel-context.xml file, the designer will show the structure of the default route generated by the archetype:

How to design camel routes

By selecting any of the Components in the Route, you will be able to edit its properties from the Properties Panel, which is located in the lower part of the UI:

How to design camel routes

To run the default application, just right-click on the camel-context.xml file and choose Run As | Local Camel Context.

Creating a new Route with the Designer

As a proof of concept, let's create a brand new Route which will be in charge to invoke a remote JAX-WS Service.

First off, let's add to your pom.xml a dependency required to run the Camel-CXF Component:

<dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-cxf</artifactId>
</dependency>

Then, in your camel-context.xml include the remote JAX-WS endpoint just before the Camel context definition:

<cxf:cxfEndpoint
        address="http://localhost:8080/DemoWS/SimpleWebService"
        id="SimpleWebService" serviceClass="com.sample.SimpleWebService" wsdlURL="http://localhost:8080/DemoWS/SimpleWebService?wsdl"/>

Then, delete the existing Route (or just its content)so that you will start from an empty Route:

How to design camel routes

Now Add a Timer Component at the beginning of the Route:

How to design camel routes

In the Properties of the Timer component we have configured it to 'timer:foo?repeatCount=1' so just a single execution will be triggered.

Then, from the Transformation accordion list, pick up a Set Body and connect it to the Timer:

How to design camel routes

The SetBody uses a Simple Transformation to set the Body to "Hello World!".

Next, connect the Set Body to a Log component (available in the Components accordion list):

How to design camel routes

In its properties, select to log "${body}" so that the content of the Body will be logged.

How to design camel routes

Next, from the Components accordion list, add a CXF Component:

How to design camel routes

  • In the Properties Detail Tab set the target URL of the CXF Service to: "cxf:bean:SimpleWebService?defaultOperationName=hello"
  • In the Properties Advanced/Path Tab set as Address "SimpleWebService" and beanId "bean"
  • In the Properties Advanced/Common Tab set as Data Format "POJO"
  • In the Properties Advanced/Producer Tab set as Operation Name "hello"

Lastly, add a Mock component, which will terminate the Route, configured with the URL "mock:result"

How to design camel routes

If you have followed all the steps so far, the following camel-context.xml should be generated:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:cxf="http://camel.apache.org/schema/cxf"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd     ">
    <cxf:cxfEndpoint
        address="http://localhost:8080/DemoWS/SimpleWebService"
        id="SimpleWebService" serviceClass="com.sample.SimpleWebService" wsdlURL="http://localhost:8080/DemoWS/SimpleWebService?wsdl"/>
    <camelContext id="test_context" xmlns="http://camel.apache.org/schema/spring">
        <route id="wsClient">
            <from id="_from1" uri="timer:foo?repeatCount=1"/>
            <setBody id="_setBody1">
                <simple>Hello World!</simple>
            </setBody>
            <log id="_log1" message="${body}"/>
            <to id="_to1" uri="cxf:bean:SimpleWebService?defaultOperationName=hello"/>
            <to id="_to2" uri="mock:result"/>
        </route>
    </camelContext>
</beans>

Before running the project, the target Web service must be available

Also, you need to add to your project the remote service dependency or just add a no-op implementation of its methods:

package com.sample;

public class SimpleWebService implements Simple {

	// Not the actual endpoint
	@Override
	public String hello(String s) {
		return null;
	}

}

package com.sample;

public interface Simple {
	  public String hello(String s);
}

To run the default application, just right-click on the camel-context.xml file and choose Run As | Local Camel Context.

You can grab the source code for this example at: https://github.com/fmarchioni/masteringintegration/tree/master/camel-jaxws

Using IntelliJ Idea Plugin to edit your Camel Routes

Another option to design your Camel route is to install a plugin for your IDE. We recommend checking IntelliJ Idea plugin for Camel which is free and works pretty well to help you coding your Camel routes.

To  get started you need to have an available IntelliJ Idea IDE (Community or Ultimate). If not, you can download it from here: https://www.jetbrains.com/idea/

Once started the IDE, select File | Settings | plugins. From there, search the Camel plugin:

how to design camel routes

It will be required to restart the project.

When you open a project which includes a Caeml

Although this plugin does not let you design routes visually, it is of great help when you are coding your routes either in Java or in XML. One of the most useful features is the ability to autocomplete code for Camel endpoints, property placeholders and Bean methods which are inside Camel DSL by hitting Control + Space:

camel how to design route

Summary

In this tutorial we have covered some options to learn how to design Camel Routes. By using Red Hat Code Ready studio you will be able to code and design visually your Routes. On the other hand, if you just need assistance in code completion, quick navigation between Routes, quick link to the Documentation, the IntelliJ Plugin for Camel works like a charm.

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials