首页 > 解决方案 > How to create Artemis last-value queue with Spring Boot 2.2 and embedded broker?

问题描述

I am trying to create an ActiveMQ Artemis queue with last-value property enabled.

My app is using Spring Boot 2.2.6, and i am using Artemis as an embedded broker.


Spring Boot has a spring.artemis.embedded.queues property, which i tried to set as follows:

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

But that doesn't seem to work.

The Artemis documentation mentions 2 ways of configuring the queue:

  1. Using a broker.xml configuration file, but i couldn't make this work.
  2. Getting hold of a CORE session object, but I didn't manage to get hold of that object via Spring.

Is there an easy way to configure a last-value queue using Spring Boot, either via application.yml, or via Java/Kotlin configuration ?


Here is my test code:

@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-bootkotlinactivemq-artemis

解决方案


挖掘 Spring Boot 的代码,我发现可以提供一个ArtemisConfigurationCustomizer来:

在自动配置的 EmbeddedActiveMQ 实例使用之前自定义 Artemis JMS 服务器配置

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

推荐阅读