2016-07-27 79 views
1

在Spring 4應用程序中使用WebSphere MQ,並在偵聽到MQ(發送消息工作正常)時遇到一些麻煩。如何使用Spring 4的JmsTemplate從MQ偵聽消息?

這裏是我的一塊mvc-dispatcher-servlet.xml文件,我指定我的信息,我的JmsTemplate

<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" /> 
    </bean> 

這裏是我的監聽器類:

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; 

@Component 
public class SpringJmsConsumer { 
    @Autowired 
    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(); 
    } 
} 

的錯誤,我得到:

org.springframework.jms.InvalidDestinationException: JMSMQ0003: The destination is not understood or no longer valid.; nested exception is com.ibm.msg.client.jms.DetailedInvalidDestinationException: 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. 

我已經完成了如何解決這個問題的研究,但無法達成一個適當的解決方案。

回答

1

前一段時間發現了這個問題,但我認爲我會在此發佈答案以幫助其他人解決未來問題。

當使用Spring的JMS功能,聽者需要與@JmsListener

有了這樣說這裏是receiveMessage()更新的功能進行註釋:

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

請記住這是一個回調函數!

相關問題