microBean™ Jersey Netty Integration

Build Status Maven Central

The microBean™ Jersey Netty Integration project integrates Jersey into Netty in an idiomatic way.

Jersey can be run as a simple handler of sorts in a Netty pipeline. The Netty event loop remains unblocked, there is no locking, reads and writes involve no copying of byte arrays, output is streamed where appropriate and it is intended that there be as few object allocations as possible.

HTTP 1.1 and HTTP/2 are both supported, including upgrades via HTTP’s upgrade header, ALPN or prior knowledge.

Usage

To use, install an instance of JerseyChannelInitializer as the child handler of a Netty ServerBootstrap:

serverBootstrap.childHandler(new JerseyChannelInitializer(baseUri,
    sslContext,
    yourJaxRsApplication));

Background and Motivation

Jersey itself contains a Netty integration project, but it is annotated with @Beta, and the author additionally writes:

Note that this implementation cannot be more experimental.

There are several issues with this “native” Netty integration project. The most problematic seems to be issue 3500. This issue and others stem from the fact that the “native” Netty integration project sets up its own internal queues for streaming, which overflow. Additionally, new ByteBuffers are allocated throughout. It is also not entirely clear if HTTP/2 is fully supported.

This implementation instead shares a ByteBuf for reading and writing, and makes heavy use of Netty’s ChunkedWriteHandler, while also ultimately ensuring that all operations on a given ByteBuf that originate from Jersey are serialized to the Netty event loop. This dramatically reduces object allocations, locks, threading issues, exception handling pathways and other concurrency problems.