Using Camel with WildFly - part 2

User Rating: 4 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Inactive
 

This is the second part tutorial about using Camel with WildFly. We will now learn how to use Camel routes within our applications.

First of all, check that you have installed Camel subsystem as described in this tutorial: Riding the Camel with WildFly application server

Using Camel with Java EE CDI applications is even easier than using Camel standalone, as you directly inject into your beans the Camel Context. Basically, two things should be done to use Apache Camel in a CDI environment:

  1. First, you need to create a BootStrap class which will be use by the Java EE container to start the Camel Context. The Cdi CamelContext when instantiated will add a CDI Bean Registry. That will allow Camel to perform lookup of beans injected and registered in CDI container. 
  2. Next, we must add CDI annotated beans (@Inject, @Named, ...) to use them from the Apache Camel routes.

Let's see a very basic example of a Route which takes as input the direct component and performs a transformation by adding a String at the beginning of it:

import javax.ejb.Startup;
import javax.enterprise.context.ApplicationScoped;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.cdi.ContextName;

@Startup
@ApplicationScoped
@ContextName("cdi-context")

public class BasicRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
       from("direct:start").transform(body().prepend("Hi ")).log(body().toString()); 
    }

}

The above route will be consumed by the following CDI beans which takes as input the message and produces the body for the Camel route:

@Model
public class Manager {
	@Inject
	@ContextName("cdi-context")
	private CamelContext camelctx;
	
	private String message;
	private String output;


	public void send() {
		ProducerTemplate producer = ctx.createProducerTemplate();
		output = producer.requestBody("direct:start", message, String.class);
	}
	
	public String getOutput() {
		return output;
	}

	public void setOutput(String output) {
		this.output = output;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}

In the send method, the ProducerTemplate interface allows you to send message exchanges to endpoints thus setting the request body with the message field.

Here is a minimal index.xhtml page which fires the send method passing the message field:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
	<h:outputStylesheet name="style.css" />
</h:head>

<h:body>
	<h2>Camel Basic demo</h2>
	<h:form id="formpanel">
		<h:panelGrid columns="2" styleClass="default">

			<h:outputLabel for="message" value="Enter Message: " />

			<h:inputText id="message" value="#{manager.message}" />
			<h:commandButton actionListener="#{manager.send}"
				styleClass="buttons" value="Simple Route" />
			<h:outputText id="response" value="#{manager.output}" />

			<br />
			<ui:debug />
		</h:panelGrid>

	</h:form>

</h:body>
</html>

That's all! In order to compile the project you will need, besides the Java EE stack the Camel libraries which have been exploded in the modules of the application server:

camel wildfly tutorial

Here is our simple route, seen from the hawtio console (http://localhost:8080/hawtio/jmx/attributes?tab=camel):

camel wildfly tutorial

You can check by entering a message that it will be printed on the outputText field:

camel wildfly tutorial

Calling a JAX-WS Web Service using Camel

Actually there are lots of Camel Components already available to use. Let's see a slightly moe complex example which uses the following JAX-WS Web service to print the greeting message

@WebService 
public class GreetingService implements Greeting {
 
 public String greet(@WebParam(name = "message") String message){
	 System.out.println("Called Web service with "+message);
         return "Hi "+ message  ;
 }
}

And the Service Interface follows here:

public interface Greeting {
	public String greet(String message);
}

Now redefining your route to use the Web service is pretty easy and you don't need even to add extra classes:

@Startup
@ApplicationScoped
@ContextName("cdi-context")
public class BasicRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        	from("direct:start")
        .to("cxf://http://localhost:8080/camel/GreetingService?defaultOperationName=greet&serviceClass=" + GreetingService.class.getName());
    
    }

}

So all we have changed is the route destination ("to") which now points to a CXF component available at: http://localhost:8080/camel/GreetingService 

We don't need to change any line of code in our Manager class which will now trigger the Greeting Web service passing as argument the message field as well.

Here is our new route on hawtio:

camel wildfly tutorial

We have just scratched the surface of some of the available components in Came. I encourage you to take a look at the wiki http://wildflyext.gitbooks.io/wildfly-camel/content/ and try some Components. You can see how little effort it takes to create and deploy standard Camel components in Java EE applications.

You might be also interested in this article on mastertheintegration.com: Calling a JAX-WS Service from Camel 

 

Related articles available on mastertheboss.com

Wildfly 8 introduction

JBoss AS has a new name: as you probably know future versions of

Configuring Eclipse to use WildFly

This tutorial shows how to install JBoss Tools 4.3.1 plugin 

Maven configuration for Java EE 7 projects on WildFly

In this tutorial we will learn how to configure Maven Project Obj

Running WildFly with JDK 8

The JDK 1.8 early access is available for download at Oracle's si

Patching WildFly application server

The final version of WildFly 8.1.0 has been released and it conta

Configuring WildFly Batch Job Repository

WildFly ships with a subsystem named batch which is the administr