Netty Event monitoring and processing (UP)

Source: Internet
Author: User
Tags event listener readable

Through the introduction, you will learn:

    • Event monitoring, NIO, threading model and other related concepts;
    • Netty general structure;
    • event monitoring and processing;
    • Project Practice Summary;

This chapter first introduces the next two sections, the next two sections.

This article will finally explain the benefits of the extraction rules, we actively participate in >_<

Related concepts

Netty is a NIO framework that changes the setup, readable, writable, and other state of IO channels, abstracts them into events, transmits them in a chain of responsibility, and can insert custom handler on the processing chain to listen and process the events of interest.

So, the basic concepts such as event monitoring, responsibility chain model, Socket interface and IO model, threading model are introduced, which can help to understand the event monitoring and processing of Netty later.

Event Monitoring

The JDK listener pattern mainly contains the following elements:

    • EventObject Event Object
    • EventListener Event Listener Interface
    • Custom Event sources
    • Event triggering

The pattern is simple, the user can customize the event source, save the relevant data of the trigger object, and after the event is triggered, it is passed to the processor who registered the event. The event listener interface is intended to unify the processor method.

For a better understanding of the button click event, where ActionListener is the event listener, ActionEvent is the event object that contains the event source:

Implement a set of event listener specific procedures:

    • Identify event sources;
    • Identify possible events that are defined as different event objects or event methods;
    • Provides a storage structure for storing objects that listen to events, and notifies listeners when an event occurs;
    • Execute callback method, conduct business processing;

Responsibility chain Model

The main thing is to say that the event handler is organized in a way that, through the chain of responsibility mode, can be added in any processing node, the custom processor is very convenient.

With regard to the concept of the responsibility chain, here is a simple one: to allow multiple objects to have the opportunity to process the request, thus avoiding the coupling between the sender and the recipient of the request, linking the object to a chain, and passing the request along the chain until an object has processed him.

Socket

In general, NIO, mainly for network IO, read data from the NIC, write data to the network card, this is the listener mode data source.

Network programming mainly through the socket interface provided by the operating system, through the understanding of the socket interface can be summed up what events.

The socket is a unified interface between the user process and the kernel network protocol.
Socket is also a special kind of file, network communication can be regarded as the reading of the file, making the control of the network and the control of the file is just as convenient.

NiO and IO Models

NiO refers to non-blocking IO, we generally say that Io is blocking IO, want to fully understand these concepts, and will say a lot, here is a brief summary.

The memory of a process includes the user state and the kernel state, in order to secure, the user code is not directly operating the kernel memory, through the system call to interact, such as reading network data, the interactive process is as follows:

After the user thread initiates the read request, it waits for the data to arrive to return, during which time the user thread cannot do anything, and if it is network programming, there may be many socket objects listening, creating a large number of threads being blocked, wasting resources and degrading performance.

In response to this scenario, the concept of the IO model has emerged in several ways:

    • Synchronous non-blocking IO;
    • IO multiplexing;
    • Asynchronous IO;

Specific introduction, the Internet has a lot of information, not detailed, here only to mention the IO multiplexing, say my understanding, our project is to use this way.

The so-called multiplexing, mainly the operating system provides us with this development mode: can be interested in the IO event (build, readable, writable, etc.) early registration, and multiple socket objects can be registered to a selector selector, This allows multiple socket objects to be monitored using a single user thread, and when the event occurs, the corresponding socket is searched for read and write operations.

Before making a friend of NiO development, you can look at the following example to review the entire process:

Threading model

It says my understanding of multiplexing, mentions that a thread listens to multiple sockets, but if there are many sockets, a thread cannot handle it. In addition, the receiving and judging of events and the reading, processing and writing of data can be performed on different threads.

This leads to the concept of threading model, such as reactor and Proactor model, the details are not introduced, the Internet has a lot of information, the ultimate goal is to improve the performance of IO event processing.

Netty Overall structure

This part is mainly to understand the next netty, the implementation of the principle of the first do not delve into.

Overview

Netty is an asynchronous event-driven network application framework that enables fast development of maintainable, high-performance protocol-oriented servers and clients;
It harnesses the power of the Java Advanced API and hides it behind an easy-to-use API;

    • Core, a common abstraction of the underlying network communication, is key.
    • Transport Services (transport services), the definition of specific network transmission capabilities, and some implementations.
    • Protocol Support (protocol supported), Netty for some common protocol encoding and decoding implementation.
0 Copy

A generalized 0 copy refers to the process of computer operation in which the CPU does not need to consume resources for the copying of data between memory. The Sendfile () in Linux and the Filechannel.transferto () method in Java NIO implement 0 copies of the functionality, and in Netty, they are also wrapped in fileregion. NiO The Filechannel.transferto () method achieves 0 copies.

Netty refers to 0 copies, completely in the user state, more inclined to optimize the data operation. Netty allows us to combine multiple pieces of data into a whole segment of virtual data for use by users without the need to copy the data.

A Unified communications model

Traditional Java IO APIs require different types and methods when dealing with different transport protocols, such as: Java.net.Socket and Java.net.DatagramSocket, which do not have the same superclass; Java new IO API is incompatible with the original blocking IO API;

Netty provides a unified API programming interface that abstracts all point-to-point communication operations and adjusts only a few lines of code to switch between different transport implementations:

    • TCP/IP transmission based on NIO
    • TCP/IP transmission based on OIO
    • UDP/IP Transmission based on OIO
    • Local transport
Event model

That is to say the event monitoring and processing, provides a good way to deal with various events.

The approximate process is described in the next article.

Welcome to scan the QR code below, follow my personal public number ~

Netty Event monitoring and processing (UP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.