2017-02-22 90 views
0

我正在使用最新的Spring 4和ActiveMQ將JMS消息放入隊列中。使用JMSTemplate,我有一個默認隊列,並且我有示例代碼可以讓我在默認隊列中輸入一條消息,而不會出現任何問題。還有一個示例代碼,可以讓我在Destination上輸入消息......這是我掛斷的地方。Spring和JMS DynamicDestinationResolution

原始的方法:

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; 
    } 
}); 
} 

如果我有一個目標,我可以通過在它應該工作,但我還沒有嘗試過呢。我真正想要做的是傳遞一個字符串作爲名稱或主題。

這裏是我想什麼:

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; 
     } 
    }); 
    } 
} 

如果隊列或主題存在,返回目的地,否則返回null。

我們不想臨時排隊或討論話題,我們也沒有創建新的隊列或話題。我們在這個Spring應用程序中也沒有使用JNDI。我們使用ActiveMQ網絡管理工具來創建我們的主題或隊列。

所以,我正在尋找像我描述的方法的例子。在我來這裏之前,我已經搜遍了網絡,在我發佈這個問題之前,我先看了這裏。如果有人可以向我推薦一些文檔或者有代碼片段的網站,那就太棒了。

感謝您的幫助!

回答

0

原來我不需要做任何事情。這裏是我的ActiveMQ是如何在上下文中的XML文件中定義:

<!-- =============================================== --> 
<!-- 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> 

當我看着在「JmsTemplate的」不同的方法我都沒有意識到有一個與字符串的目的地名稱的方法。發送。我知道有一個發送方法,將Destination作爲第一個參數。所以,真的沒有問題。此方法工作正常。

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; 
} 

希望這可以幫助別人。