Netty 4.x User Guide Demos
repository·master·Indexed 23 days ago
https://github.com/waylau/netty-4-user-guide-demosA 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.
What's inside netty-4-user-guide-demos
- This repository contains source code implementations for the examples used in the Netty 4.x User Guide and the book Netty 原理解析与开发实战. It provides practical demonstrations of Netty 4.x features, ranging from basic Echo servers to advanced protocols like HTTP/2 and WebSocket.
Understand the custom message protocol format
masterThe 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
Field Name Byte Offset Range Description Magic magic0 0x80,0x81Frame header identifier MsgType msgType1 0x00-0xffMessage type identifier Reserve reserve2-3 0x00Reserved for future extensions SN sn4-5 0-32767Sequence number (transaction ID). Increments by 1 starting from 0. When it reaches 32767, it wraps back to 0. Response messages must copy the snfrom the request message.Len len6-9 0-2147483647Length of the message body Body Structure
Field Name Byte Offset Range Description Body bodyVariable 0-2GBThe actual message content. The format depends on the msgType. The size should not exceed 2GB.Understand the custom message format
masterThe 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.
Component Name Byte Offset Range Description Header msgType0 0x00-0xffIdentifies the type of message Header len1 - 4 0-2147483647Length of the message body (4 bytes) Body bodyVariable 0-nThe actual message payload List of Netty and Java I/O Demos
masterThe 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
ByteBufferusage (com.waylau.java.demo.buffer) and NettyByteBufusage/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)
- Java Standard I/O: Echo server and client (
Technology Stack and Versions
masterThe demos in this repository are built using the following technology stack and versions:
- Netty: 4.1.52.Final
- Jackson: 2.10.1
- JUnit: 5.5.2
Initialize a ThreadPoolExecutor with a LinkedBlockingQueue
masterTo create a custom thread pool in Java, use the
ThreadPoolExecutorconstructor. 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, aLinkedBlockingQueuewith a capacity of 10 is used to manage the task backlog.Key parameters used in the example:
corePoolSize: 2maximumPoolSize: 2keepAliveTime: 4000unit:TimeUnit.MILLISECONDSworkQueue: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()); }