[英]How can I set the name of the threads created by DefaultMessageListenerContainer?
I am working in Java code, without using xml to configure spring.我在 Java 代码中工作,没有使用 xml 来配置 spring。 I suspect I need to subclass it, but how can I do that and have spring use my version of it?
我怀疑我需要对它进行子类化,但是我该怎么做才能让 spring 使用我的版本呢? I tried this:
我试过这个:
@Bean
public DefaultMessageListenerContainer messageListenerContainer() {
DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer() {
@Override
protected TaskExecutor createDefaultTaskExecutor() {
String beanName = this.getBeanName();
String threadNamePrefix = "MyThreadNamePrefix";
return new SimpleAsyncTaskExecutor(threadNamePrefix);
}
};
messageListenerContainer.setConcurrency();
return messageListenerContainer;
}
At runtime I get this error if I do that:如果我这样做,在运行时我会收到此错误:
Error creating bean with name 'messageListenerContainer' defined in class path resource [com/hexagon/apollo/tasks/TAplTaskConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'connectionFactory' is required
How can I make this work, or is there a better way to do it?我怎样才能完成这项工作,或者有更好的方法吗?
Property 'connectionFactory' is required
需要属性“connectionFactory”
The error is quite obvious, you are defining the container bean without setting the required connectionFactory
property.错误非常明显,您在定义容器 bean 时没有设置所需的
connectionFactory
属性。
/**
* Set the ConnectionFactory to use for obtaining JMS {@link Connection Connections}.
*/
public void setConnectionFactory(@Nullable ConnectionFactory connectionFactory) {
It has nothing to do with the thread naming.它与线程命名无关。
Furthermore, there is no need to subclass it;此外,没有必要对其进行子类化; just inject a TE:
只需注入一个 TE:
/**
* Set the Spring {@code TaskExecutor} to use for running the listener threads.
* <p>Default is a {@link org.springframework.core.task.SimpleAsyncTaskExecutor},
* starting up a number of new threads, according to the specified number
* of concurrent consumers.
* <p>Specify an alternative {@code TaskExecutor} for integration with an existing
* thread pool. Note that this really only adds value if the threads are
* managed in a specific fashion, for example within a Jakarta EE environment.
* A plain thread pool does not add much value, as this listener container
* will occupy a number of threads for its entire lifetime.
* @see #setConcurrentConsumers
* @see org.springframework.core.task.SimpleAsyncTaskExecutor
*/
public void setTaskExecutor(Executor taskExecutor) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.