问题
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?
回答1:
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());
}
});
来源:https://stackoverflow.com/questions/42415468/spring-jms-producer-and-consumer-interaction