具有 spring 集成的简单 publish/subscribe

Simple publish/subscribe with spring-integration

我在 spring-boot application.properties 中有一个队列和主题设置如下:

spring.hornetq.embedded.queues=parts.queue
spring.hornetq.embedded.topics=parts.topic

我需要从一个应用程序向另一个应用程序发送消息。

app1 (publisher)
app2 (subscriber)
bridge (contains the integration code)

我从app1发布一部分如下:

this.jmsTemplate.convertAndSend("parts.queue", message);

在桥梁项目中,我在 queue 上获取它,然后将其路由到 topic

<int:channel id="partsChannel" />

<int-jms:message-driven-channel-adapter
    id="jmsPartsInbound"
    acknowledge="transacted"
    destination-name="parts.queue"
    channel="partsChannel"
    connection-factory="jmsConnectionFactory"
    />

<int-jms:outbound-channel-adapter
    id="jmsPartsOutbound"
    destination-name="parts.topic"
    channel="partsChannel"
    connection-factory="jmsConnectionFactory"
    pub-sub-domain="true"
    >
    <int-jms:request-handler-advice-chain>
        <int:retry-advice max-attempts="3">
            <int:exponential-back-off initial="2000" multiplier="2" />
        </int:retry-advice>
    </int-jms:request-handler-advice-chain>
</int-jms:outbound-channel-adapter>

app2 然后订阅 parts.topic 并处理消息。

这是有效的,但是,上面的桥接代码似乎对我正在尝试做的事情来说有点矫枉过正。我猜我只需要 parts.topic 而根本不需要 parts.queue。

上面的 spring-集成 XML 可以用某种方式简化吗?

好吧,我找到了一个更简单的解决方案:

<int-jms:publish-subscribe-channel id="partsPubSubChannel" topic-name="parts.topic" connection-factory="jmsConnectionFactory"/>

不需要队列,并不是说您需要设置 jmsTemplate 才能在 application.properties 中使用 spring.jms.pub-sub-domain=true

this.jmsTemplate.convertAndSend("parts.topic", 留言);

就是这样。

还不完全清楚为什么不能只使用...

this.jmsTemplate.convertAndSend("parts.topic", message);