1. 首页
  2. 网络技术
  3. 网络基础
  4. Netty in Action第五版英文.pdf

Netty in Action第五版英文.pdf

上传者: 2020-09-19 10:43:54上传 PDF文件 3.33MB 热度 11次
brief contents PART 1: GETTING STARTED 1. Netty and Java NIO APIs 2. Your first Netty application 3. Netty from the ground的 PART 2: CORE FUNCTIONS/PARTS 4. Transports 5. Buffers 6. Channelhandler 7. Codec 8. Provided channelhandlers and codecs 9. Bootstrapping Netty applications part 3: NETTY BY EXAMPLE 10. Unit-test your code 11.IV sockets 12. SPDY 13. Broadcasting events via UDP PART 4: ADVANCED TOPICS 14. Implement a custom codec 15. Choosing the right thread model 16. Deregister/re-register with EventLoop 17. Case studies Appendix A: The community -boy to get involved Appendix B: Related books Appendix C: Related pre oManning 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/forumjspa?forumid=857 Netty and Java NO APls This chapter covers Netty architecture Why we need non-blocking IO(NIO) Blocking versus non-blocking io Known problems with the JDK's Nio implementation and Netty s solutions This chapter introduces Netty but it's focus is Javas non-blocking Io(NIO) API. If you're new to networking on the JVM, this chapter is an ideal place to begin but it's also a good refresher for the seasoned Java developer. If you're familiar with Nio and NIo. 2, feel free to skip ahead to chapter 2, which dives into Netty after you get it running on your machine Netty is a Nio client-server framework which enables quick and easy development of network applications, such as protocol servers and clients. Netty offers you a new way to develop your network applications, which makes it easy and scalable. It achieves this by abstracting away the complexity involved and by providing an easy-to-use API that decouples business-logic from the network-handling code. Because it's built for Nio the entire Netty APi is asynchronous Generally network applications have scalability issues, whether they're based on Netty or other NIO APIs. a key component of Netty is its asynchronous nature and this chapter discusses synchronous (blocking) and asynchronous(non-blocking)Io to illustrate why and how asynchronous code solves scalability problems For those new to networking this chapter gives you an understanding of networking applications, in general, and how Netty helps implement them. It explains how to use fundamental Java networking APIs discusses both its strengths and weaknesses and shows how Netty addresses Java issues, such as the Epoll bug or memory leaks oManning 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=85 2 By the end of the chapter, you'll understand what Netty is and what it offers and you' ll gain enough understanding about Javas NIo and asynchronous processing to enable you to work through the other chapters of this book. 1. 1 Why Netty In the words of David Wheeler, all problems in computer science can be solved by another level of indirection as an nio client-server framework netty offers one such level of indirection. Netty simplifies network programming of tCP or UDP servers but you can still access and use the low-level APis because Netty provides high-level abstractions 1.1.1 Not all networking frameworks are alike With Netty ,"quick and easy"doesnt mean that a resulting application suffers from maintaina bility or performance issues. the experiences earned from the implementation of protocols such as Ftp, Smtp, Http, Websocket, spDy and various binary and text-based legacy protocols led Netty's founders to take great care in its design as a result Netty successfully delivers ease of development performance stability and flexibility without compromIse High-profile companies and open-source projects including redHat Twitter, Infinispan and HornetQ, Vertx, Finagle, Akka, Apache Cassandra, Elasticsearch and others use and contribute to Netty. It's also fair to say that some of the features of Netty are a result of the needs of these projects Over the years Netty has become more widely known and is one of the most-used networking frameworks for the JVM, as evident by its use in several popular open- and closed-source projects. In fact, Netty was awarded the duke's Choice Award- in 2011 Also in 2011, Netty founder Trustin Lee left RedHat to join Twitter. At this point the Netty project became independent of any company in an effort to simplify contributions to it. Both Red hat and Twitter use Netty so it should come as no surprise that these two companies are the biggest contributors to Netty at the time of writing. Adoption of Netty continues to increases, as does the number of individual contributors. The community of Netty users is active and the project remains vibrant 1.1.2 Netty boasts a rich feature set As you work through the chapters of this book, you'll learn about and use many of Netty's Features. Figure 1.1 highlights some of the supported transports and protocols to give you a brief overview of the Netty architecture Add citation 2 Add citation oManning 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=85 Transport Services Protocol Support Socket t Http E SSL· StartTls Google Datagram WebSocket Protobuf zlib/gzi Large file Http Tunnel RISP Compression Transfer Pip Legacy Text Binary Protocols ith Unit Testability Extensible Event Model Universat Communication APl Zero-Copy- Capable Rich Byte Buffer Figure 1.1 Overview of the model, transports, and protocols In addition to its wide array of transports and protocols, Netty offers benefits in various development areas(see table 1.1) Table 1. 1 Netty gives developers a full set of tools Development area Netty Features Design Unified API for various transport types-blocking and non- blocking socket Flexible to use Simple but powerful thread- model True connectionless datagram socket support Chaining of logics to make reuse easy Ease of us Well-documented Javadoc and plenty of examples provided No additional dependencies except JDK 1.6 (or above). Some features are supported only in Java 1.7 and above. Other features may have other dependencies, but these are optional Performance Better throughput; lower latency than core Java APls Less resource consumption because of pooling and reuse Minimized unnecessary memory copy Robustness No more OutofMemoryError due to fast, slow, or overloaded connection No more unfair read/write ratio often found in a NIo application in high-speed oManning 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=85 networks ecurity Complete SSL/TLS and StartTLS support Runs in a restricted environment such as applet or OSGI Community Release early, release often Active In addition to the features listed, Netty also ships workarounds for known bugs/limitations in Java's Nio, so you dont need to hassle with them With an overview of Netty's features under your belt it's time to take a closer look a asynchronous processing and the idea behind it. NIo and Netty make heavy use of asynchronous code, and without understanding the impact of the choices associated with this, it's hard to get the best out of it. In the next section we'll take a look at why we need asynchronous APIs 1.2 Asynchronous by design The entire Netty API is asynchronous Asynchronous processing isnt new; the idea has been around for a while. These days, however, Io is often a bottleneck and asynchronous processing is becoming more important every day. but how does it work and what are the different patterns available? Asynchronous processing encourages you to use your resources more efficiently by allowing you to start a task and get notified when it's done rather than waiting for it to complete. You're free to do something else while the task is running asynchronous APi and discusses the differences between the technique This section explains the two most common ways to work with or implement an 1.2.1 Callbacks Callbacks are a technique often used with asynchronous processing. A callback is passed to the method and executes after the method completes. You may recognize this pattern from JavaScript, in which callbacks are at the heart of the language. the following listing shows how to use this technique to fetch data Listing 1.1 Callback example public interface Fetcher I void fetchData(FetchCallback callback)i b terface FetchCallback void onData(data data) void onError (Throwable cause) public class Worker I public void dowork()i oManning 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=85 Fetcher fetcher fetcher. fetchData(new FetchCallback()( @Override public void onData(datadata) System. out. println("Data re ed data) @Override public void onError (Throwable cause) i #2 System. err println("An error accour: " cause getMessage())i #1 Call if data is fetched without error #2 Call if error is received during fetch The Fetcher. fetchData() method takes one argument of type Fetchcallback, which is called when either the data is fetched or an error occurs For each of these situations, it provides one method FetchCallback onData()-Called if the data was fetched without an error(#1) FetchCallback onError()-Called if an error was received during the fetch operation(#2) You can therefore move the execution of these methods from the caller thread to some other thread There is no guarantee whenever one of the methods of the Fetchcallback will be called One problem with the callback approach is that it can lead to spaghetti code when you chain many asynchronous method calls with different callbacks. Some people tend to think this approach results in hard-to-read code but i think it's more a matter of taste and style. For example, Node. js, which is based on Java Script is increasingly popular. It makes heavy use of callbacks, yet many people find that it 's easy to read and write applications with 12.2 Futures A second technique is the use of Futures. A Future is an abstraction, which represents a value that may become available at some point. A Future object either holds the result of a computation or, in the case of a failed computation, an exception Java ships with a Future interface in the java util. concurrent package, which uses it by its Executor for asynchronous processing For example, as shown in the next listing whenever you pass a Runnable object to the ExecutorService. submit()method, you get a Future back, which you can use to check if the execution completed Listing 1.2 Future example via Executor Service ExecutorService executor Executors. newCachedThreadPool()i Runnable task new Runnable oManning 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=85 @override pu Work( as @override public Integer call()I eturn doSomeHeavyWorkWithResul()i uture futurel executor submit(taskl)i Future future2 executor submit(task2 hile (! futurel. isDone( 2. isDone())( hing else #A You can also use this technique in your own API. For example, you could implement a Fetcher (as in listing 1.1) that uses a Future. As shown in the following listing Listing 1.3 Future usage for Fetcher public interface Fetcher Future fetchData( blic class worker pu id dowork( Fetcher fetcher Future future fetcher fetchData(i try while(! fetcher. isDone())i do something else System. out. printin(" Data received: future get() I catch (Throwable cause)( System. err println(An error accour:"+ cause getMessage())i #A oManning 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=85 Again, you check if the fetcher is done yet and if not do some other work. Sometimes using futures can feel ugly because you need to check the state of the future in intervals to see if it is completed yet, whereas with a callback you're notified directly after it's don After seeing both common techniques for asynchronous execution you may wonder which is the best. There is no clear answer here. Netty in fact, uses a mixture of the two to provide you with the best of both worlds The next section provides an introduction to writing networking applications on the JVM first using blocking then using the Nio and Nio. 2 APIs. These foundations are essential for getting the most out of subsequent chapters of this book. If you're familiar with the Java networking APIs, then perhaps only a quick scan of the next section is required to refresh your memory 1.3 Blocking versus non-blocking 10 on the JVM The continued growth of the web has increased the need for networking applications that are capable of handling its scale. Efficiency has become important in meeting these demands. Fortunately Java comes with the tools needed to create efficient, scalable networking applications. Although early versions of Java included networking support, it was Java 1. 4 that introduced the NIo API and paved the way for us to write more efficient networking applications The new API (NIO. 2), introduced in Java 7, was designed to allow us to write asynchronous networking code but tries to provide a more high-level aPi than does its predecessor. To do networking-related tasks in Java, you can take one of two approaches use io, also known as blocking IO use Nio, also known as new/non-blocking Io New or non-blocking? The N in Nio is ty pically thought to mean "non-blocking"rather than new. "Nio has been around for so long now that nobody calls it"new"IO anymore. Most people refer to it as"non- blocking"IO- Figure 1.2 shows how the blocking Io uses one dedicated thread to handle one connection which means that you have a 1: 1 relationship between connections and threads and are, therefore, limited by the number of threads you can create in the JVM oManning 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=85
用户评论