Additional Netty convenience classes and handlers
Java
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
README.md
pom.xml

README.md

This library represents a small collection of netty utilities. These are mostly ideas and nothing is tested!

TypedMessageEventHandler

Strongly types message handlers and prevents the user from having to cast of check instanceof on individual messages. This results in cleaner, easier to understand handlers.

First declare a POJO, which implements the TypedMessageListener marker interface, and annotate methods you wish to handle specific messages with the Listen annotation.

public class MyMessageHandler implements TypedMessageListener {

  @Listen(type=Integer.class)
  public void handleInteger(ChannelHandlerContext ctx, TypedMessageEvent<Integer> e) {

  }

  @Listen(type=Float.class)
  public void handleFloat(ChannelHandlerContext ctx, TypedMessageEvent<Float> e) {

  }

}

You then need to insert an instance of your POJO as an argument to the TypedMessageEventHandler in your pipeline. It's important to recognize that this handler will not parse messages for you, you must first handle framing and decoding.

pipeline.addLast("frameDecoder", new MyFrameDecoder());
pipeline.addLast("messageDecoder", new MyMessageDecoder());
pipeline.addLast("messageHandler", new TypedMessageEventHandler(new MyMessageHandler()));

My specific use case is one where I have a protocol made-up of many protobuf messages. In this case I use a length based frame decoder, a custom protobuf message decoder then handle indidual messages with a custom handler.

ChannelOutputStream

Adapts Channel to an OutputStream

final ChannelOutputStream channelOutputStream = new ChannelOutputStream(channel);