ebook img

Netty in Action PDF

305 Pages·2014·3.018 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Netty in Action

MEAP Edition Manning Early Access Program Netty in Action Version 8 Copyright 2014 Manning Publications For more information on this and other Manning titles go to www.manning.com ©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. http://www.manning-sandbox.com/forum.jspa?forumID=857 brief contents PART 1:GETTING STARTED 1 Netty – Asynchronous and Event-Driven by nature 2 Your first Netty application 3 Netty Overview PART 2:CORE FUNCTIONS/PARTS 4 Transports 5 Buffers 6 ChannelHandler and ChannelPipeline 7 The Codec framework 8 Provided ChannelHandlers and Codecs 9 Bootstrapping Netty Applications PART 3:NETTY BY EXAMPLE 10 Unit-test your code 11 WebSockets 12 SPDY 13 Broadcasting events via UDP PART 4:ADVANCED TOPICS 14 Implement a custom codec 15 EventLoop and Thread-Model 16 Case Studies, Part 1: Droplr, Firebase, and Urban Airship 17 Case Studies, Part 2: Facebook and Twitter APPENDIXES: A The Community / How to get involved B Related books C Related projects ©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. http://www.manning-sandbox.com/forum.jspa?forumID=857 1 1 Netty – Asynchronous and Event-Driven 1.1 Introducing Netty .................................................................................................. 5 1.2 Building Blocks .................................................................................................. 8 1.2.1 Channels .......................................................................................................... 8 1.2.2 Callbacks .......................................................................................................... 8 1.2.3 Futures ............................................................................................................ 9 1.2.4 Events and Handlers ........................................................................................ 10 1.2.5 Putting it All Together ...................................................................................... 11 1.3 About this Book ................................................................................................ 12 ©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. http://www.manning-sandbox.com/forum.jspa?forumID=857 2 This chapter covers • What is Netty? • Some History • Introducing Netty • Asynchrony and Events • About this Book WHAT IS NETTY? Netty is a client/server framework that harnesses the power of Java's advanced networking while hiding its complexity behind an easy-to use API. Netty provides performance and scalability, leaving you free to focus on what really interests you - your unique application! In this first chapter we'll explain how Netty provides value by giving some background on the problems of high-concurrency networking. Then we'll introduce the basic concepts and building blocks that make up the Netty toolkit and that we'll be studying in depth in the rest of the book. SOME HISTORY If you had started out in the early days of networking you would have spent a lot of time learning the intricacies of sockets, addressing and so forth, coding on the C socket libraries, and dealing with their quirks on different operating systems. The first versions of Java (1995-2002) introduced just enough object-oriented sugar- coating to hide some of the intricacies, but implementing a complex client-server protocol still required a lot of boilerplate code (and a fair amount of peeking under the hood to get it right). Those early Java APIs (java.net) supported only the so-called "blocking" functions provided by the native socket libraries. An unadorned example of server code using these calls is shown in Listing 1.1. Blocking I/O Example ServerSocket serverSocket = new ServerSocket(portNumber); //1 Socket clientSocket = serverSocket.accept(); //2 BufferedReader in = new BufferedReader( //3 new InputStreamReader(clientSocket.getInputStream())); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); String request, response; while ((request = in.readLine()) != null) { //4 if ((cid:147)Done(cid:148).equals(request) ( //5 break; } response = processRequest(request); //6 out.println(response); //7 } //8 ©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. http://www.manning-sandbox.com/forum.jspa?forumID=857 3 1. A ServerSocket is created to listen for connection requests on a specified port. 2. The accept() call blocks until a connection is established. It returns a new Socket which will be used for the conversation between the client and the server. 3. Streams are created to handle the socket’s input and output data. The BufferedReader reads text from a character-input stream. The PrintWriter prints formatted representations of objects to a text- output stream. 4. The processing loop begins. readLine() blocks until a string terminated by a line-feed or carriage return is read in. 5. If the client has sent “Done” the processing loop is exited. 6. The request is handled a processing method, which returns the server’s response. 7. The response is sent to the client. 8. The processing loop continues. Obviously, this code is limited to handling only one connection at a time. In order to manage multiple, concurrent clients we need to allocate a new Thread for each new client Socket (and there is still plenty of code being written right now that does just that). But consider the implications of using this approach to support a very large number of simultaneous, long-lived connections. At any point in time many threads could be dormant, just waiting for input or output data to appear on the line. This could easily add up to a significant waste of resources, with a corresponding negative impact on performance. However, there is an alternative. In addition to the blocking calls shown in the example, the native socket libraries have long included nonblocking I/O functions as well. These enable us to determine if there are data ready for reading or writing on any among a set of sockets. We can also set flags that will cause read/write calls to return immediately if there are no data; that is, if a blocking call would have blocked1. With this approach, at the cost of somewhat greater code complexity, we can obtain considerably more control over how networking resources are utilized. JAVA NIO In 2002, Java 1.4 introduced support for these nonblocking APIs in the package java.nio (“NIO”). “New” or “Nonblocking?” ”NIO” was originally an acronym for “New Input/Output.” However, the Java API has been around long enough that it is no longer “new.” The acronym is now commonly repurposed to signify “Non- blocking I/O.” On the other hand, some (the author included) refer to blocking I/O as “OIO” or “Old Input/Output.” You may also encounter “Plain I/O.” We've already shown an example of blocking I/O in Java. Figure 1.1 shows how that approach has to be expanded to handle multiple connections: by creating a dedicated thread for each 1 "4.3BSD returned EWOULDBLOCK if an operation on a nonblocking descriptor could not complete without blocking." W. Richard Stevens, Advanced Programming in the UNIX Environment (1992), p. 364. ©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. http://www.manning-sandbox.com/forum.jspa?forumID=857 4 one - including connections that are idle! Clearly, the scalability of this method is going to be constrained by the number of threads you can create in the JVM. Figure 1.1 Blocking I/O This may be acceptable if you know you will have a small number of connections to manage. But if you reach 10,000 or more concurrent connections the overhead of context- switching will begin to be noticeable. Furthermore, each thread has a default stack memory allocation between 128K and 1M. Considering the overall memory and operating system resources required to handle 100,000 or more concurrent connections, this appears to be a less than ideal solution. SELECTORS By contrast, figure 1.2 shows an approach using non-blocking I/O that mostly eliminates these constraints. Here we introduce the "Selector", which constitutes the linchpin of Java's non- blocking I/O implementation. ©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. http://www.manning-sandbox.com/forum.jspa?forumID=857 5 Figure 1.2 Nonblocking I/O The Selector determines which of a set of registered sockets is ready for I/O at any point in time. As we explained earlier, this works because the I/O operations are set to non-blocking mode. Given this notification, a single associated thread can handle multiple concurrent connections. (A Selector is normally handled by one Thread but a specific implementation might employ multiple threads.) Thus, each time a read or write operation is performed it can be checked immediately for completion. Overall, this model provides much better resource usage than the blocking I/O model, since • it can handle many connections with fewer threads, which means far less overhead due to memory and context-switching, and • threads can be retargeted to other tasks when there is no I/O to handle. You could build your applications directly on these Java NIO API constructs, but doing it correctly and safely is far from trivial. Implementing reliable and scalable event-processing to handle and dispatch data as efficiently as possible is a cumbersome and error-prone task best left to a specialist - Netty. 1.1 Introducing Netty Not so long ago the idea that an application could support hundreds of thousands of concurrent clients would have been judged absurd. Today we take it for granted. Indeed, developers know that the bar keeps moving higher and that there will always be demands for greater throughput and availability - to be delivered at lower cost. Let's not underestimate the importance of that last point. We have learned from long and painful experience that the direct use of low-level APIs not only exposes a high level of ©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. http://www.manning-sandbox.com/forum.jspa?forumID=857 6 complexity, but introduces a critical dependency on skill sets that are often in short supply. Hence a fundamental principle of Object Orientation: hide complexity behind abstractions. This principle has borne fruit in the form of frameworks that encapsulate solutions to common programming tasks, many of them typical of distributed systems. Nowadays most professional Java developers are familiar with one or more of these frameworks2 and for many they have become indispensable, enabling them to meet both their technical requirements as well as their schedules. WHO USES NETTY? Netty is one of the most widely-used Java networking frameworks3. Its vibrant and growing user community includes large companies like Facebook and Instagram as well as popular open-source projects such as Infinispan, HornetQ, Vert.x, Apache Cassandra and Elasticsearch, all of which have employed its powerful network abstractions in their core code. In turn, Netty has benefited from interaction with these projects, enhancing both its scope and flexibility through implementations of protocols such as FTP, SMTP, HTTP, WebSocket and SPDY as well as others, both binary and text-based. Firebase and Urban Airship are among the startups using Netty, the former Firebase for long-lived HTTP connections, the latter for all kinds of push notifications. Whenever you use Twitter, you are using Finagle, their Netty-based API for inter-system communication. Facebook uses Netty to do something similar in Nifty, their Apache Thrift service. Both companies see scalability and performance as critical concerns and both are ongoing contributors to Netty. These examples are presented as detailed case studies in Chapters 16 and 17, so if you are interested in real-world usage of Netty you might want to have a look there straight away. In 2011 the Netty project became independent from Red Hat with the goal of facilitating the participation of contributors from the broader developer community. Both Red Hat and Twitter continue to use Netty and remain among its most active contributors. The table below highlights many of the technical and methodological features of Netty that you will learn about and use in this book. Category Netty Features Unified API for multiple transport types—blocking and nonblocking Simple but powerful threading model Design True connectionless datagram socket support Chaining of logics to support reuse Ease of Use Extensive Javadoc and large example set 2 Spring is probably the best known of these and is actually an entire ecosystem of application frameworks addressing dependency injection, batch processing, database programming, etc. 3 Netty was awarded the Duke’s Choice Award in 2011. See https://www.java.net/dukeschoice/2011 ©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. http://www.manning-sandbox.com/forum.jspa?forumID=857 7 No additional dependencies except JDK 1.6+. (Some features are supported only in Java 1.7+. Optional features may have additional dependencies.) Better throughput, lower latency than core Java APIs Performance Less resource consumption thanks to pooling and reuse Minimized memory copying Eliminates OutOfMemoryError due to slow, fast, or overloaded connection. Robustness Eliminates unfair read/write ratio often found in NIO applications in high-speed networks Complete SSL/TLS and StartTLS support Security Runs in a restricted environment such as Applet or OSGI Release early and often Community Community-Driven ASYNCHRONOUS AND EVENT-DRIVEN All network applications need to be engineered for scalability, which may be defined as "the ability of a system, network, or process to handle a growing amount of work in a capable manner or its ability to be enlarged to accommodate that growth"4. As we have said, Netty helps you to accomplish this goal by exploiting nonblocking I/O, often referred to as "asynchronous I/O." We'll be using the word "asynchronous" and its cognates a great deal in this book, so this is a good time to introduce them. Asynchronous, that is, un-synchronized events are certainly familiar to you from everyday life. For example, you send an E-mail message; you may or may not get a response later, or you may receive a message even while you are sending one. Asynchronous events can also have an ordered relationship. For example, you typically don't receive an answer to a question until you have asked it, but you aren't prevented from doing something else in the meantime. In everyday life asynchrony "just happens," so we may not think about it very often. But getting computer programs to work the way we do does poses special problems, which go far beyond questions of network calls however sophisticated they may be. In essence, a system that is both asynchronous and "event-driven" exhibits a particular, and to us, valuable kind of behavior: it can respond to events occurring at any time in any order. This is the kind of system we want to build, and as we shall see, this is the paradigm Netty supports from the ground up. 4 Bondi, André B. (2000). "Characteristics of scalability and their impact on performance". Proceedings of the second international workshop on Software and performance - WOSP '00. p. 195. ©Manning Publications Co. We welcome reader comments about anything in the manuscript - other than typos and other simple mistakes. These will be cleaned up during production of the book by copyeditors and proofreaders. http://www.manning-sandbox.com/forum.jspa?forumID=857

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.