Java Spring和JMS DynamicDestinationResolution
我正在使用最新的Spring4和ActiveMQ将JMS消息放到队列中。使用JMSTemplate,我有一个默认队列,我的示例代码允许我在默认队列上放置一条消息,没有任何问题。还有一个示例代码,可以让我在目的地上放置消息。。。这就是我挂断电话的地方 原始方法:Java Spring和JMS DynamicDestinationResolution,java,spring,jms,activemq,spring-jms,Java,Spring,Jms,Activemq,Spring Jms,我正在使用最新的Spring4和ActiveMQ将JMS消息放到队列中。使用JMSTemplate,我有一个默认队列,我的示例代码允许我在默认队列上放置一条消息,没有任何问题。还有一个示例代码,可以让我在目的地上放置消息。。。这就是我挂断电话的地方 原始方法: public void send(final Destination dest,final String text) { this.jmsTemplate.send(dest,new MessageCreator() { @Over
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 web管理工具来创建主题或队列
所以,我在寻找一个像我所描述的方法的例子。在我来这里之前,我已经搜索过了,在我发布这个问题之前,我先看了这里。如果有人能让我参考一些文档或者一个有代码片段的网站,那就太好了
谢谢你的帮助 结果证明我什么都不需要做。以下是如何在上下文xml文件中定义我的activemq:
<!-- =============================================== -->
<!-- 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>
希望这能帮助别人
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;
}