簡體   English   中英

如何設置 DefaultMessageListenerContainer 創建的線程的名稱?

[英]How can I set the name of the threads created by DefaultMessageListenerContainer?

我在 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

我怎樣才能完成這項工作,或者有更好的方法嗎?

需要屬性“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) {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM