如何使用 Spring Boot 2.2 和嵌入式代理创建 Artemis 最后值队列?

How to create Artemis last-value queue with Spring Boot 2.2 and embedded broker?

我正在尝试创建启用 last-value 属性 的 ActiveMQ Artemis 队列。

我的应用程序使用 Spring Boot 2.2.6,我使用 Artemis 作为嵌入式代理。


Spring Boot 有一个 spring.artemis.embedded.queues 属性,我试着设置如下:

spring.artemis.embedded.queues: myqueue?last-value-key=code

但这似乎不起作用。

Artemis 文档提到了 2 种配置队列的方法:

  1. 使用 broker.xml 配置文件,但我无法完成这项工作。
  2. 获取一个 CORE 会话对象,但我没能通过 Spring.
  3. 获取该对象

是否有使用 Spring 引导配置最后值队列的简单方法,通过 application.yml 或通过 Java/Kotlin 配置?


这是我的测试代码:

@ExtendWith(SpringExtension::class)
@SpringBootTest
class ArtemisTest(
  @Autowired private val jmsTemplate: JmsTemplate
) {

  @Test
  fun testMessage() {
    for(i in 1..5) {
      jmsTemplate.convertAndSend(
        "myqueue",
        "message $i"
      ) {
        it.also { it.setStringProperty("code", "1") }
      }
    }

    val size = jmsTemplate.browse("myqueue") { _: Session, browser: QueueBrowser ->
      browser.enumeration.toList().size
    }

    assertThat(size).isEqualTo(1)
  }
}

通过 Spring 引导代码挖掘,我发现可以提供一个 ArtemisConfigurationCustomizer 到 :

customize the Artemis JMS server Configuration before it is used by an auto-configured EmbeddedActiveMQ instance

@Configuration
class ArtemisConfig : ArtemisConfigurationCustomizer {
  override fun customize(configuration: org.apache.activemq.artemis.core.config.Configuration?) {
    configuration?.let {
      it.addQueueConfiguration(
        CoreQueueConfiguration()
          .setAddress("myqueue")
          .setName("myqueue")
          .setLastValueKey("code")
          .setRoutingType(RoutingType.ANYCAST)
      )
    }
  }
}