Container Thread Naming
TaskExecutor
用于调用消费者和侦听器。您可以通过设置容器的 ContainerProperties
的 consumerExecutor
属性来提供自定义执行器。使用池化执行器时,请确保有足够线程来处理其中使用它们的全部容器中的并发。在使用 ConcurrentMessageListenerContainer
时,来自执行器的某个线程用于每个消费者(concurrency
)。
A TaskExecutor
is used to invoke the consumer and the listener.
You can provide a custom executor by setting the consumerExecutor
property of the container’s ContainerProperties
.
When using pooled executors, be sure that enough threads are available to handle the concurrency across all the containers in which they are used.
When using the ConcurrentMessageListenerContainer
, a thread from the executor is used for each consumer (concurrency
).
如果您未提供消费者执行器,系统会为每个容器使用 SimpleAsyncTaskExecutor
。此执行器创建具有类似于 <beanName>-C-<n>
的名称的线程。对于 ConcurrentMessageListenerContainer
,线程名称中的 <beanName>
部分将变为 <beanName>-m
,其中 m
表示消费者实例。在首次启动容器后,n
会在每次启动容器时递增。因此,在首次启动容器后,名为 container
的 bean 中的线程将被命名为 container-0-C-1
、container-1-C-1
等;在停止然后后续启动后,它们将被命名为 container-0-C-2
、container-1-C-2
等。
If you do not provide a consumer executor, a SimpleAsyncTaskExecutor
is used for each container.
This executor creates threads with names similar to <beanName>-C-<n>
.
For the ConcurrentMessageListenerContainer
, the <beanName>
part of the thread name becomes <beanName>-m
, where m
represents the consumer instance.
n
increments each time the container is started.
So, with a bean name of container
, threads in this container will be named container-0-C-1
, container-1-C-1
etc., after the container is started the first time; container-0-C-2
, container-1-C-2
etc., after a stop and subsequent start.
从版本 3.0.1
开始,无论使用哪个执行器,您现在都可以更改线程的名称。将 AbstractMessageListenerContainer.changeConsumerThreadName
属性设置为 true
,系统会调用 AbstractMessageListenerContainer.threadNameSupplier
以获取线程名称。这是一个 Function<MessageListenerContainer, String>
,其默认实现返回 container.getListenerId()
。
Starting with version 3.0.1
, you can now change the name of the thread, regardless of which executor is used.
Set the AbstractMessageListenerContainer.changeConsumerThreadName
property to true
and the AbstractMessageListenerContainer.threadNameSupplier
will be invoked to obtain the thread name.
This is a Function<MessageListenerContainer, String>
, with the default implementation returning container.getListenerId()
.