An idea for an annotation-based handler

Trustin Lee (이희승) trustin at gmail.com
Sun Nov 8 16:34:33 EST 2009


Here's my rough idea about adopting annotation for handler
implementation.  Let me show you some example handler class that
should be straightforward enough:

@Stateful
// Note that there's no need to implement an interface.
public class MyHandler {

    // This replaces LifeCycleAwareChannelHandler
    @LifeCycleHandler(LifeCyclePhase.PREADD)
    void init(ChannelHandlerContext ctx);

    // These replace messageReceived event handler methods.
    @MessageEventHandler
    void handleRequest(MessageEvent<MyMessage> evt);
    @MessageEventHandler
    void handleRequest(HttpRequest request);
    @MessageEventHandler
    void handleChunk(HttpChunk chunk);

    // Alternatively, you can ask for more parameter injection.
    @MessageEventHandler
    void handleRequest(Channel channel, HttpRequest request);
    @MessageEventHandler
    void handleChunk(ChannelHandlerContext ctx, HttpChunk chunk);

    // These replace exceptionCaught handler methods.
    // More specific handler (SSLException in this case) has precedence.
    @ExceptionEventHandler
    void handleException(SSLException e);
    @ExceptionEventHandler
    void handleException(Throwable t);
}

Please let me know what you think.

— Trustin Lee, http://gleamynode.net/



More information about the netty-dev mailing list