Netty 4.x User Guide Demos

repository·master·Indexed 23 days ago

https://github.com/waylau/netty-4-user-guide-demos

A collection of practical code demonstrations for Netty 4.x, serving as the implementation reference for the 'Netty 4.x User Guide' and the book 'Netty 原理解析与开发实战'. The repository includes examples for Java Standard I/O, NIO, AIO, and Netty 4.1.52.Final, covering features such as HTTP/2, WebSocket, SSL/TLS mutual authentication, custom codecs, and heartbeat mechanisms.

Tokens
1.2K
Snippets
1
Records
6
Agent score
84%

What's inside netty-4-user-guide-demos

  1. Understand the custom message protocol format

    master

    The project uses a custom binary protocol consisting of a fixed-size header and a variable-length body. The header is 10 bytes long and provides metadata required to parse the subsequent message body.

    Header Structure

    FieldNameByte OffsetRangeDescription
    Magicmagic00x80, 0x81Frame header identifier
    MsgTypemsgType10x00-0xffMessage type identifier
    Reservereserve2-30x00Reserved for future extensions
    SNsn4-50-32767Sequence number (transaction ID). Increments by 1 starting from 0. When it reaches 32767, it wraps back to 0. Response messages must copy the sn from the request message.
    Lenlen6-90-2147483647Length of the message body

    Body Structure

    FieldNameByte OffsetRangeDescription
    BodybodyVariable0-2GBThe actual message content. The format depends on the msgType. The size should not exceed 2GB.
  2. Understand the custom message format

    master

    The demos in this repository use a specific binary message format consisting of a fixed-size header followed by a variable-length body. This format is designed to handle message framing in Netty by specifying the length of the payload upfront.

    ComponentNameByte OffsetRangeDescription
    HeadermsgType00x00 - 0xffIdentifies the type of message
    Headerlen1 - 40 - 2147483647Length of the message body (4 bytes)
    BodybodyVariable0 - nThe actual message payload
  3. List of Netty and Java I/O Demos

    master

    The repository contains various implementations for learning network programming and Netty. Key categories include:

    I/O Models

    • Java Standard I/O: Echo server and client (com.waylau.java.demo.net)
    • Java NIO: Echo server and client (com.waylau.java.demo.nio)
    • Java AIO: Echo server and client (com.waylau.java.demo.aio)
    • Netty: Echo server and client (com.waylau.netty.demo.echo)

    Netty Specific Features

    • Servers: Discard server (com.waylau.netty.demo.discard), Time server (com.waylau.netty.demo.time), and Connectionless protocol Echo server (com.waylau.netty.demo.echo)
    • Buffers: Java ByteBuffer usage (com.waylau.java.demo.buffer) and Netty ByteBuf usage/modes (com.waylau.netty.demo.buffer)
    • Codecs: Custom decoders (com.waylau.java.demo.decoder), custom encoders (com.waylau.java.demo.encoder), custom codecs (com.waylau.java.demo.codec), and object serialization (com.waylau.java.demo.codec.serialization)
    • Protocols: HTTP Web server (com.waylau.java.demo.httpserver), HTTP/2 Web server/client (com.waylau.java.demo.http2), and WebSocket Chat room (com.waylau.java.demo.websocketchat)
    • Security: SSL/TLS mutual authentication Echo server/client (com.waylau.java.demo.secureecho)
    • Mechanisms: Heartbeat mechanism (com.waylau.java.demo.heartbeat) and JSON serialization via Jackson (com.waylau.java.demo.codec.jackcon)
  4. Initialize a ThreadPoolExecutor with a LinkedBlockingQueue

    master

    To create a custom thread pool in Java, use the ThreadPoolExecutor constructor. This allows you to control the core pool size, maximum pool size, keep-alive time, and the work queue used to hold tasks before they are executed. In this example, a LinkedBlockingQueue with a capacity of 10 is used to manage the task backlog.

    Key parameters used in the example:

    • corePoolSize: 2
    • maximumPoolSize: 2
    • keepAliveTime: 4000
    • unit: TimeUnit.MILLISECONDS
    • workQueue: LinkedBlockingQueue<Runnable>(10)
    // 队列
    BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(10);
    
    // 初始化线程池执行器
    ExecutorService pool = new ThreadPoolExecutor(2, 2, 4000, TimeUnit.MILLISECONDS, workQueue);
    
    for (int i = 0; i < 3; i++) {
        // 提交任务
        pool.submit(new MyTask());
    }