Overview on Jetty component of Apache Camel

background
Reading Time: 3 minutes

Introduction

Before going deep into Jetty component of Apache camel we will learn about camel. At the core, Camel framework is a routing engine, or more precisely a routing engine builder. It allows you to define your own routing rules, decide from which sources to accept messages, and determine how to process and send those messages to other destinations. Camel uses an integration language that allows you to define complex routing rules, akin to business processes.
These are the main ideas behind Camel:
■ Routing and mediation engine
■ Enterprise integration patterns (EIPs)
■ Domain-specific language (DSL)
■ Extensive component library
■ Payload-agnostic router
■ Modular and pluggable architecture
■ POJO model
■ Easy configuration
■ Automatic type converters
■ Lightweight core
■ Test kit
■ Vibrant community

One of the fundamental principles of Camel is that it makes no assumptions about the type of data you need to process. This is an important point, because it gives you, the developer, an opportunity to integrate any kind of system, without the need to change your data to canonical format.

Jetty Component

  • Jetty is free and open source Java HTTP (Web) server and Java Servlet container.
  • Apache Camel Jetty component provides HTTP based end points for consuming and producing HTTP requests.
  • Jetty component consumes and produces the http request/response and support GET, POST, PUT, DELETE method of forms.
  • Jetty is most commonly used as the start component in a Camel route.

Jetty Maven Dependency

if you’re using Maven, just add the following to your project’s POM:



<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-jetty</artifactId>
  <version>2.16.2</version>
</dependency>

Let’s create some simple service using Jetty!

We will create one example of Hello World using DSL, we can use the Spring DSL as well. First of all, we will define the from as from is the starting point in Apache Camel and then we have defined the jetty. Jetty indicates that this is the jetty component of Apache Camel and which indicates that we require the jetty server to access this END point given in example.

package com.camel.jetty;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class HelloWord {

	public static void main(String[] args) throws Exception {
		CamelContext context = new DefaultCamelContext();
		context.addRoutes(new RouteBuilder() {
			@Override
			public void configure() throws Exception {
				from("jetty:http://localhost:8080/hellowordjetty").process(new Processor() {
					public void process(Exchange exchange) throws Exception {
						Message out = exchange.getOut();
						out.setBody("Hello Jetty World!");
					}
				});
			}
		});
		context.start();
	}
}

Telling Camel to use the Jetty component and start a web server which listen on IP 127.0.0.1, port 8080, recieving requests at /hellowordjetty. Return Hello, Jetty World! in success response.

Testing the Jetty Service

Open your web browser and go to the address  http://localhost:8080/hellowordjetty.

You can also run the route from the command line by cding to your project folder and typing mvn exec:java.

Jetty URI Format

Jetty is most commonly used as the start component in a Camel route. The sample URI formats for the component is:

Pattern: jetty:http://{ip_address}:{port}/{path}

jetty:http://127.0.0.1:8080/hellowordjetty
jetty:http://127.0.0.1:8080/hellowordjetty?name=abc
jetty:http://0.0.0.1:8080/hellowordjetty
jetty:https://localhost:8080/hellowordjetty

Where ip_address is the address that Jetty should listen on, port is the HTTP port number, and path is the path of the endpoint. For example:

Now, we will see one example of HTTP session

HTTP Session Example Of Jetty

For using the HTTP session, we will have to pass option i.e sessionSupport=true as given below:

URI:jetty:https://localhost:8080/session/?sessionSupport=true

DSL

from("jetty:http://localhost:8080/session?sessionSupport=true").process(new Processor() {
					public void process(Exchange exchange) throws Exception {
						HttpSession session = exchange.getIn(HttpMessage.class).getRequest().getSession();
						Message out = exchange.getOut();
						out.setBody(session.getId());
					}
				});

HTTP Servlet Request Example of Jetty

from("jetty:http://localhost:8080/user/agent").process(new Processor() {
					public void process(Exchange exchange) throws Exception {
						HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class); 
						Message out = exchange.getOut();
						out.setBody(req.getHeader("User-Agent"));
					}
				});

Conclusion

We have seen the basic usage of the Jetty component like creating a simple service using Jetty. In coming blogs, we will see some detailed things related to Jetty component of Apache Camel. Continue learning about camel by clicking here.