GH-1433: Fix DMLC Monitor Thread Name

Resolves https://github.com/spring-projects/spring-amqp/issues/1433

Used `beanName` instead of `listenerId` (which falls back to `beanName` if `null`).
Containers for annotations are not beans per se.

**cherry-pick to 2.4.x, 2.3.x**
This commit is contained in:
Gary Russell
2022-03-15 13:52:06 -04:00
committed by Artem Bilan
parent d5e7c3fb79
commit 0ccfcbe283
3 changed files with 25 additions and 2 deletions

View File

@@ -156,7 +156,7 @@ public abstract class AbstractMessageListenerContainer extends RabbitAccessor
private TransactionAttribute transactionAttribute = new DefaultTransactionAttribute();
@Nullable
private String beanName;
private String beanName = "not.a.Spring.bean";
private Executor taskExecutor = new SimpleAsyncTaskExecutor();

View File

@@ -391,7 +391,7 @@ public class DirectMessageListenerContainer extends AbstractMessageListenerConta
protected void doInitialize() {
if (this.taskScheduler == null) {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setThreadNamePrefix(getBeanName() + "-consumerMonitor-");
threadPoolTaskScheduler.setThreadNamePrefix(getListenerId() + "-consumerMonitor-");
threadPoolTaskScheduler.afterPropertiesSet();
this.taskScheduler = threadPoolTaskScheduler;
}

View File

@@ -360,6 +360,29 @@ public class DirectMessageListenerContainerMockTests {
container.stop();
}
@Test
void monitorTaskThreadName() {
DirectMessageListenerContainer container = new DirectMessageListenerContainer(mock(ConnectionFactory.class));
assertThat(container.getListenerId()).isEqualTo("not.a.Spring.bean");
container.setBeanName("aBean");
assertThat(container.getListenerId()).isEqualTo("aBean");
container.setListenerId("id");
assertThat(container.getListenerId()).isEqualTo("id");
container.afterPropertiesSet();
assertThat(container).extracting("taskScheduler")
.extracting("threadNamePrefix")
.asString()
.startsWith("id-consumerMonitor");
container = new DirectMessageListenerContainer(mock(ConnectionFactory.class));
container.setBeanName("aBean");
container.afterPropertiesSet();
assertThat(container).extracting("taskScheduler")
.extracting("threadNamePrefix")
.asString()
.startsWith("aBean-consumerMonitor");
}
private Envelope envelope(long tag) {
return new Envelope(tag, false, "", "");
}