Home / Class/ UptimeClientHandler Class — netty Architecture

UptimeClientHandler Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  3cb02bd3_0cdb_62f3_e0aa_6e45512b7da7["UptimeClientHandler"]
  92eb5435_6027_ab8e_e36d_99c28cfd379a["UptimeClientHandler.java"]
  3cb02bd3_0cdb_62f3_e0aa_6e45512b7da7 -->|defined in| 92eb5435_6027_ab8e_e36d_99c28cfd379a
  82a1e31c_ca3b_a84f_d2c2_7ca5ea23caf0["channelActive()"]
  3cb02bd3_0cdb_62f3_e0aa_6e45512b7da7 -->|method| 82a1e31c_ca3b_a84f_d2c2_7ca5ea23caf0
  b41b0f11_c5e9_0962_81e2_bb7cba541b9b["channelRead0()"]
  3cb02bd3_0cdb_62f3_e0aa_6e45512b7da7 -->|method| b41b0f11_c5e9_0962_81e2_bb7cba541b9b
  f070360d_dde6_37f3_20a2_7506e338aaac["userEventTriggered()"]
  3cb02bd3_0cdb_62f3_e0aa_6e45512b7da7 -->|method| f070360d_dde6_37f3_20a2_7506e338aaac
  14f2beaa_5815_3cee_d8ce_04295a924aa6["channelInactive()"]
  3cb02bd3_0cdb_62f3_e0aa_6e45512b7da7 -->|method| 14f2beaa_5815_3cee_d8ce_04295a924aa6
  aad1918f_1777_96b7_d624_06f375056a64["channelUnregistered()"]
  3cb02bd3_0cdb_62f3_e0aa_6e45512b7da7 -->|method| aad1918f_1777_96b7_d624_06f375056a64
  e6962585_5363_a486_0299_4546d6a03573["exceptionCaught()"]
  3cb02bd3_0cdb_62f3_e0aa_6e45512b7da7 -->|method| e6962585_5363_a486_0299_4546d6a03573
  0945add4_0748_7af9_50cb_380edf8c4e56["println()"]
  3cb02bd3_0cdb_62f3_e0aa_6e45512b7da7 -->|method| 0945add4_0748_7af9_50cb_380edf8c4e56

Relationship Graph

Source Code

example/src/main/java/io/netty/example/uptime/UptimeClientHandler.java lines 30–93

@Sharable
public class UptimeClientHandler extends SimpleChannelInboundHandler<Object> {

    long startTime = -1;

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        if (startTime < 0) {
            startTime = System.currentTimeMillis();
        }
        println("Connected to: " + ctx.channel().remoteAddress());
    }

    @Override
    public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        // Discard received data
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
        if (!(evt instanceof IdleStateEvent)) {
            return;
        }

        IdleStateEvent e = (IdleStateEvent) evt;
        if (e.state() == IdleState.READER_IDLE) {
            // The connection was OK but there was no traffic for last period.
            println("Disconnecting due to no inbound traffic");
            ctx.close();
        }
    }

    @Override
    public void channelInactive(final ChannelHandlerContext ctx) {
        println("Disconnected from: " + ctx.channel().remoteAddress());
    }

    @Override
    public void channelUnregistered(final ChannelHandlerContext ctx) throws Exception {
        println("Sleeping for: " + UptimeClient.RECONNECT_DELAY + 's');

        ctx.channel().eventLoop().schedule(new Runnable() {
            @Override
            public void run() {
                println("Reconnecting to: " + UptimeClient.HOST + ':' + UptimeClient.PORT);
                UptimeClient.connect();
            }
        }, UptimeClient.RECONNECT_DELAY, TimeUnit.SECONDS);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

    void println(String msg) {
        if (startTime < 0) {
            System.err.format("[SERVER IS DOWN] %s%n", msg);
        } else {
            System.err.format("[UPTIME: %5ds] %s%n", (System.currentTimeMillis() - startTime) / 1000, msg);
        }
    }
}

Frequently Asked Questions

What is the UptimeClientHandler class?
UptimeClientHandler is a class in the netty codebase, defined in example/src/main/java/io/netty/example/uptime/UptimeClientHandler.java.
Where is UptimeClientHandler defined?
UptimeClientHandler is defined in example/src/main/java/io/netty/example/uptime/UptimeClientHandler.java at line 30.

Analyze Your Own Codebase

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

Try Supermodel Free