java 如何设置由DefaultMessageListenerContainer创建的线程的名称?

5vf7fwbs  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(152)

我使用的是Java代码,没有使用xml来配置spring。我怀疑我需要对它进行子类化,但是我如何才能做到这一点,并让spring使用我的版本呢?我尝试了以下方法:

@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;
  }

在运行时,如果我这样做,我会得到这个错误:

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

我怎样才能做到这一点,或者有更好的方法吗?

htzpubme

htzpubme1#

需要属性“connectionFactory”
错误非常明显,您定义容器bean时没有设置所需的connectionFactory属性。

/**
 * Set the ConnectionFactory to use for obtaining JMS {@link Connection Connections}.
 */
public void setConnectionFactory(@Nullable ConnectionFactory connectionFactory) {

它与线程命名无关。
而且,不需要对它进行子类化;只需注入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) {

相关问题