Event based NiO multithreaded server — reprint

JDK1. NiO of 4 effectively solves the problem of thread overhead existing in the original streaming io. The main purpose of using multithreading in NiO is not to allocate independent service threads in response to each client request, but to improve the service ability by making full use of the processing capacity and waiting time of multiple CPUs through multithreading. The introduction of multithreading is easy to further reduce readability and maintainability for the already slightly complex NiO code. The introduction of good design model will not only bring high-performance and reliable code, but also bring a pleasant development process.

The selector of NiO adopts multiplexing (multiplexing) technology can process multiple sockets on one selector and perform IO operations by obtaining read-write channels. Due to network bandwidth and other reasons, it is easy to wait in the read and write operations of channels. Therefore, the introduction of multithreading in the read and write operations can significantly improve the performance and improve the perceived quality of service of the client. Therefore, the model in this paper Type will mainly improve the data exchange ability with the client by using the read and write thread pool. As shown in the figure below, after the server accepts the client request, the control thread gives the requested read channel to the read thread pool, and the read thread pool allocates threads to complete the read operation of the client data; When the read thread completes the read operation, it returns the data to the control thread for service processing; After completing the business processing, submit the data and write channel to be responded to the client to the write thread pool, and the write thread completes the operation of sending response data to the client.

At the same time, the process processing of the whole server is based on the event mechanism. In the process of [accept connection > read > business processing > write > close connection], the trigger will trigger corresponding events, and the event processor will respond to the corresponding events respectively to complete the business processing on the server side. Let's take a detailed look at the various components of this model. (1) Onaccept: this event is triggered when the server receives a client connection request. Through this event, we can know that there is a new client incoming call. This event can be used to control the load of the server. For example, the server can set to only provide services for a certain number of clients at the same time. When the number of simultaneous requests exceeds the number, an exception can be thrown directly in response to the event to reject the request Absolutely new connection. (2) Onaccepted: this event is triggered after the client request is accepted by the server. This event indicates that a new client has officially established a connection with the server. (3) Onread: this event is triggered when the data sent by the client has been correctly read by the server control thread. This event informs each event processor that the data sent by the client can be actually processed. It should be noted that in this model, the data reading of the client is completed by the control thread to the reader thread, and the event processor does not need to be in this event For special read operations, you only need to directly process the data transmitted by the control thread. (4) Onwrite: this event is triggered when the client can start receiving data sent by the server. Through this event, we can send response data to the client. In this model, the event processor only needs to set (5) onclosed in this event: this event is triggered when the client is disconnected from the server. (6) Onerror: this event is triggered when an error occurs between the client and the server from the beginning of the connection to the last disconnection. Through this event, we can know what error occurred. In this model, the event is broadcast, that is, all registered event processors can get event notifications. In this way, different businesses can be processed in different ways The processor implementation makes the business function of each processor as single as possible. As shown in the following figure: the whole event model is composed of listener, event adapter, event trigger and event processor. Serverlistener: This is an event interface that defines the server events to listen to. If you need to define more events, you can extend it here. @ h_502_34 @ event adapter: implement an adapter (eventadapter) for the serverlistener interface, The advantage is that the final event handler can only process the events of interest@ H_ 502_ 34 @ event notifier: it is used to notify the registered event processor to respond to the event by triggering the server event at an appropriate time. The trigger is implemented in singleton mode to uniformly control the events on the whole server to avoid confusion. = 0; I -- ((serverlistener) listeners get(i)). onAccept(); } ....// other fire method } @H_ 502_ 34 @ event handler (handler): inherits the event adapter, responds to events of interest and implements business processing. The following is a simple implementation of the event processor, which responds to the onread event and prints the data read from the client at the terminal. @ h_502_34 @ registration of the event processor. In order for the event processor to obtain the event notification of the service thread, the event processor needs to Register in trigger@ H_ 502_ 34 @ NiO multithreading server is mainly composed of master service thread, read thread and write thread. Master service thread (server): the master thread will create read and write thread pools to listen to and accept client requests, and submit read and write channels. The corresponding read thread (Reader) and write service thread (writer) will read the client data and respond to the client respectively. 0) {set selectedkeys = selector. Selectedkeys() ; Iterator it = selectedKeys. iterator(); While (it. Hasnext()) {selectionkey key = (selectionkey) it. Next(); it. Remove(); / / handle IO event if ((key. Readyops() & selectionkey. Op_accept) {/ / accept the new connection serversocketchannel SSC = (serversocketchannel) key. Channel(); notifier. Fireonaccept(); socketchannel SC = SSc. Accept(); sc.configureblocking (false); // Trigger the connection acceptance event request request = new request (SC); notifier. fireOnAccepted(request); // Register the read operation for the next read operation
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>