microBean™ Jersey Netty Integration


License

License

Categories

Categories

Net Netty Networking Jersey Program Interface REST Frameworks
GroupId

GroupId

org.microbean
ArtifactId

ArtifactId

microbean-jersey-netty
Last Version

Last Version

0.25.0
Release Date

Release Date

Type

Type

jar
Description

Description

microBean™ Jersey Netty Integration
microBean™ Jersey Netty Integration
Project URL

Project URL

https://microbean.github.io/microbean-jersey-netty
Project Organization

Project Organization

microBean™
Source Code Management

Source Code Management

https://github.com/microbean/microbean-jersey-netty/

Download microbean-jersey-netty

How to add to project

<!-- https://jarcasting.com/artifacts/org.microbean/microbean-jersey-netty/ -->
<dependency>
    <groupId>org.microbean</groupId>
    <artifactId>microbean-jersey-netty</artifactId>
    <version>0.25.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.microbean/microbean-jersey-netty/
implementation 'org.microbean:microbean-jersey-netty:0.25.0'
// https://jarcasting.com/artifacts/org.microbean/microbean-jersey-netty/
implementation ("org.microbean:microbean-jersey-netty:0.25.0")
'org.microbean:microbean-jersey-netty:jar:0.25.0'
<dependency org="org.microbean" name="microbean-jersey-netty" rev="0.25.0">
  <artifact name="microbean-jersey-netty" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.microbean', module='microbean-jersey-netty', version='0.25.0')
)
libraryDependencies += "org.microbean" % "microbean-jersey-netty" % "0.25.0"
[org.microbean/microbean-jersey-netty "0.25.0"]

Dependencies

compile (5)

Group / Artifact Type Version
io.netty : netty-codec-http jar
io.netty : netty-codec-http2 jar
io.netty : netty-handler jar
io.netty : netty-transport jar
org.glassfish.jersey.core : jersey-server jar

provided (1)

Group / Artifact Type Version
org.microbean : microbean-base-specification pom 0.6.0

test (4)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-api jar 5.6.0
org.junit.jupiter : junit-jupiter-engine jar 5.6.0
org.glassfish.jersey.inject : jersey-hk2 jar
org.glassfish.jersey.core : jersey-client jar

Project Modules

There are no modules declared in this project.

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.

Status

This project uses semantic versioning. It is still in an alpha state.

Installation

Add a dependency on this project in your Netty-based Maven project:

<dependency>
  <groupId>org.microbean</groupId>
  <artifactId>microbean-jersey-netty</artifactId>
  <version>0.25.0</version>
</dependency>

Usage

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

serverBootstrap.childHandler(new JerseyChannelInitializer(baseUri, // e.g. URI.create("/")
    sslContext, // an SslContext, or null if you don't want TLS support
    true, // yes, HTTP/2 support please
    20971520L, // 20MB maximum incoming payload (arbitrary)
    new DefaultEventExecutorGroup(8), // a DefaultEventExecutorGroup with 8 threads (arbitrary) to run your application
    true, // yes, use Jersey's native dependency injection facilities
    new ApplicationHandler(yourJaxRsApplication), // the ApplicationHandler wrapping your application
    8192, // write in 8K chunks (arbitrary)
    Unpooled::new /* how to create those chunks */));

Background and Motivation

While Jersey itself contains a Netty integration project, it is annotated with @Beta, and the author additionally writes that his "implementation cannot be more experimental". In addition, there are several issues with the Jersey-supplied Netty integration project. The most problematic seems to be issue 3500. This issue and others stem from the fact that the Jersey-supplied Netty integration project sets up its own internal queues for streaming, which overflow. Additionally, new ByteBuffers and InputStreams are allocated throughout. It is also not entirely clear if HTTP/2 is fully supported.

Recently, there have been some efforts to improve this area, but they still do not take advantage of the built in queuing and threading constructs offered up by Netty itself.

Implementation Details

By contrast, microBean™ Jersey Netty Integration approaches the problem of running a Jakarta RESTful Web Services application under Netty by following the spirit of Netty.

Several composable channel pipeline components are provided.

Considering HTTP 1.1 support, the first is a decoder that decodes HttpRequest and HttpContent messages into ContainerRequest objects, which are the objects consumed natively by Jersey.

A ContainerRequest may need an entity stream, and that can be tricky. Since a Jakarta RESTful Web Services application may block whatever thread it is running on for some time (for example, perhaps it is performing synchronous database access over a slow connection), then normally it should be run on its own thread that is not the Netty event loop. But assuming that this is so, the InputStream it will receive by way of a ContainerRequest's entity stream will now be operated on by two threads: the application-hosting thread, and the Netty event loop. microBean™ Jersey Netty Integration ensures that this InputStream implementation is thread-safe, uses as few locks as possible, allocates as little memory as possible, and takes advantage of CompositeByteBuf and other Netty native constructs to ensure this inter-thread messaging is safe and efficient.

Similarly, a ContainerRequest needs a ContainerResponseWriter which is responsible for actually writing the Jakarta RESTful Web Services application's output. microBean™ Jersey Netty Integration provides a ContainerResponseWriter implementation that is also a ChannelInboundHandlerAdapter: it takes ContainerRequests as input, installs itself as their ContainerResponseWriter implementation, and writes the requisite output back to the channel safely and efficiently.

The OutputStream implementation used to do this must, of course, ensure that writes take place on the Netty event loop. microBean™ Jersey Netty Integration ensures that this OutputStream implementation does not needlessly buffer and/or copy byte arrays but instead takes advantage of the built-in outbound event queuing present in the Netty event loop itself.

Similar constructs are provided for HTTP/2 support as well.

org.microbean

microBean™

At the intersection of Java, Kubernetes and the enterprise.

Versions

Version
0.25.0
0.24.2
0.24.1
0.24.0
0.23.0
0.22.0
0.21.0
0.20.0
0.12.0
0.11.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.1
0.8.0
0.7.1
0.7.0
0.5.1
0.5.0
0.4.0
0.3.2
0.3.1
0.3.0