Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you publish a JMS topic with Spring JMS?

I have a component that sends messages to a queue to be handled by another system. It should also publish a topic about job statuses every once in a while. Can I just use the same JmsTemplate used to send to a queue AND to publish to a topic?

I created a new topic in ActiveMQ, except that when I send from JmsTemplate a message, a new queue with the topic name gets created with the sent message (instead of sending the data to the actual topic), what am I doing wrong here?

here's my config:

<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">     <constructor-arg ref="amqConnectionFactory" />     <property name="exceptionListener" ref="jmsExceptionListener" />     <property name="sessionCacheSize" value="100" /> </bean>  <!--  JmsTemplate Definition --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">     <constructor-arg ref="connectionFactory" /> </bean>   <bean id="messageFacade" class="org.foo.MessageFacadeJms">     <property name="jmsTemplate" ref="jmsTemplate" /> </bean> 

MessageFacadeJms is the class I use to send a queue message (and it works), can I also just used that to publish a topic?

Can I just use this to do both queue sending and topic publishing?:

jmsTemplate.convertAndSend("TOPIC_NAME" /* or queue name */, message); 
like image 757
wsb3383 Avatar asked Aug 19 '10 02:08

wsb3383


People also ask

How can I send a message to JMS topic in Spring boot?

As we want to send a message to a topic we need to update our SenderConfig configuration. Use the setPubSubDomain() method on the JmsTemplate to set pubSubDomain to true . If you are using the autoconfigured JmsTemplate you can change the JMS domain by setting the spring. jms.

How does JMS queue connect to Spring?

First you start with the setup of the Spring application. You should place the @EnableJms annotation to enable Jms support and then setup a new queue. The listener component (BookMgrQueueListener. java) is using Spring's @JmsListener annotation with selectors to read the messages with a given Operation header.


1 Answers

This might seem a bit odd, you need to tell JmsTemplate that it's a topic rather than a queue, by setting its pubSubDomain property to true.

That means you're going to need two JmsTemplate beans, one for the queue, and one for the topic:

<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">    <constructor-arg ref="connectionFactory" />    <property name="pubSubDomain" value="false"/> </bean>  <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">    <constructor-arg ref="connectionFactory" />    <property name="pubSubDomain" value="true"/> </bean> 
like image 85
skaffman Avatar answered Sep 28 '22 12:09

skaffman