简体   繁体   English

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

[英]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.

相关问题 如何在Spring中创建动态数量的DefaultMessageListenerContainer MDPojos? - How can I create a dynamic number of DefaultMessageListenerContainer MDPojos in Spring? 如何阻止使用匿名类创建的线程? - How can I stop threads created with an anonymous class? 如何限制JMS DefaultMessageListenerContainer重试消息的次数? - How do I limit the amount of times a JMS DefaultMessageListenerContainer will retry a message? 我的程序不断创建多个线程,而我只希望创建一个后台线程。 如何防止创建额外的线程? - My program keeps creating multiple threads where I only want one background thread created. How can I prevent additional threads from being created? 如何确保没有自定义名称不会创建任何线程? - How to make sure that no threads are created without a custom name? 如何模拟DefaultMessageListenerContainer - How to mock DefaultMessageListenerContainer 将会话/事务的timeOut设置为DefaultMessageListenerContainer - Set timeOut for session/transaction to DefaultMessageListenerContainer 如何同时创建线程 - How are threads created concurrently 可以为双核处理器创建多少个Java线程 - How many java threads can be created for a dual core processor 如何在“主”线程上运行DefaultMessageListenerContainer - How to run DefaultMessageListenerContainer on the 'main' thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM