Over a million developers have joined DZone.

Using Camel-Undertow Component for Supporting an http2 Connection

DZone's Guide to

Using Camel-Undertow Component for Supporting an http2 Connection

In this article, and integration developers gives some help on how to configure http2 protocol support for the camel-undertow component.

Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

This article will help you to configure http2 protocol support for the camel-undertow component.

  • Camel's undertow component uses an embedded undertow web-container of version undertow-core:jar:1.4.21. This version also supports the http2 connection.
  • I have used the Camel version 2.21.0-SNAPSHOT from upstream: https://github.com/apache/camel.
  • Also, the version to test applications using the camel-undertow component is 7.53.1. This curl version supports tje -http2 flag for sending an http2 request.
  • I have also used to test application from a Linux terminal. However, this article is not about http2 insights.
  • For http2 details, I found articles [1] and [2] helpful.

1. The project structure is depicted below.

[csp@dhcppc1 undertow-camel-testing]$ tree
├── pom.xml
├── README.md
└── src
    └── main
        └── resources
            └── META-INF
                └── spring
                    └── camel-context.xml

2. In pom.xml, we would have to set up the following dependencies.


3. With the version of Camel we're using:


4. Now, one can use a Spring-based dsl and configure the camel-undertow component as I have below. Let's say we named this file, camel-context.xml.

<beans xmlns="http://www.springframework.org/schema/beans"
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/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext id="cbr-example-context" xmlns="http://camel.apache.org/schema/spring">
<route id="cbr-route" trace="true">
<from id="_from1" uri="undertow:http://localhost:7766/foo/bar"/>
<setBody id="_setBody1">
<constant&gt;Sending Response&lt;/constant>
<log id="_log5" message="Headers ${in.headers}"/>
<log id="_log5" message="Done processing ${body}"/>
<bean class="org.apache.camel.component.undertow.UndertowComponent" id="undertow">
<property name="hostOptions" ref="undertowHostOptions"/>
class="org.apache.camel.component.undertow.UndertowHostOptions" id="undertowHostOptions">
<property name="http2Enabled" value="true"/>

5. Point to the note above http2Enabled and make sure it is set to true. For the UndertowHostOptions class, by default it is set to false. This UndertowHostOptions class is then referred to UndertowComponent, which is then used in the camel route.
6. We can use camel-maven-plugin in pom.xml then we can run this using maven command: 

mvn camel:run 


7. Once successfully run, this should expose an HTTP service at http://localhost:7766/foo/bar.
8. We can test this using curl and nghttp commands from the Linux terminal. I used Fedora 26 where the curl command with http2 support was available. For RHEL7, I used the nghttp utility to test the http2 connection.
9. Using the curl command:

[csp@dhcppc1 undertow-camel-testing]$ curl -v --http2 http://localhost:7766/foo/bar
* Trying ::1...
* connect to ::1 port 7766 failed: Connection refused
* Trying
* Connected to localhost ( port 7766 (#0)
> GET /foo/bar HTTP/1.1
> Host: localhost:7766
> User-Agent: curl/7.53.1
> Accept: */*
> Connection: Upgrade, HTTP2-Settings
> Upgrade: h2c
< HTTP/1.1 101 Switching Protocols
< Connection: Upgrade
< Upgrade: h2c
< Date: Sun, 10 Dec 2017 08:43:58 GMT
* Received 101
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
< HTTP/2 200 
< accept: */*
< breadcrumbid: ID-dhcppc1-1512886066149-0-25
< content-length: 16
< user-agent: curl/7.53.1
< date: Sun, 10 Dec 2017 08:43:58 GMT
* Connection #0 to host localhost left intact
Sending Response

10. Using the nghttp command:

cpandey@cpandey camel-undertow]$ nghttp -v http://localhost:7766/foo/bar
[ERROR] Could not connect to the address ::1
Trying next address
[  0.000] Connected
[  0.000] send SETTINGS frame <length=12, flags=0x00, stream_id=0>
[  0.000] send PRIORITY frame <length=5, flags=0x00, stream_id=3>
          (dep_stream_id=0, weight=201, exclusive=0)
[  0.000] send PRIORITY frame <length=5, flags=0x00, stream_id=5>
          (dep_stream_id=0, weight=101, exclusive=0)
[  0.000] send PRIORITY frame <length=5, flags=0x00, stream_id=7>
          (dep_stream_id=0, weight=1, exclusive=0)
[  0.000] send PRIORITY frame <length=5, flags=0x00, stream_id=9>
          (dep_stream_id=7, weight=1, exclusive=0)
[  0.000] send PRIORITY frame <length=5, flags=0x00, stream_id=11>
          (dep_stream_id=3, weight=1, exclusive=0)
[  0.000] send HEADERS frame <length=45, flags=0x25, stream_id=13>
          (padlen=0, dep_stream_id=11, weight=16, exclusive=0)
          ; Open new stream
          :method: GET
          :path: /foo/bar
          :scheme: http
          :authority: localhost:7766
          accept: */*
          accept-encoding: gzip, deflate
          user-agent: nghttp2/1.21.1
[  0.001] recv SETTINGS frame <length=18, flags=0x00, stream_id=0>
[  0.001] send SETTINGS frame <length=0, flags=0x01, stream_id=0>
          ; ACK
[  0.001] recv SETTINGS frame <length=0, flags=0x01, stream_id=0>
          ; ACK
[  0.003] recv (stream_id=13) :status: 200
[  0.003] recv (stream_id=13) accept: */*
[  0.003] recv (stream_id=13) accept-encoding: gzip, deflate
[  0.003] recv (stream_id=13) breadcrumbid: ID-cpandey-pnq-csb-1512577017865-0-3
[  0.003] recv (stream_id=13, sensitive) content-length: 16
[  0.003] recv (stream_id=13) user-agent: nghttp2/1.21.1
[  0.003] recv (stream_id=13, sensitive) date: Wed, 06 Dec 2017 17:11:51 GMT
[  0.003] recv HEADERS frame <length=88, flags=0x04, stream_id=13>
          ; END_HEADERS
          ; First response header
Sending Response[  0.003] recv DATA frame <length=16, flags=0x01, stream_id=13>
          ; END_STREAM
[  0.003] send GOAWAY frame <length=8, flags=0x00, stream_id=0>
          (last_stream_id=0, error_code=NO_ERROR(0x00), opaque_data(0)=[])


1. http://undertow.io/blog/2015/04/27/An-in-depth-overview-of-HTTP2.html
2. https://developers.google.com/web/fundamentals/performance/http2/

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

integration ,apache camel ,http2

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}