Netty asynchronous model

The article said that the use of Task implement asynchronous processing time-consuming task, this talk about specific asynchronous model
basic introduction:

1. The concept of asynchronous and synchronous relative, when starting an asynchronous procedure call, the caller can not immediately get results. The actual assembly process after completion of the call, the status, notification and callback to notify the caller
IO 2.Netty operation is asynchronous, including Bind, Write, Connect and other operations will simply return a ChannelFuture
3. caller and not immediately get the results, but by Future-Listener mechanism, users can easily take the initiative to acquire or obtain IO operating results through the mechanism of
the asynchronous model 4.Netty is built on the future and the callback, callback is the callback. The end of said Future, its core idea is: Suppose a fun method, the calculation process can be very time consuming, fun wait to return is clearly inappropriate. You can call when the fun, immediately returns a Future, can subsequently Future fun to monitor the processing method (i.e., Future-Listener Mechanism)

Future Description

1. Expresses its asynchronous execution results can be detected by the method of execution is complete it offers, such as retrieving calculations, etc.
2.ChannelFuture is an interface, we can add the listener, when listening event will inform listeners

Chaining schematic:

Here Insert Picture Description
1. When using the program netty, interception and converts inbound and outbound data is only required to callback or future use, which makes chaining simple, efficient, and to facilitate the preparation of reusable common code
2.netty the goal of the framework is to make your business logic from application code base network, the freed

Future-Listener mechanism

1. When the Future object just created, in the non-completion status, the caller can obtain the status of the operations performed by ChannelFuture return, register a listener function to perform the operation after the completion of
2. The following operations are common
to determine the current operating method by isDone completion
by isSuccess way to tell if completed the current operation is successful
to get the reason completed the current operation failed by getCause methods
judged by isCancelled method has completed the current operation was canceled
to register by addListener method listener, when the operation completed (isDone method returns completed), will inform the designated listener; if Future object has been completed, the notification listener of the specified
specific code implementation:
Here Insert Picture Description

to sum up:

Compared to traditional blocking IO, IO operation after execution threads will be blocked to live, know the operation is complete; the benefits of asynchronous processing is not cause thread to block threads can execute other programs during the IO operation, it will be more stable under high concurrency scenarios and higher throughput.

Lack of study time, too shallow knowledge, that's wrong, please forgive me.

There are 10 kinds of people in the world, one is to understand binary, one is do not understand binary.

Published 71 original articles · won praise 54 · views 420 000 +

Guess you like

Origin blog.csdn.net/weixin_43326401/article/details/104228911