Apache Mina thread model configuration

Source: Internet
Author: User
Http://chinaliwee.blog.163.com/blog/static/34118206200871081847280/Apache Mina thread model configuration

This article describes how to configure the thread model of a Mina-based application.

1. Disable the default threadmodel settings.

Mina2.0 and later versions have no threadmodel. If you use these versions, you can skip this section.

Threadmodel settings are introduced after mina1.0. However, using threadmodel increases the configuration complexity. We recommend that you disable the default theadmodel configuration.

Ioacceptor acceptor = ...;
Ioserviceconfig acceptorconfig = acceptor. getdefaconfig config ();
Acceptorconfig. setthreadmodel (threadmodel. manual );

Note that in the relevant Guide, it is assumed that you have disabled the default configuration of threadmodel as described in this section.

2. Configure the number of I/O worker threads

This section is only related to NiO implementation. Nio data packets and Virtual Machine pipelines do not have this configuration.

In the NIO Implementation of Mina, there are three I/O working threads:

> The acceptor thread accepts incoming connections and transfers them to the I/O processor thread for read/write operations.
> Each socketacceptor generates an acceptor thread. The number of threads cannot be configured.

> The connector thread tries to connect to a remote peer and transfers the successful connection to the I/O processor thread for read/write operations.
> Each socketconneproduces A ctor thread, and the number cannot be configured.

> The I/O processor thread executes the actual read/write operations until the connection is closed.
> Each socketacceptor or socketconnegenerates its own I/O processing thread. This number can be configured. The default value is 1.

Therefore, for each ioservice, you can configure the number of I/O processing threads. The following code generates a socketacceptor with four I/O processing threads.

Ioacceptor acceptor = new socketacceptor (4, executors. newcachedthreadpool ());

There is no way to determine the number of I/O processing threads based on experience. It can be increased from 1.

Ioacceptor acceptor = new socketacceptor (runtime. getruntime (). availableprocessors () + 1,

Executors. newcachedthreadpool ());

3. Add an executorfilter to iofilterchain.

Executorfilter is an iofilter used to convert the incoming I/O events to a java. util. Concurrent. executor implementation. The event will be transferred from the executor to the next iofilter, usually a thread pool. You can add any number of executorfilters anywhere in iofilterchain to implement any type of thread model, from simple thread pool to complex Seda.

So far, we have not added executorfilter. If executorfilter is not added, the event will be transferred to an iohandler through method calls, this means that the business logic in iohandler implementation will run in the I/O processing thread. We call this thread model "single thread model ". The single-thread model can be used to reduce the response of network applications and business logic (such as game servers) restricted by CPU ).

A typical network application requires an executorfilter to be inserted into the iofilterchain, because the business logic and the I/O processing thread have different resource usage modes. If you use iohandler to perform database operations without adding an executorfilter, your entire server will be locked when performing database operations, especially when the database performance is low. The following example configures an ioservice to add an executorfilter when a new iosession is created.

Ioacceptor acceptor = ...;
Defaultiofilterchainbuilder filterchainbuilder = acceptor. getdefaconfig config (). getfilterchain ();
Filterchainbuilder. addlast ("threadpool", new executorfilter (executors. newcachedthreadpool ());

It should be noted that executorfilter does not manage the lifecycle of a specific executor. When it is completed, it is necessary to close all worker threads of a specific executor.

Executorservice executor = ...;
Ioacceptor acceptor = ...;
Defaultiofilterchainbuilder filterchainbuilder = acceptor. getdefaconfig config (). getfilterchain ();
Filterchainbuilder. addlast ("threadpool", new executorfilter (Executor );
// Start the server.
Acceptor. BIND (...);
// Shut down the server.
Acceptor. Unbind (...);
Executor. Shutdown ();

Using an executorfilter usually does not mean that a thread pool is used and there are no restrictions on executor implementation.

4. Where should I put executorfilter in iofilterchain?

This depends on the specific application. If an application has a protocolcodecfilter implementation and a commonly used iohandler implementation with database operations, we recommend that you add an executorfilter after the protocolcodecfilter implementation, this is because the performance characteristics of most protocol decoding implementations are limited by the CPU, which is the same as that of the I/O processing thread.

Ioacceptor acceptor = ...;
Defaultiofilterchainbuilder filterchainbuilder = acceptor. getdefaconfig config (). getfilterchain ();
// Add CPU-bound job first,
Filterchainbuilder. addlast ("codec", new protocolcodecfactory (...));
// And then a thread pool.
Filterchainbuilder. addlast ("threadpool", new executorfilter (executors. newcachedthreadpool ());

5 be careful when selecting the ioservice thread pool type

Executors. newcachedthreadpool () is often the preferred choice for ioservice. If other types are used, they may have unpredictable performance impact on ioservice. Once all threads in the pool are in use, ioservice will start to lock when trying to request a thread from the pool, and then there will be a strange performance drop, which is sometimes difficult to track.

6 ioservices and executorfilters are not recommended to share a thread pool.

You can share ioservices and executorfilters with each other in a thread pool, instead of a single thread pool. This is not forbidden, but there will be many problems. In this case, unless you create a buffer thread pool for ioservices.

References:

Apache Mina indexing ing thread model http://mina.apache.org/configuring-thread-model.html

Added by Mark Webb, Last edited by trustin Lee On Apr 16,200 7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.