Home / Class/ ChannelHandlerAdapter Class — netty Architecture

ChannelHandlerAdapter Class — netty Architecture

Architecture documentation for the ChannelHandlerAdapter class in ChannelHandlerAdapter.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  733fd4a8_2b66_5e68_0a41_9ec76f4eeefb["ChannelHandlerAdapter"]
  a82141d3_259a_c18d_093f_4af8b7a4a1c6["ChannelHandlerAdapter.java"]
  733fd4a8_2b66_5e68_0a41_9ec76f4eeefb -->|defined in| a82141d3_259a_c18d_093f_4af8b7a4a1c6
  f27b18fb_4b39_04d7_a9c9_7cb661254c26["ensureNotSharable()"]
  733fd4a8_2b66_5e68_0a41_9ec76f4eeefb -->|method| f27b18fb_4b39_04d7_a9c9_7cb661254c26
  8794f3b5_081e_5ba9_b410_2d73fa5ed955["isSharable()"]
  733fd4a8_2b66_5e68_0a41_9ec76f4eeefb -->|method| 8794f3b5_081e_5ba9_b410_2d73fa5ed955
  6da36e1e_e2f7_62e2_2149_01c2908037a7["handlerAdded()"]
  733fd4a8_2b66_5e68_0a41_9ec76f4eeefb -->|method| 6da36e1e_e2f7_62e2_2149_01c2908037a7
  4d70f241_a8b5_6b41_9136_4e352eea3848["handlerRemoved()"]
  733fd4a8_2b66_5e68_0a41_9ec76f4eeefb -->|method| 4d70f241_a8b5_6b41_9136_4e352eea3848
  0d672431_edf3_4d0d_a882_47c849c6c8f3["exceptionCaught()"]
  733fd4a8_2b66_5e68_0a41_9ec76f4eeefb -->|method| 0d672431_edf3_4d0d_a882_47c849c6c8f3

Relationship Graph

Source Code

transport/src/main/java/io/netty/channel/ChannelHandlerAdapter.java lines 27–94

public abstract class ChannelHandlerAdapter implements ChannelHandler {

    // Not using volatile because it's used only for a sanity check.
    boolean added;

    /**
     * Throws {@link IllegalStateException} if {@link ChannelHandlerAdapter#isSharable()} returns {@code true}
     */
    protected void ensureNotSharable() {
        if (isSharable()) {
            throw new IllegalStateException("ChannelHandler " + getClass().getName() + " is not allowed to be shared");
        }
    }

    /**
     * Return {@code true} if the implementation is {@link Sharable} and so can be added
     * to different {@link ChannelPipeline}s.
     */
    public boolean isSharable() {
        /**
         * Cache the result of {@link Sharable} annotation detection to workaround a condition. We use a
         * {@link ThreadLocal} and {@link WeakHashMap} to eliminate the volatile write/reads. Using different
         * {@link WeakHashMap} instances per {@link Thread} is good enough for us and the number of
         * {@link Thread}s are quite limited anyway.
         *
         * See <a href="https://github.com/netty/netty/issues/2289">#2289</a>.
         */
        Class<?> clazz = getClass();
        Map<Class<?>, Boolean> cache = InternalThreadLocalMap.get().handlerSharableCache();
        Boolean sharable = cache.get(clazz);
        if (sharable == null) {
            sharable = clazz.isAnnotationPresent(Sharable.class);
            cache.put(clazz, sharable);
        }
        return sharable;
    }

    /**
     * Do nothing by default, sub-classes may override this method.
     */
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        // NOOP
    }

    /**
     * Do nothing by default, sub-classes may override this method.
     */
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        // NOOP
    }

    /**
     * Calls {@link ChannelHandlerContext#fireExceptionCaught(Throwable)} to forward
     * to the next {@link ChannelHandler} in the {@link ChannelPipeline}.
     *
     * Sub-classes may override this method to change behavior.
     *
     * @deprecated is part of {@link ChannelInboundHandler}
     */
    @Skip
    @Override
    @Deprecated
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.fireExceptionCaught(cause);
    }
}

Frequently Asked Questions

What is the ChannelHandlerAdapter class?
ChannelHandlerAdapter is a class in the netty codebase, defined in transport/src/main/java/io/netty/channel/ChannelHandlerAdapter.java.
Where is ChannelHandlerAdapter defined?
ChannelHandlerAdapter is defined in transport/src/main/java/io/netty/channel/ChannelHandlerAdapter.java at line 27.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free