Spring 和 JMS DynamicDestinationResolution

Spring and JMS DynamicDestinationResolution

我正在使用最新的 Spring 4 和 ActiveMQ 将 JMS 消息放入队列。使用 JMSTemplate,我有一个默认队列,我的示例代码让我可以毫无问题地将消息放入默认队列。还有一个示例代码可以让我在目标上发送消息...这就是我挂断电话的地方。

原始方法:

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

如果我有 Destination,我可以将它传递进去,它应该可以工作,但我还没有尝试过。我真正想做的是传入一个字符串作为名称或主题。

这是我想要的:

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

如果队列或主题存在,return那个目的地,否则return空。

我们不需要临时队列或主题,也不会即时创建新队列或主题。我们也没有在此 Spring 应用程序中使用 JNDI。我们使用 ActiveMQ 网络管理工具来创建我们的主题或队列。

所以,我一直在寻找我所描述的方法的示例。来这里之前已经搜过网了,发这个问题之前先看这里的。如果有人可以向我推荐一些文档或具有此代码片段的网站,那就太好了。

感谢您的帮助!

原来我不需要做任何事情。以下是我的 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' 下的不同方法时,我没有意识到有一个 .send 方法,其中包含用于目标名称的字符串。我知道有一个以 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;
}

希望这对某人有所帮助。