Mina is used as the application framework to process some requests in the site. However, if a problem is found, the actual number of requests waiting is higher than the actual number of requests displayed by Mina.
Get the number of requests currently being handle by MINA: Session. getservice (). getmanagedsessioncount ()
This quantity is different from the quantity obtained by netstat-anp | grep-c ": 1234". The latter is much higher, excluding some connections that have been disconnected during the statistical period, it is still high.
Why? How to adjust it?
First:
Differentiate the differences between sessionopened and sessioncreated of iohandleradapter,
Sessioncreated:
Invoked from an I/O processor thread when a new connection has been created. Because this method is supposed
To be called from the same thread that handles I/O of multiple sessions, please implement this method to perform tasks that consumes minimal amount of time such as socket parameter and user-defined session attribute initialization.
This method is called when the connection is established in the I/O processing phase. This thread must process many other session processes at the same time. Therefore, the call cost of this method is high, try not to implement code here
Sessionopened is called in another thread and does not affect the efficiency of the main I/O thread. If there is no absolute need, it is best to use sessionopened instead of sessioncreated for some processing code.
After processing this part, Mina's processing will be smoother.
Second:
Do it now
Inheriting iohandleradapter, You can implement many methods. Apart from the several required methods, such as messagereceived, it is best to implement other methods. When an exception occurs, when the task has been completed and an idle is found, the client is disconnected as soon as possible to avoid thread accumulation.
Third:
The returned topic involves how to reasonably set thread concurrency and thread queue.
During initialization, niosocketacceptor can specify the number of concurrent threads. In some cases, we recommend the number of CPU cores + 1, but in terms of server CPU processing efficiency, this is already low. Of course, you need to set it based on your business needs. I added this value.
Add a thread processing pool for niosocketacceptor: acceptor. getfilterchain (). addlast ("threadpool", new executorfilter (executors. newcachedthreadpool ()));
In addition, backlog, that is, the size of the processing queue. Some people may think that the larger the queue, the better. but in fact, this also needs to consider the needs of the client, the client needs efficiency or quality, the waiting of the client will not cause problems. for my applications, the longer the client waits, the more likely the problem arises. so I reduced this value.
After these changes, Mina is much smoother than before.