Announcement Announcement Module
Collapse
No announcement yet.
Multiple JMS destinations from one Sender Class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple JMS destinations from one Sender Class

    I have a JMS Sender class with two methods which sends different messages to different queues:

    Class SomeClass{

    public void methodA() {
    jmsTemplate.send(destination, new MessageCreator() {
    public Message createMessage(Session session) throws JMSException {
    MapMessage message = session.createMapMessage();
    message.setInt("a", a);
    return message;
    }
    });
    }

    public void methodB(){
    jmsTemplate.send(destination, new MessageCreator() {
    public Message createMessage(Session session) throws JMSException {
    MapMessage message = session.createMapMessage();
    message.setInt("b", b);
    return message;
    }
    });
    }

    private JmsTemplate jmsTemplate;
    public void setJmsTemplate(JmsTemplate jmsTemplate){
    this.jmsTemplate=jmsTemplate;
    }

    private Destination destination;
    public void setDestination(Destination destination){
    this.destination=destination;
    }

    }


    Now I want to set QueueA for methodA, and a different queue QueueB for methodB.

    How can I configure this in the application context xml??

    I am able to do this for a single queue, but I am clueless about multiple queues. Any help guys??

    My config xml for a methodA pushing to QueueA is :

    <bean id="queueA" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName" value="queue/QueueA"/>
    </bean>

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name ="connectionFactory" ref="jmsQueueConnectionFactory"/>
    <property name ="defaultDestination" ref="queueA"/>
    </bean>

    <bean id="someClass" class="SomeClass">
    <property name="jmsTemplate" ref="jmsTemplate"/>
    </bean>

    <!-- Configuring the Listener -->
    <!-- this is the Message Driven POJO (MDP) -->
    <bean id="messageListener" class="QueueAMDB"/>

    <!-- and this is the message listener container -->
    <bean id="messageListenerContainer"
    class="org.springframework.jms.listener.DefaultMes sageListenerContainer">
    <property name="connectionFactory" ref="jmsQueueConnectionFactory" />
    <property name="destination" ref="queueA" />
    <property name="messageListener" ref="messageListener" />
    </bean>

    Thanks a lot in advance!

  • #2
    Define two JmsTemplates in your config and inject both of them into your class.
    Something like:
    Code:
    <bean id="queueA" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName" value="queue/QueueA"/>
    </bean>
    
    <bean id="jmsTemplateA" class="org.springframework.jms.core.JmsTemplate">
    <property name ="connectionFactory" ref="jmsQueueConnectionFactory"/>
    <property name ="defaultDestination" ref="queueA"/>
    </bean>
    
    <bean id="queueB" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName" value="queue/QueueB"/>
    </bean>
    
    <bean id="jmsTemplateB" class="org.springframework.jms.core.JmsTemplate">
    <property name ="connectionFactory" ref="jmsQueueConnectionFactory"/>
    <property name ="defaultDestination" ref="queueB"/>
    </bean>
    
    <bean id="someClass" class="SomeClass">
    <property name="jmsTemplateA" ref="jmsTemplateA"/>
    <property name="jmsTemplateB" ref="jmsTemplateB"/>
    </bean>

    Comment


    • #3
      Thanks a lot for clarifying this. But how does the methodA know which queue to push the message to?

      How do I set the destination for each of the methods?

      Comment


      • #4
        No problem. I can use the jmsTemplateA and jmsTemplateB for sending the message to appropriate queue. Thanks a lot.

        Comment

        Working...
        X