The Apprentice The Apprentice - 1 year ago 126
Java Question

How to listen to messages from an MQ using Spring 4's JmsTemplate?

Working with WebSphere MQ in a Spring 4 application and having some trouble with listening to the MQ (sending messages works just fine).

Here is a piece of my

file where I specify my information for my

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="appJmsConnectionFactory" />
<property name="defaultDestinationName" value="MQ.LISTENER.INFO.HERE" />
<property name="sessionTransacted" value="true" />

Here is my listener class:

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

public class SpringJmsConsumer {
private JmsTemplate jmsTemplate;

private Destination destination;

public JmsTemplate getJmsTemplate() {
return jmsTemplate;

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

public Destination getDestination() {
return destination;

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

public String receiveMessage() throws JMSException {
Message message = jmsTemplate.receive(jmsTemplate.getDefaultDestination());
// TextMessage textMessage = (TextMessage)
// jmsTemplate.receive(destination);
System.out.println("The message listened is: " + message.toString());
return message.toString();

The error I get:

org.springframework.jms.InvalidDestinationException: JMSMQ0003: The destination is not understood or no longer valid.; nested exception is JMSMQ0003: The destination is not understood or no longer valid. The queue or topic might have become unavailable, the application might be using an incorrect connection for the queue or topic, or the supplied destination is not of the correct type for this method.

I have performed research on how to solve this issue, but unable to arrive at a proper solution.

Answer Source

Figured this out a while ago but figured I would post the answer here to help anyone else out in the future.

When using Spring's JMS functionality, the listener needs to be annotated with @JmsListener

With that being said here is the updated function of receiveMessage():

    @JmsListener(destination = "MQ.LISTENER.INFO.HERE")
    public String receiveMessage(String text) throws JMSException {"Received something!");
        this.jmsTemplate.receiveAndConvert("MQ.LISTENER.INFO.HERE");"Received message from MQ.LISTENER.INFO.HERE: ");
        return "Acknowledgement from receiveMessage";

Keep in mind it is a callback function!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download