Skip to content
Advertisement

Spring and JMS DynamicDestinationResolution

I am using the latest Spring 4 and ActiveMQ to put JMS messages on a queue. Using the JMSTemplate, I have a default queue, and the sample code I have lets me put a message on the default queue with no issues. There is also a sample code that lets me put a message on a Destination … this is where I am hung up.

original method:

public void send(final Destination dest,final String text) {

this.jmsTemplate.send(dest,new MessageCreator() {
  @Override
  public Message createMessage(Session session) throws JMSException {
    Message message = session.createTextMessage(text);
    return message;
  }
});
}

If I have a Destination, I can pass that in and it should work, but I haven’t tried it yet. What I really want to do is pass in a string for a name or a topic.

Here is what I’d like:

public void send(final String destination,final String text) {

    Destination dest = getDestinationFromString(destination);

    if( dest != null ) {

    this.jmsTemplate.send(dest,new MessageCreator() {
     @Override
       public Message createMessage(Session session) throws JMSException {
       Message message = session.createTextMessage(text);
       return message;
        }
     });
  }
}

If the queue or topic exist, return that Destination, otherwise return null.

We are not wanting temporary queues or topics, and we are not creating new queues or topics on the fly. We are also not using JNDI within this Spring application. We use the ActiveMQ web-admin tool to create our topics or queues.

So, I was looking for an example of a method like I described. I have scoured the net before I came here, and I looked here first before I posted this question. If someone can refer me to some documentation or a site that has a code snippet for this, that would be great.

Thanks for the help!

Advertisement

Answer

Turns out I didn’t need to do anything. Here is how my activemq is defined in the context xml file:

<!-- =============================================== -->
<!-- JMS Common, Define JMS connectionFactory -->
<!-- =============================================== -->
<!-- Activemq connection factory -->
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <!-- brokerURL, You may have different IP or port -->
    <constructor-arg index="0" value="${message.broker.url}" />
</bean>

<!-- Pooled Spring connection factory -->
<bean id="jmsConnectionFactory"
    class="org.springframework.jms.connection.CachingConnectionFactory">
    <constructor-arg ref="amqConnectionFactory" />
</bean>

<!-- ======================================================= -->
<!-- JMS Send, define default destination and JmsTemplate -->
<!-- ======================================================= -->
<!-- Default Destination Queue Definition -->
<bean id="defaultDestination" class="org.apache.activemq.command.ActiveMQQueue">
    <!-- name of the queue -->
    <constructor-arg index="0" value="${default.message.queue}" />
</bean>

<bean id="jmsDestinationResolver" class="org.springframework.jms.support.destination.DynamicDestinationResolver"/>

<!-- JmsTemplate Definition -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="jmsConnectionFactory"/>
    <property name="defaultDestination" ref="defaultDestination" />
    <property name="destinationResolver" ref="jmsDestinationResolver"/>
    <property name="pubSubDomain" value="${pub.sub.domain}"/>
    <property name="receiveTimeout" value="${receive.timeout}"/>
</bean>

When I was looking at the different methods under ‘jmsTemplate’ I didn’t realize there was a .send method with String for a destination name. I knew there was a send method with Destination as the first parameter. So, there was really no issue. This method works fine.

  public void sendToDestination(final String destination, final MyObjectDTO myObject) throws JMSException {
    this.jmsTemplate.send(destination, new MessageCreator() {
        @Override
        public Message createMessage(Session session) throws JMSException {
            Message message = session.createObjectMessage(myObject);
            return message;
        }
    });
    return success;
}

Hope this helps someone out.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement