• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Questions about JMS topics

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am a newbie to spring, and wanted to know if there is any way to subscribe to multiple events published to a bus asynchronously.

In essence, does spring support the notion of a jms topic? I did some research around this, but could not come up with anything.

Any examples/sample codes would be most appreciated.

Sincerely,

AJ
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Spring definitely simplifies using JMS.

Documentation can be found here
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jms.html

Basically you will define a bean for ConnectionFactory, a bean for your Topic, then if you want any of your classes to receive any messages on the Topic you create a <jms:listener-container> inside that you create <jms:listener> where you assign either a MessageListener implementation or you can assign a POJO to be a listener in that configuration.

If you want to send a message to the Topic, then you use the JmsTemplate class to send a message.

Mark
 
Abhishek Joshi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Mark

Thanks for the response. Here is how I am trying to achieve it ...

1. I set up a jms topic in our spring config (applicationContext-jms.xml).

<!-- ActiveMQ destinations to use -->
<amq:topic id="destination1" physicalName="${foo.topic.name}"/>

2. I set up the 2 listener beans

<bean id="queueListener1" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean class="foo.listeners.EventListenerJmsConsumer1"/> </constructor-arg>
<property name="defaultListenerMethod" value="receive"/>
<property name="messageConverter" ref="marshallingConverter"/>
</bean>

<bean id="queueListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg>
<bean class="foo.listeners.EventListenerJmsConsumer"/>
</constructor-arg>
<property name="defaultListenerMethod" value="receive"/>
<property name="messageConverter" ref="marshallingConverter"/>
</bean>

3. I added a listener to the existing one ...

<jms:listener-container concurrency="10" >
<jms:listener id="QueueListener1" destination="${foo.topic.name}" ref="queueListener1" />
</jms:listener-container>

4. With the above done, I modified the EventListenerJmsProducer to accomodate

/**
* The JMS topic destination
*
* @param destination the destination to set
*/
public final void setDestinationtopic(String destination){
this.destinationtopic = destination;
}

The corresponding entries were mapped in the applicationContext-eventlistener.xml

<bean name="eventClient" class="com.foo.event.listeners.EventListenerJmsProducer">
<property name="destinationtopic" value="${foo.topic.name}"/> modified
<property name="jmsTemplate" ref="jmsTemplate"/>
</bean>

5. When all this was done, I created a dummy event and the producer published it to the topic ...

jmsTemplate.convertAndSend(destinationtopic, event.getPayload());

6. As the final part of the test, I caught the event published by the topic

Problem: I see either my consumer1 catching this event, or consumer2. In essence, I do not see a broadcast happening. Is there anything wrong I do here? Please let me know.
 
Abhishek Joshi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Mark, working against a deadline, hence the request again. Do you see any obvious mistakes wrt the problem I mention above? Thanks for all your help.
 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, personally that is way too much xml for me and I like POJO Listeners, not MessageListeners.



Here is my configuration to have both A and B listen to a Topic. Now the message contains an Order.



And now my two POJO beans A and B will be called when a message is received on the Topic, and say I get a default MessageConverter given to me, so say I have JAXB annotation in my Order that maps to xml that is in the Message, well, Spring will automatically convert the XML to my Order object.

That is the simplest approach.

Mark
 
I RELEASE YOU! (for now .... ) Feel free to peruse this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic