In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "the case study of NIO non-blocking communication in Netty". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the case study of NIO non-blocking Communication in Netty.
1. Map
The following figure shows the relationship between several non-blocking attributes of NIO: before several different customers (Socket) enter the restaurant (system), they pass through the restaurant gate (ServerSocketChannel). At this time, there is a guiding waiter (SelectionKey) at the door. When they see the customer (Socket) coming in, they will arrange a seat for you (register registers to the monitor), and then the ordering waiter receives the registered relationship of the customer (Socket). According to the customer you have just arranged to be seated, you will be served on the menu.
1.2 SelectionKey
Represents the registration relationship between SelectableChannel and Selector. An event (selection key) is selected each time the channel is registered with the selector. The selection key contains two sets of operations represented as integer values. Each bit of the action set represents the type of selectable operation supported by the channel of the key.
1.3 register
When register (Selector sel, int ops) is called to register the channel to the selector, the selector's listening event to the channel needs to be specified by the second parameter ops.
1.4 Monitoring Typ
The types of events that can be listened for (which can be represented by the four constants of SelectionKey):
Read: SelectionKey.OP_READ (1)
Write: SelectionKey.OP_WRITE (4)
Connection: SelectionKey.OP_CONNECT (8)
Receive: SelectionKey.OP_ACCEPT (16)
If you listen for more than one event when registering, you can use the bit OR operator to connect
/ / register listening event int intersetSet = SelectionKey.OP_READ | SelectionKey.OP_WRITE
The use of SelectionKey
Method
Description
Int interestOps ()
Get a collection of events of interest
Int readyOps ()
Gets a collection of operations that the channel is ready for
SelectableChannel channel ()
Get registration channel
Selector selector ()
Return to selector
Boolean isReadable ()
Detect whether the read event in Channal is ready
Boolean isWritable ()
Detect whether write events are ready in Channal
Boolean isConnectable ()
Check whether the connection is ready in Channel
Boolean isAcceptable ()
Detect whether the reception is ready in Channel
Selector method
Method
Description
Set keys ()
All SelectionKey collections. Represents the Channel registered on the Selector
SelectedKeys ()
The selected SelectionKey collection. Returns the selected key set for this Selector
Int select ()
Monitor all registered Channel, and when there are IO operations that need to be processed among them, the method returns and adds the corresponding SelectionKey to the selected SelectionKey collection, and the method returns the number of these Channel.
Int select (long timeout)
You can set the select () operation for the timeout period
Int selectNow ()
Perform an immediately returned select () operation, which does not block the thread
Selector wakeup ()
Causes a select () method that has not yet been returned to return immediately
Void close ()
Close the selector
2. Tcp network non-blocking communication / / client @ Testpublic void client () throws IOException {/ / 1. Get channel SocketChannel sChannel = SocketChannel.open (new InetSocketAddress ("127.0.0.1", 8085)); / / 2. Switch non-blocking mode sChannel.configureBlocking (false); / / 3. Allocates a buffer of the specified size ByteBuffer buf = ByteBuffer.allocate (1024); / / 4. Send data to the server String str = "mujiutian"; while (! StringUtils.isEmpty (str)) {buf.put ((new Date (). ToString () + "\ n" + str). GetBytes ()); buf.flip (); sChannel.write (buf); buf.clear (); str = "";} / / 5. Close the channel sChannel.close ();} / / server @ Testpublic void server () throws IOException {/ / 1. Get the channel ServerSocketChannel ssChannel = ServerSocketChannel.open (); / / 2. Switch non-blocking mode ssChannel.configureBlocking (false); / / 3. Bind connection ssChannel.bind (new InetSocketAddress (8085)); / / 4. Get selector Selector selector = Selector.open (); / / 5. Register the channel with the selector and specify "listen to receive events" ssChannel.register (selector, SelectionKey.OP_ACCEPT); / / 6. The "ready" event while (selector.select () > 0) {/ / 7 on the polling get selector. Get all the "Select Keys (ready listening events)" registered in the current selector Iterator it = selector.selectedKeys (). Iterator (); while (it.hasNext ()) {/ / 8. Getting ready is the event SelectionKey sk = it.next (); / / 9. Determine which event is ready if (sk.isAcceptable ()) {/ / 10. If "receive ready", get the client connection SocketChannel sChannel = ssChannel.accept (); / / 11. Switch non-blocking mode sChannel.configureBlocking (false); / / 12. Register the channel with the selector sChannel.register (selector, SelectionKey.OP_READ);} else if (sk.isReadable ()) {/ / 13. Gets the channel SocketChannel sChannel = (SocketChannel) sk.channel () of the read ready state on the current selector; / / 14. Read data ByteBuffer buf = ByteBuffer.allocate (1024); int len = 0; while ((len = sChannel.read (buf)) > 0) {buf.flip (); System.out.println (new String (buf.array (), 0, len)); buf.clear () }} / / 15. Deselect key SelectionKey it.remove ();}
3. Udp network non-blocking communication @ Testpublic void send () throws IOException {DatagramChannel dc = DatagramChannel.open (); dc.configureBlocking (false); ByteBuffer buf = ByteBuffer.allocate (1024); Scanner scan = new Scanner (System.in); while (scan.hasNext ()) {String str = scan.next (); buf.put ((new Date (). ToString () + ":\ n" + str). GetBytes (); buf.flip () Dc.send (buf, new InetSocketAddress ("127.0.0.1", 9898); buf.clear ();} dc.close ();} @ Testpublic void receive () throws IOException {DatagramChannel dc = DatagramChannel.open (); dc.configureBlocking (false); dc.bind (new InetSocketAddress (9898)); Selector selector = Selector.open (); dc.register (selector, SelectionKey.OP_READ) While (selector.select () > 0) {Iterator it = selector.selectedKeys (). Iterator (); while (it.hasNext ()) {SelectionKey sk = it.next (); if (sk.isReadable ()) {ByteBuffer buf = ByteBuffer.allocate (1024); dc.receive (buf); buf.flip () System.out.println (new String (buf.array (), 0, buf.limit ()); buf.clear ();}} it.remove ();}} so far, I believe you all have a deeper understanding of "case Analysis of NIO non-blocking Communications in Netty". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.