Spring JMS 生产者和消费者交互 [英] Spring JMS Producer and Consumer interaction
问题描述
如何确定消费者是否已经收到了来自生产者的消息,或者如何通知生产者该消息已经在 Spring JMS 中发送/消费了?
you need to use request/reply by using org.springframework.jms.core.JmsTemplate.sendAndReceive(...)
on生产者端和消费者端在容器中设置的 MessageListener 方法必须返回一个值.
或者让订阅 ActiveMQ.Advisory.MessageConsumed.Queue
的生产者通过访问一些属性(如 orignalMessageId
)在消息被消费时得到通知,看看这里http://activemq.apache.org/advisory-message.html>
你可以这样使用它:
目标advisoryDestination = org.apache.activemq.advisory.AdvisorySupport.getMessageConsumedAdvisoryTopic(session.createQueue("yourQueue"));MessageConsumer 消费者 = session.createConsumer(advisoryDestination);消费者.setMessageListener(新消息监听器(){@覆盖公共无效 onMessage(消息 msg){System.out.println(msg);System.out.println(((ActiveMQMessage) msg).getMessageId());ActiveMQMessage aMsg = (ActiveMQMessage) ((ActiveMQMessage) msg).getDataStructure();System.out.println(aMsg.getMessageId());}});
How can i determine if the consumer already received the message from the producer or how can i notify the producer that the message have already been sent/consumed in Spring JMS?
you need to use request/reply by using org.springframework.jms.core.JmsTemplate.sendAndReceive(...)
on the producer side and on the consumer side the MessageListener method set in the container must return a value.
or make your producer subscribing to ActiveMQ.Advisory.MessageConsumed.Queue
to be notified when the message is consumed by having access to some properties like orignalMessageId
, take a look here http://activemq.apache.org/advisory-message.html
you can use it like this :
Destination advisoryDestination = org.apache.activemq.advisory.AdvisorySupport
.getMessageConsumedAdvisoryTopic(session.createQueue("yourQueue"));
MessageConsumer consumer = session.createConsumer(advisoryDestination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message msg) {
System.out.println(msg);
System.out.println(((ActiveMQMessage) msg).getMessageId());
ActiveMQMessage aMsg = (ActiveMQMessage) ((ActiveMQMessage) msg).getDataStructure();
System.out.println(aMsg.getMessageId());
}
});
这篇关于Spring JMS 生产者和消费者交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!