使用 Spring Integration 向 ActiveMQ Artemis 主题发送消息

Csu*_*uki 4 jms spring-integration activemq-artemis spring-integration-dsl

目标

我想向某个主题发送一条消息,稍后我将使用客户端应用程序处理该消息。为此,我使用 Spring Boot 和 Spring Integration Java DSL 及其 JMS 模块。作为消息代理,我使用本机 ActiveMQ Artemis。


这是我的设置

演示应用程序.java

@SpringBootApplication
public class DemoApplication {

    private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class);

    public interface StarGate {
        void sendHello(String helloText);
    }

    @Autowired
    private ConnectionFactory connectionFactory;

    @Bean
    public IntegrationFlow mainFlow() {
        return IntegrationFlows
                .from(StarGate.class)
                .handle(Jms.outboundAdapter(connectionFactory)
                        .configureJmsTemplate(jmsTemplateSpec -> jmsTemplateSpec
                                .deliveryPersistent(true)
                                .pubSubDomain(true)
                                .sessionTransacted(true)
                                .sessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE)
                                .explicitQosEnabled(true)
                        )
                        .destination(new ActiveMQTopic("wormhole")))
                .get();
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        StarGate stargate = context.getBean(StarGate.class);
        stargate.sendHello("Jaffa, kree!");
        logger.info("Hello message sent.");
    }

}
Run Code Online (Sandbox Code Playgroud)

应用程序属性

spring.artemis.mode=native
spring.artemis.host=localhost
spring.artemis.port=61616
spring.artemis.user=artemis
spring.artemis.password=simetraehcapa

spring.jms.pub-sub-domain=true
spring.jms.template.delivery-mode=persistent
spring.jms.template.qos-enabled=true
spring.jms.listener.acknowledge-mode=client

logging.level.org.springframework=INFO
Run Code Online (Sandbox Code Playgroud)

build.gradle(重要部分)

springBootVersion = '2.0.2.RELEASE'
dependencies {
    compile('org.springframework.boot:spring-boot-starter-artemis')
    compile('org.springframework.boot:spring-boot-starter-integration')
    compile('org.springframework.integration:spring-integration-jms')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}
Run Code Online (Sandbox Code Playgroud)

作为 ActiveMQ Artemis 服务器,我使用默认配置的 vromero/artemis (2.6.0) docker 映像。


问题

在生产者端,消息似乎已成功发送,但在消息代理端,消息丢失。地址已创建,但队列丢失。

ActiveMq Artemic 控制台地址 ActiveMq Artemic 控制台队列

主题的名称将来会是动态的,所以我不允许在broker.xml中手动创建主题。我依赖 Artemis 的自动队列创建功能。

为什么在这种情况下消息发送不起作用?


书呆子笔记:我知道星门基本上是通过虫洞以点对点的方式连接的,但为了这个问题,让我们忽略这个事实。

Jus*_*ram 5

当消息发送到主题并且为地址和队列启用自动创建时,只会创建地址而不是队列。如果自动创建队列并将消息放入队列中,则会违反主题的语义。主题地址上的订阅队列仅是为了响应订阅者而创建的。因此,在发送消息之前,您需要该主题的订阅者,否则消息将被删除(根据主题语义)。