If the logic of event processing can be completed quickly and no new IO requests are initiated, such as just recording an identifier in memory, processing directly on the IO thread is faster because thread pool scheduling is reduced.
However, if the event processing logic is slow, or new IO requests need to be initiated, such as querying the database, it must be dispatched to the thread pool, otherwise the IO thread will be blocked and other requests will not be received.
If an IO thread is used to process an event, and a new IO request is initiated during the event processing, for example, a login request is initiated in a connection event, an exception of "may cause a deadlock" will be reported, but it will not be deadlocked.
Therefore, it is necessary to deal with different scenarios through a combination of different dispatch strategies and different thread pool configurations:
<dubbo:protocol name="dubbo" dispatcher="all" threadpool="fixed" threads="100" />
Dispatcher
all
All messages are dispatched to the thread pool, including requests, responses, connect events, disconnect events, heartbeats, etc.- Corresponding class: com.alibaba.dubbo.remoting.transport.dispatcher.all.AllChannelHandler
direct
All messages are not dispatched to the thread pool, and all are executed directly on the IO thread.- Corresponding class: com.alibaba.dubbo.remoting.transport.dispatcher.direct.DirectDispatcher
message
Only the request response message is dispatched to the thread pool, other connection disconnection events, heartbeat and other messages are directly executed on the IO thread.- Corresponding class: com.alibaba.dubbo.remoting.transport.dispatcher.message.MessageOnlyDispatcher
execution
Only the request message is dispatched to the thread pool, without the response, response and other connection disconnection events, heartbeat and other messages, which are executed directly on the IO thread.- Corresponding class: com.alibaba.dubbo.remoting.transport.dispatcher.execution.ExecutionDispatcher
connection
On the IO thread, the connection disconnection events are put into the queue, executed one by one in an orderly manner, and other messages are dispatched to the thread pool.- Corresponding class: com.alibaba.dubbo.remoting.transport.dispatcher.connection.ConnectionOrderedDispatcher
ThreadPool
fixed
Fixed-size thread pool. Threads are created at startup, not closed, and held all the time. (default)cached
The cached thread pool is automatically deleted after one minute of idle time, and rebuilt when needed.limited
Scalable thread pool, but the number of threads in the pool will only grow and not shrink. The purpose of only growing and not shrinking is to avoid performance problems caused by sudden large traffic when shrinking.eager
Create aWorker
thread pool first. When the number of tasks is greater thancorePoolSize
but less thanmaximumPoolSize
that, priority is createdWorker
to process tasks. When the number of tasks is greater thanmaximumPoolSize
that, put the task into the blocking queue. Thrown when the blocking queue is fullRejectedExecutionException
. (Compared tocached
: throwing an exception directly whencached
the number of tasks exceedsmaximumPoolSize
instead of putting tasks into a blocking queue)