Monday, June 22, 2015

Apache Camel: Jetty Component

Today, I'm going to explain about the Camel's Jetty component and how to configure to the jetty component. Here, I used Spring DSL to configure the jetty component. So, let's start the camel riding.

Camel's jetty component is used for HTTP/S based request/response as like classical Servlet. Jetty component consumes and produces the http request/response and support GET, POST, DELTE, PUT methods of forms. It act as a servlet as we define J2EE web  project. It receive the request from the client, process it and create the response accordingly. With Jetty component we can create the HTTP based endpoints which consumes/produces http request.

Now, Let's start the programming. I assume that you have created your project by using Maven. I also assume that you have added all the dependency required for camel in your project.

Add the below dependency in your project's pom.xml

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jetty</artifactId>
    <version>X.X.X</version> <!-- Here, the version must be same as your project's camel version -->
</dependency>
After adding jetty dependency please do clean and build your project.

Now, I'm going to add the endpoints which receive the incoming request and respond it accordingly. Below is the configuration based on Spring DSL.

The  Spring-DSL for camel-jetty component,

<!-- Jetty Component -->
<camel:route id="httpComponent" startupOrder="1">
<camel:from uri="jetty:http://URL:PORT/makhir?continuationTimeout=100000" />
<camel:setExchangePattern pattern="InOut" />
<camel:convertBodyTo type="java.lang.String" charset="UTF-8"/>
<camel:to uri="log:Recived Input in httpComponent?showAll=true" />
<camel:choice>
<camel:when>
<camel:simple>${header.CamelHttpMethod} == "GET"</camel:simple>
<camel:to uri="direct:testGetRoute" />
</camel:when>
<camel:when>
<camel:simple>${header.CamelHttpMethod} == "POST"</camel:simple>
<camel:to uri="direct:splitterRoute" />
</camel:when>
</camel:choice>
</camel:route>

We can have same component using Java DSL also. I've created the jetty component which receives the incoming http request and serve accordingly as per below.



/**
 * This is the jetty component which creates the http endpoint which
 * consumes the http request and respond accordingly.
 * @author Ashish Mishra
 * @since 22-06-2015 15:02:05
 * */
public class JettyCommponent extends RouteBuilder{
@Override
public void configure() throws Exception {
from("jetty:http://localhost:8080/makhir?continuationTimeout=100000").process(
new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
String message = exchange.getIn().getBody(String.class);
System.out.println("Request Body Message : " + message);
String httpMethod = (String) exchange.getProperty("CamelHttpMethod");
System.out.println("Http Method : " + httpMethod);
if(httpMethod.equalsIgnoreCase("GET")){
System.out.println("Add implementation for GET method and route accordingly");
} else if(httpMethod.equalsIgnoreCase("POST")){
System.out.println("Add implementation for POST method and route accordingly");
} else
System.out.println("Default Implementation " + message);
exchange.getOut().setBody("Response: " + message);
}
});
}
}

Now, you should add this routes in your main class which have CamelContext object. About how to add the routes in camelcontext I've explained in my first blog related to camel.

That's it. Now build your project and deployed it in your ESB and try to call that endpoint by creating some java test class using http client.

You can use the Jetty component as per your need in your application. It gives web servlet like features in camel routing.

You can read more about camel:jetty component Apache Camel.

Hope, you have enjoyed this camel ride.

No comments:

Post a Comment