Netty in Action
Norman Maurer and Marvin Allen Wolfthal
Foreword by Trustin Lee
  • December 2015
  • ISBN 9781617291470
  • 296 pages
  • printed in black & white

The first-ever book on Netty ... shows how to build a high-performance, low-latency network application.

From the Foreword by Trustin Lee, Founder of Netty

Introduces the Netty framework and shows you how to incorporate it into your Java network applications. You'll learn to write highly scalable applications without the need to dive into the low-level non-blocking APIs at the core of Java.

Table of Contents detailed table of contents

foreword

preface

acknowledgments

about this book

about the authors

author online

about the cover illustration

Part 1 NETTY CONCEPTS AND ARCHITECTURE

1. Netty—asynchronous and event-driven

1.1. Networking in Java

1.1.1. Java NIO

1.1.2. Selectors

1.2. Introducing Netty

1.2.1. Who uses Netty?

1.2.2. Asynchronous and event-driven

1.3. Netty’s Core Components

1.3.1. Channels

1.3.2. Callbacks

1.3.3. Futures

1.3.4. Events and handlers

1.3.5. Putting it all together

1.4. Summary

2. Your first Netty application

2.1. Setting up the development environment

2.1.1. Obtaining and installing the Java Development Kit

2.1.2. Downloading and installing an IDE

2.1.3. Downloading and installing Apache Maven

2.1.4. Configuring the toolset

2.2. Netty client / server overview

2.3. Writing the Echo server

2.3.1. ChannelHandlers and business logic

2.3.2. Bootstrapping the server

2.4. Writing an Echo client

2.4.1. Implementing the client logic with ChannelHandlers

2.4.2. Bootstrapping the client

2.5. Building and running the Echo Server and Client

2.5.1. Running the build

2.5.2. Running the Echo Server and Client

2.6. Summary

3. Netty components and design

3.1. Channel, EventLoop and ChannelFuture

3.1.1. Interface Channel

3.1.2. Interface EventLoop

3.1.3. Interface ChannelFuture

3.2. ChannelHandler and ChannelPipeline

3.2.1. Interface ChannelHandler

3.2.2. Interface ChannelPipeline

3.2.3. A closer look at ChannelHandlers

3.2.4. Encoders and Decoders

3.2.5. Abstract class SimpleChannelInboundHandler

3.3. Bootstrapping

3.4. Summary

4. Transports

4.1. Case study: transport migration

4.1.1. Using I/O and NIO without Netty

4.1.2. Using I/O and NIO with Netty

4.1.3. Non-blocking Netty version

4.2. Transport API

4.3. Included transports

4.3.1. NIO - Nonblocking I/O

4.3.2. Epoll - Native Non-blocking Transport for Linux

4.3.3. OIO - Old blocking I/O

4.3.4. Local transport for communication within a JVM

4.3.5. Embedded transport

4.4. Transport use cases

4.5. Summary

5. ByteBuf

5.1. The ByteBuf API

5.2. Class ByteBuf - Netty’s data container

5.2.1. How it works

5.2.2. ByteBuf usage patterns

5.3. Byte-level operations

5.3.1. Random access indexing

5.3.2. Sequential access indexing

5.3.3. Discardable bytes

5.3.4. Readable bytes

5.3.5. Writable bytes

5.3.6. Index management

5.3.7. Search operations

5.3.8. Derived buffers

5.3.9. Read/write operations

5.3.10. More operations

5.4. Interface ByteBufHolder

5.5. ByteBuf allocation

5.5.1. On-demand: interface ByteBufAllocator

5.5.2. Unpooled buffers

5.5.3. Class ByteBufUtil

5.6. Reference counting

5.7. Summary

6. ChannelHandler and ChannelPipeline

6.1. The ChannelHandler family

6.1.1. The Channel lifecycle

6.1.2. The ChannelHandler lifecycle

6.1.3. Interface ChannelInboundHandler

6.1.4. Interface ChannelOutboundHandler

6.1.5. ChannelHandler adapters

6.1.6. Resource Management

6.2. Interface ChannelPipeline

6.2.1. Modifying a ChannelPipeline

6.2.2. Firing events

6.3. Interface ChannelHandlerContext

6.3.1. Using ChannelHandlerContext

6.3.2. Advanced usages of ChannelHandler and ChannelHandlerContext

6.4. Exception handling

6.4.1. Handling inbound exceptions

6.4.2. Handling outbound exceptions

6.5. Summary

7. EventLoop and threading Model

7.1. Threading model overview

7.2. The EventLoop

7.2.1. I/O and event handling in Netty 4

7.2.2. I/O operations in Netty 3

7.3. Task scheduling

7.3.1. JDK scheduling API

7.3.2. Scheduling tasks using EventLoop

7.4. Implementation details

7.4.1. Thread management

7.4.2. EventLoop / Thread Allocation

7.5. Summary

8. Bootstrapping

8.1. Bootstrap classes

8.2. Bootstrapping clients and connectionless protocols

8.2.1. Bootstrapping a client

8.2.2. Channel and EventLoopGroup compatibility

8.3. Bootstrapping servers

8.3.1. The ServerBootstrap class

8.3.2. Bootstrapping a server

8.4. Bootstrapping clients from a Channel

8.5. Adding multiple ChannelHandlers during a bootstrap

8.6. Using Netty ChannelOptions and attributes

8.7. Bootstrapping DatagramChannels

8.8. Shutdown

8.9. Summary

9. Unit Testing

9.1. Overview of EmbeddedChannel

9.2. Testing ChannelHandler

9.2.1. Testing inbound messages

9.2.2. Testing outbound messages

9.3. Testing exception handling

9.4. Summary

Part 2 CODECS

10. The codec framework

10.1. What is a codec?

10.2. Decoders

10.2.1. Abstract class ByteToMessageDecoder

10.2.2. Abstract class ReplayingDecoder

10.2.3. Abstract class MessageToMessageDecoder

10.2.4. Class TooLongFrameException

10.3. Encoders

10.3.1. Abstract class MessageToByteEncoder

10.3.2. Abstract class MessageToMessageEncoder

10.4. Abstract Codec classes

10.4.1. Abstract class ByteToMessageCodec

10.4.2. Abstract MessageToMessageCodec

10.4.3. Class CombinedChannelDuplexHandler

10.5. Summary

11. Provided ChannelHandlers and codecs

11.1. Securing Netty applications with SSL/TLS

11.2. Building Netty HTTP/HTTPS applications

11.2.1. HTTP decoder, encoder, and codec

11.2.2. HTTP message aggregation

11.2.3. HTTP compression

11.2.4. Using HTTPS

11.2.5. WebSocket

11.3. Idle Connections and Timeouts

11.4. Decoding delimited and length-based protocols

11.4.1. Delimited protocols

11.4.2. Length-based protocols

11.5. Writing big data

11.6. Serializing data

11.6.1. JDK Serialization

11.6.2. Serialization with JBoss Marshalling

11.6.3. Serialization via Protocol Buffers

11.7. Summary

Part 3 NETWORK PROTOCOLS

12. Web Sockets

12.1. Introducing WebSocket

12.2. Our example WebSockets application

12.3. Adding WebSockets support

12.3.1. Handling HTTP requests

12.3.2. Handling WebSocket frames

12.3.3. Initializing the ChannelPipeline

12.3.4. Bootstrapping

12.4. Testing the application

12.4.1. What about encryption?

12.5. Summary

13. Broadcasting Events with UDP

13.1. UDP basics

13.2. UDP broadcast

13.3. The UDP sample application

13.4. The message POJO: LogEvent

13.5. Writing the broadcaster

13.6. Writing the monitor

13.7. Running the LogEventBroadcaster and LogEventMonitor

13.8. Summary

Part 4 CASE STUDIES

14. Case studies, part 1

14.1. Droplr - building mobile services

14.1.1. How it all started

14.1.2. How Droplr works

14.1.3. Creating a faster upload experience

14.1.4. The technology stack

14.1.5. Performance

14.1.6. Summary - standing on the shoulders of giants

14.2. Firebase - a real-time data synchronization service

14.2.1. The Firebase architecture

14.2.2. Long polling

14.2.3. HTTP 1.1 keep-alive and pipelining

14.2.4. Control of SSL handler

14.2.5. Firebase Summary

14.3. Urban Airship - building mobile services

14.3.1. Basics of mobile messaging

14.3.2. Third-party delivery

14.3.3. Binary protocol example

14.3.4. Direct to device delivery

14.3.5. Netty excels at managing large numbers of concurrent connections

14.3.6. Summary - Beyond the perimiter of the Ffirewall

14.4. Summary

15. Case Studies, Part 2

15.1. Netty at Facebook: Nifty and Swift

15.1.1. What is Thrift?

15.1.2. Improving the state of Java Thrift using Netty

15.1.3. Nifty server design

15.1.4. Nifty asynchronous client design

15.1.5. Swift: A faster way to build Java Thrift service

15.1.6. Results

15.1.7. Facebook Summary

15.2. Netty at Twitter: Finagle

15.2.1. Twitter’s growing pains

15.2.2. The birth of Finagle

15.2.3. How Finagle works

15.3. Finagle’s abstraction

15.3.1. Failure management

15.3.2. Composing services

15.4. The future: Netty

15.4.1. Twitter Summary

15.5. Summary

Appendixes

Appendix A: Introduction to Maven

A.1. What is Maven?

A.1.1. Installing and configuring Maven

A.1.2. Basic Maven concepts

A.2. POM examples

A.2.1. A Project POM

A.2.2. POM inheritance and aggregation

A.3. Maven command-line arguments

A.4. Summary

About the Technology

Netty is a Java-based networking framework that manages complex networking, multithreading, and concurrency for your applications. And Netty hides the boilerplate and low-level code, keeping your business logic separate and easier to reuse. With Netty, you get an easy-to-use API, leaving you free to focus on what’s unique to your application.

About the book

Netty in Action introduces the Netty framework and shows you how to incorporate it into your Java network applications. You will discover how to write highly scalable applications without getting into low-level APIs. The book teaches you to think in an asynchronous way as you work through its many hands-on examples and helps you master the best practices of building large-scale network apps.

What's inside

  • Netty from the ground up
  • Asynchronous, event-driven programming
  • Implementing services using different protocols
  • Covers Netty 4.x

About the reader

This book assumes readers are comfortable with Java and basic network architecture.

About the authors

Norman Maurer is a senior software engineer at Apple and a core developer of Netty. Marvin Wolfthal is a Dell Services consultant who has implemented mission-critical enterprise systems using Netty.

combo $54.99 pBook + eBook
eBook $43.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks

High-performance Java network stacks—covered from concepts to best practices.

Christian Bach, Grid Trading Platform

The most comprehensive content for getting the most out of Netty.

Jürgen Hoffmann, Red Hat

An excellent overview of the Netty framework. Highly recommended to anyone doing performance-sensitive network I/O work in Java.

Yestin Johnson, Impact Radius