About this Book

Netty is a Java framework for the rapid development of high-performance network applications. It encapsulates the complexities of network programming and makes the most recent advances in networking and web technologies accessible to a broader range of developers than ever before.

Netty is more than a collection of interfaces and classes; it also defines an architectural model and a rich set of design patterns. But until now, the lack of a comprehensive and systematic user’s guide has been an obstacle to getting started with Netty, a situation Netty in Action aims to remedy. Beyond explaining the details of the framework components and APIs, this book will show how Netty can help you to write more efficient, reusable, and maintainable code.

Who should read this book?

This book assumes you are comfortable with intermediate Java topics such as generics and multithreading. Experience with advanced network programming is not required, but familiarity with the basic Java networking APIs will prove very helpful.

Netty uses Apache Maven as its build management tool. If you have not used Maven, the appendix will provide the information you need to run the book’s sample code. You’ll also be able to reuse the sample Maven configurations as starting points for your own Netty-based projects.

Roadmap

Netty in Action has four parts and an appendix.

Part 1: Netty concepts and architecture

Part 1 is a detailed guide to the framework, covering its design, components, and programming interfaces.

Chapter 1 begins with a brief overview of the blocking and non-blocking network APIs and the corresponding JDK interfaces. We introduce Netty as a toolkit for building highly scalable, asynchronous and event-driven networking applications. We take a first look at the basic building blocks of the framework: channels, callbacks, futures, events, and handlers.

Chapter 2 explains how to configure your system for building and running with the book’s sample code. We test it out with a simple application, a server that echoes the messages it receives from connected clients. We introduce bootstrapping—assembling and configuring all the components of an application at runtime.

Chapter 3 begins with a discussion of the technical and architectural aspects of Netty. The core components of the framework are introduced: Channel, EventLoop, ChannelHandler, and ChannelPipeline. The chapter concludes with an explanation of the differences between bootstrapping servers and clients.

Chapter 4 discusses network transports and contrasts the use of blocking and non-blocking transports with the JDK APIs and with Netty. We study the interface hierarchy underlying Netty’s transport API and the transport types they support.

Chapter 5 is devoted to the framework’s data handling API—ByteBuf, Netty’s byte container. We describe its advantages over the JDK’s ByteBuffer, and the ways in which the memory used by a ByteBuf can be allocated and accessed. We show how to manage memory resources using reference counting.

Chapter 6 focuses on the core components ChannelHandler and ChannelPipeline, which are responsible for dispatching application processing logic and moving data and events through the network layer. Additional topics include the role of ChannelHandlerContext in implementing advanced use cases and the sharing of ChannelHandlers among multiple ChannelPipelines. The chapter concludes with an illustration of handling exceptions triggered by inbound and outbound events.

Chapter 7 provides a general overview of threading models and covers Netty’s threading model in detail. We examine interface EventLoop, which is the principal component of Netty’s concurrency API, and explain its relationship with threads and Channels. This information is essential for understanding how Netty implements asynchronous, event-driven networking. We show how to perform task scheduling using EventLoop.

Chapter 8 explores bootstrapping in depth, starting with the Bootstrap class hierarchy. We revisit the basic use cases as well as some special ones, such as bootstrapping a client connection within a server application, bootstrapping datagram channels, and adding multiple channels during the bootstrapping phase. The chapter concludes with a discussion of how to shut down an application gracefully and release all resources in an orderly fashion.

Chapter 9 is about unit testing ChannelHandlers, for which Netty provides a special Channel implementation, EmbeddedChannel. The examples show how to use this class with JUnit to test both inbound and outbound handler implementations.

Part 2: Codecs

Data conversion is one of the most common operations in network programming. Part 2 describes the rich set of tools Netty provides to simplify this task.

Chapter 10 begins by explaining decoders and encoders, which transform sequences of bytes from one format to another. A ubiquitous example is converting an unstructured byte stream to and from a protocol-specific layout. A codec, then, is a component that combines both an encoder and a decoder in order to handle conversions in both directions. We provide several examples to show how easy it is to create custom decoders and encoders with Netty’s codec framework classes.

Chapter 11 examines the codecs and ChannelHandlers Netty provides for a variety of use cases. These classes include ready-to-use codecs for protocols such as SSL/TLS, HTTP/HTTPS, WebSocket, and SPDY, and decoders that can be extended to handle almost any delimited, variable length or fixed-length protocol. The chapter concludes with a look at framework components for writing large volumes of data and for serialization.

Part 3: Network protocols

Part 3 elaborates on several network protocols that have been touched on briefly earlier in the book. We’ll see once again how Netty makes it easy to adopt complex APIs in your applications without having to be concerned with their internal complexities.

Chapter 12 shows how to use the WebSocket protocol to implement bidirectional communications between a web server and client. The example application is a chat room server that allows all connected users to communicate with one another in real time.

Chapter 13 illustrates Netty’s support for connectionless protocols with a server and client that utilize the broadcast capabilities of the User Datagram Protocol (UDP). As in the previous examples, we employ a set of protocol-specific support classes: DatagramPacket and NioDatagramChannel.

Part 4: Case studies

Part 4 presents five case studies submitted by well-known companies that have used Netty to implement mission-critical systems. These examples illustrate not only real-world usages of the framework components we have discussed throughout the book, but also the application of Netty’s design and architectural principles to building highly scalable and extensible applications.

Chapter 14 has case studies submitted by Droplr, Firebase, and Urban Airship.

Chapter 15 has case studies submitted by Facebook and Twitter.

Appendix: Introduction to Maven

The primary goal of the appendix is to provide a basic introduction to Apache Maven so that you can compile and run the book’s sample code listings and extend them to create your own projects as you begin to work with Netty.

The following topics are presented:

  • The primary goals and uses of Maven
  • Installing and configuring Maven
  • Basic Maven concepts: the POM file, artifacts, coordinates, dependencies, plugins, and repositories
  • Example Maven configurations, POM inheritance, and aggregation
  • Maven’s command-line syntax

Code conventions and downloads

This book provides copious examples that show how you can make use of each of the topics covered. Source code in listings or in text appears in a fixed-width font like this to separate it from ordinary text. In addition, class and method names, object properties, and other code-related terms and content in text are presented using fixed-width font.

Occasionally, code is italicized, as in reference.dump(). In this case reference should not be entered literally but replaced with the content that is required.

The book’s source code is available from the publisher’s website at www.manning.com/books/netty-in-action and at GitHub: https://github.com/normanmaurer/netty-in-action. It is structured as a multimodule Maven project, with a top-level POM and modules corresponding to the book’s chapters.

About the authors

Norman Maurer is one of the core developers of Netty, a member of the Apache Software Foundation, and a contributor to many OpenSource Projects over the past years. He’s a Senior Software Engineer for Apple, where he works on Netty and other network-related projects as part of the iCloud Team.

Marvin Wolfthal has been active in many areas of software development as a developer, architect, lecturer, and author. He has been working with Java since its earliest days and assisted Sun Microsystems in developing its first programs dedicated to promoting distributed object technologies. As part of these efforts he wrote the first cross-language programming courses using C++, Java, and CORBA for Sun Education. Since then his primary focus has been middleware design and development, primarily for the financial services industry. Currently a consultant with Dell Services, he is engaged in extending methodologies that have emerged from the Java world to other areas of enterprise computing; for example, applying the practices of Continuous Integration to database development. Marvin is also a pianist and composer whose work is published by Universal Edition, Vienna. He and his wife Katherine live in Weston, MA, with their three feline companions Fritz, Willy, and Robbie.

Author Online

Purchase of Netty in Action includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the authors and from other users. To access the forum and subscribe to it, point your web browser to www.manning.com/books/netty-in-action. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum. It also provides links to the source code for the examples in the book, errata, and other downloads.

Manning’s commitment to our readers is to provide a venue where a meaningful dialog between individual readers and between readers and the authors can take place. It is not a commitment to any specific amount of participation on the part of the authors, whose contribution to the AO remains voluntary (and unpaid). We suggest you try asking the authors some challenging questions lest their interest stray!

The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset