如何为使用嵌入式ActiveMQ Artemis的Spring Boot配置max-delivery-attempts
解决方案
你可以通过以下两种方式配置嵌入式ActiveMQ Artemis的max-delivery-attempts参数:
方式1:自定义配置Bean注入(全版本适配,推荐)
直接注册Artemis核心配置Bean,在全局地址规则中配置最大投递尝试次数:
import org.apache.activemq.artemis.core.config.Configuration; import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl; import org.apache.activemq.artemis.core.settings.impl.AddressSettings; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ArtemisCustomConfig { @Bean public Configuration artemisCoreConfiguration() throws Exception { Configuration config = new ConfigurationImpl(); // 保留默认的VM连接器配置 config.addAcceptorConfiguration("in-vm", "vm://0"); AddressSettings globalSettings = new AddressSettings(); // 自定义最大投递尝试次数,示例为投递失败3次后进入死信队列 globalSettings.setMaxDeliveryAttempts(3); // 规则#代表匹配所有地址,可按需替换为指定地址的匹配规则 config.addAddressesSetting("#", globalSettings); return config; } }
方式2:通过broker.xml配置文件设置
如果习惯使用原生Artemis配置文件,可按以下步骤操作:
- 在项目
resources目录下新建broker.xml,添加地址配置:
<configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd"> <core> <address-settings> <address-setting match="#"> <max-delivery-attempts>3</max-delivery-attempts> <!-- 按需补充死信队列、过期队列等其他配置 --> <dead-letter-address>DLQ</dead-letter-address> </address-setting> </address-settings> </core> </configuration>
- 在
application.yml中指定加载该配置文件:
spring: artemis: embedded: config: classpath:broker.xml
注意事项
- 若需要针对特定地址单独配置,将匹配规则
#替换为对应地址的匹配规则即可 - 若将
max-delivery-attempts设置为-1,代表消息会无限重试,永远不会进入死信队列
内容的提问来源于stack exchange,提问作者nonono
相关产品推荐
相关产品推荐

