I am building a spring boot application with a cluster of ActiveMQ servers. In order to solve the duplicate message issue in the consumer side, all the dequeued JMSMessageID will be saved in redis, and the consumer will ignore all the new messages with duplicate IDs in redis.
In order to achieve this, I need all the JMSMessageID to be unique. I tried to set JMSMessageID in the producer code, but it seems that I cannot change it. My question is that in the distributed system(multiple application servers, and a cluster of ActiveMQ server), will the system generated JMSMessageID be unique?
jmsTemplate.send(name, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
Email email = new Email("l****@gmail.com", "this is test body", "test");
ObjectMessage objectMessage = session.createObjectMessage();
objectMessage.setJMSMessageID(customID);
return objectMessage;
}
});
2
Answers
The JMSMessageID is set by the JMS provider. It cannot be set by the client as per the JMS specification. The JMS spec does guarantee the uniqueness of the JMSMessageID. Section 3.4.3 of both the JMS 1.1 & 2 specifications says:
Send a generic message
GenericMessage(T payload, Map<String,Object> headers)
. The payload will be your object, and in the header you can inject some unique value, with which you can uniquely identify the message.