Apache Camel: Jetty Component: IN PROGRESS -- NEEDS PROFILE PIC
Join the DZone community and get the full member experience.
Join For FreeApache Camel: Jetty Component
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>After adding jetty dependency please do clean and build your project.
<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>
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.
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.
Opinions expressed by DZone contributors are their own.
Comments