Search code examples
spring-integrationhornetq

Simple publish/subscribe with spring-integration


I have a queue and topic setup in a spring-boot application.properties as follows:

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

I need to send messages from one application to another.

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

From app1 I publish a part as follows:

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

In the bridge project I get it on the queue and then route it to the 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 then subscribes to parts.topic and processes the message.

This is working, however, the above bridge code seems like it might be an overkill for what I am trying to do. I am guessing I only need a parts.topic and not the parts.queue at all.

Can the above spring-integration XML be simplified in some way?


Solution

  • Well a found a much simpler solution:

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

    No need for a queue, not that you need to setup your jmsTemplate to use spring.jms.pub-sub-domain=true in application.properties.

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

    an that is it.