In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to parse Mina code, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
First of all, what is Mina? What can you do for us? That's the first question I ask when I'm working on a new technology. My personal understanding is that Mina is a framework that focuses on the communication layer and can be used by any application that requires underlying communication. For a more detailed and accurate introduction, please refer to the official website http://mina.apache.org/
Advantages of Mina:
1. Implemented in NIO without too many threads. The amount of concurrency that can be handled is greater.
two。 Programming is more convenient for the application layer.
All right, cut the crap and take a look at a Demo first.
Public static void main (String [] args) throws IOException {/ / create Acceptor IoAcceptor acceptor = new NioSocketAcceptor (); / / register filter acceptor.getFilterChain () .addLast ("logger", new LoggingFilter ()); acceptor.getFilterChain () .addLast ("codec", new ProtocolCodecFilter (Charset.forName ("UTF-8"); / / register your business processing class acceptor.setHandler (new TimeServerHandler ()) / / configuration parameter acceptor.getSessionConfig () .setReadBufferSize (2048); acceptor.getSessionConfig () .setIdleTime (IdleStatus.BOTH_IDLE, 10); / / Let Acceptor listen for acceptor.bind (new InetSocketAddress (PORT)) at the bound address;}
IoHandler
Public interface IoHandler {void sessionCreated (IoSession session) throws Exception; void sessionOpened (IoSession session) throws Exception; void sessionClosed (IoSession session) throws Exception; void sessionIdle (IoSession session, IdleStatus status) throws Exception; void exceptionCaught (IoSession session, Throwable cause) throws Exception; void messageReceived (IoSession session, Object message) throws Exception; void messageSent (IoSession session, Object message) throws Exception;}
First of all, new an Acceptor, and you can see that Acceptor is the server object we want to operate on. Then, register two filter with Acceptor. The concept of filter is similar to filter in a web server. Filter lies between your business-side code and the specific code for sending data, and it is responsible for filtering or processing the transmitted information. Business processing code, written in Handler.
The structure of the code is quite clear. The business codes we need to fill in are Handler and Filter. The two of them are very similar, but they are different in concept. In fact, filter mainly deals with the underlying communication byte stream, communication protocols, etc., and generally has nothing to do with business logic. Handler is specifically exposed to application developers and is used to fill in business processing code.
Just take a look at the picture below.
The interface that Mina exposes to developers is mainly IoAcceptor (server side) or IoConnector (client side). How does Mina intercept and handle connections? Let's first introduce the methodological philosophy or terminology of Mina.
IoSession
IoSession: IoSession is an abstraction of customer connections, and the entire communication framework, so to speak, revolves around IoSession.
IoAcceptor: initializes the server with it
IoProcessor: responsible for handling IoSession, including creation, removal, read and write events
IoFilter: filtering messages, or message processing (communication layer)
IoHandler: message processing (of the application logic layer)
The core class of the communication layer is actually IoAcceptor,IoProcessor,IoSession. Here we only analyze the IoAcceptor pattern, that is, the server-side code. The code on the client side is similar, and you can study it yourself.
First, when the IoAcceptor.bind () method is called, the server has been initialized, and the server starts listening for connection requests from the client on the specified port.
When IoAcceptor detects a client request, it passes the request to IoProcessor for processing. Then IoAcceptor goes back to listening. Obviously, the workflow of IoAcceptor is to listen-- > deliver requests-- > continue listening.
The task of IoProcessor is to handle the IO events of these connections. Obviously it is impossible for IoProcessor to run in the same thread as IoAcceptor. The two of them have different division of labor and can be executed concurrently. Mina actually maintains a pool of IoProcessor objects internally, and the default size of the object pool is + 1. 0 CPU. In other words, if your machine is 4-core, there may be five IoProcessor (which can be understood as threads) working at the same time.
Take a look at the following diagram. A picture is worth a thousand words:
Here we mainly analyze IoAcceptor and IoProcessor.
IoAcceptor is relatively simple, its core code is in AbstractPollingAccetor, and the polling code is in the run method in its inner class Accetpor. Take a look at the UML diagram below:
IoProcessor is more complex, its main code is in AbstractPollingProcessor, and the polling code is implemented by the run method in its inner class Processor. It is worth noting that the IoProcessor reference held by IoAcceptor is actually a pool of objects. The object pool is responsible for scheduling a Processor to process the request. The following is the UML class diagram
The above is all the contents of the article "how to parse Mina code". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.