2017-02-22 91 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; 
} 

希望这可以帮助别人。