0
如何確定消費者是否已收到來自生產者的消息,或者如何通知生產者消息已在Spring JMS中發送/消耗?Spring JMS生產者和消費者互動
如何確定消費者是否已收到來自生產者的消息,或者如何通知生產者消息已在Spring JMS中發送/消耗?Spring JMS生產者和消費者互動
您需要在生產者端和消費者端使用org.springframework.jms.core.JmsTemplate.sendAndReceive(...)
的請求/回覆,容器中設置的MessageListener方法必須返回一個值。
或使生產者訂閱ActiveMQ.Advisory.MessageConsumed.Queue
當消息是由具有類似於orignalMessageId
獲得一些性能消耗通知,採取這裏http://activemq.apache.org/advisory-message.html
一看,你可以使用它像這樣:
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());
}
});
如果你只是想通知,那麼你可以使用AUTO_ACK模式,但如果你需要在異常情況下重新交付,那麼你應該使用transacted =「true」。這已經在SO上被問到了。檢查此:http://stackoverflow.com/questions/9095471/getting-a-simple-spring-jms-client-acknowledge-to-work – Tarun