- About
- Architecture
- Project Structure
- Prerequisites
- Getting Started
- Usage
- Configuration
- Built With
- License
Netty Client is a lightweight, non-blocking TCP client application that demonstrates how to build high-performance network clients using the Netty framework within a Spring Boot environment.
The client connects to a remote TCP server, sends structured RequestData payloads, and processes ResponseData replies through a custom Netty pipeline — all with automatic connection retry logic for resilience.
- ⚡ Non-blocking I/O — leverages Netty's event-driven architecture
- 🔄 Automatic retry — gracefully waits for the server with increasing delays (up to 10 attempts)
- 🧩 Custom pipeline — dedicated encoder/decoder for protocol handling
- 📦 Spring Boot integration — runs as a managed component with devtools hot-reload
- 🛡️ Graceful shutdown — proper resource cleanup via Netty's
EventLoopGroup
┌─────────────────────────────────────────────┐
│ Netty Client App │
│ │
│ ┌──────────────┐ ┌──────────────────┐ │
│ │ Spring Boot │ │ Netty Core │ │
│ │ (Application) │───▶│ │ │
│ └──────────────┘ │ ┌────────────┐ │ │
│ │ │ Bootstrap │ │ │
│ │ └──────┬─────┘ │ │
│ │ │ │ │
│ │ ┌──────▼──────┐ │ │
│ │ │ Channel │ │ │
│ │ │ Pipeline │ │ │
│ │ │ │ │ │
│ │ │ ┌──────────┐ │ │ │
│ │ │ │RequestData│ │ │ │
│ │ │ │ Decoder │ │ │ │
│ │ │ ├──────────┤ │ │ │
│ │ │ │ResponseData│ │ │ │
│ │ │ │ Encoder │ │ │ │
│ │ │ ├──────────┤ │ │ │
│ │ │ │ Client │ │ │ │
│ │ │ │ Handler │ │ │ │
│ │ │ └──────────┘ │ │ │
│ │ └──────────────┘ │ │
│ └──────────────────┘ │
└─────────────────────┬───────────────────────┘
│
│ TCP (port 5002)
▼
┌──────────────────┐
│ TCP Server │
│ (localhost:5002) │
└──────────────────┘
- Startup — Spring Boot launches
NettyClientviaCommandLineRunner - Connection — attempts to connect to
localhost:5002with retry logic - Handshake — on successful connection,
ClientHandler.channelActive()fires - Send —
RequestData(int + string) is written and flushed through the pipeline - Encode/Decode — custom
ResponseDataEncoderandRequestDataDecodertransform bytes ↔ objects - Response — server reply is printed to console, connection closes
src/
├── main/
│ └── java/com/batsandrey/demo/
│ ├── DemoApplication.java # Spring Boot entry point
│ ├── NettyClient.java # Netty client with retry logic
│ ├── decoder/
│ │ └── RequestDataDecoder.java # Inbound byte → RequestData
│ ├── encoder/
│ │ └── ResponseDataEncoder.java # Outbound ResponseData → byte
│ ├── entity/
│ │ ├── request/
│ │ │ └── RequestData.java # Request model (int + string)
│ │ └── response/
│ │ └── ResponseData.java # Response model (int value)
│ └── handler/
│ └── ClientHandler.java # Channel event handler
└── test/
└── java/com/batsandrey/demo/
└── DemoApplicationTests.java # Spring context test
| Tool | Version |
|---|---|
| ☕ Java | 11+ |
| 🔨 Maven | 3.6+ |
git clone https://github.com/andriibats/client-netty.git
cd client-netty./mvnw clean packageIf
mvnwis not available, use:mvn clean package
java -jar target/demo-0.0.1-SNAPSHOT.jarOr directly with Maven:
./mvnw spring-boot:runNote: The client will attempt to connect to
localhost:5002. If no server is running, it will retry up to 10 times with increasing delays (2s → 4s → 6s → ...) and log a warning instead of crashing.
The client is designed to work with a TCP server listening on port 5002. Once connected, it automatically:
- Sends a
RequestDataobject:intValue:123stringValue:"all work and no play makes jack a dull boy"
- Waits for a
ResponseDatareply from the server - Prints the response to standard output
- Closes the connection
WARN [main] NettyClient: Connection to localhost:5002 failed (attempt 1/10). Retrying in 2000ms...
WARN [main] NettyClient: Connection to localhost:5002 failed (attempt 2/10). Retrying in 4000ms...
INFO [main] NettyClient: Connected to localhost:5002 on attempt 3
ResponseData(intValue=456)
Key constants are defined in NettyClient.java:
| Parameter | Default | Description |
|---|---|---|
HOST |
localhost |
Target server hostname |
PORT |
5002 |
Target server port |
MAX_RETRIES |
10 |
Maximum connection attempts |
BASE_DELAY_MS |
2000 |
Initial retry delay (× attempt number) |
💡 Tip: For production use, consider externalizing these values to
application.yml.
| Library | Version | Purpose |
|---|---|---|
| Spring Boot | 2.5.2 | Application framework & dependency injection |
| Netty | 4.1.65.Final | Non-blocking network I/O framework |
| Lombok | 1.18.30 | Boilerplate code reduction |
| Maven | — | Build & dependency management |
Contributions are welcome! Please feel free to submit a Pull Request.
- 🍴 Fork the project
- 🌿 Create your feature branch (
git checkout -b feature/amazing-feature) - 💾 Commit your changes (
git commit -m 'Add some amazing feature') - 📤 Push to the branch (
git push origin feature/amazing-feature) - 🔀 Open a Pull Request
This project is licensed under the MIT License — see the LICENSE file for details.
