spring-boot - 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:
- Using a
broker.xml
configuration file, but i couldn't make this work. - 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 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)
)
}
}
}
推荐阅读
- c# - C#在函数之间传递数据
- c# - Microsoft Graph 令牌缺少“帐户”属性
- python - 在 Python 中转换时区
- objective-c - 从转换 NSDictionary 对象创建 Swift 选项
- discord.py - 给定命令时如何使机器人分配角色
- java - 多个流过滤器或 forEach if-else
- python - 将浮点数放在python中的f.write中
- swift - 在 Swift 中使用 Cuckoo,为什么调用 stub 会抱怨 GeneratedMocks 中的类不符合 Mock 协议?
- docker - 将服务部署到 docker swarm 时,Traefik 未更新
- c# - 创建一个类来存储使用不同接口的类