Clone Message in Java ? (for DMQ move to Main Queue)

Robert
Robert Member Posts: 58 ✭✭

I have a problem with functionality which is not implemented in all offered programming languages. The use case i work on is a utility to move messages from DMQ back to main queue to re-publish the messages).
The problem is that there is a flag DMQ eligible
https://docs.solace.com/Solace-JMS-API/Setting-Message-Properties.htm#Dead

So i want to do following:

What i have done now is to do following and i wonder if there is a better way:

private Message cloneMessage(Message message) {
try {
HashMap<String, Object> propertyMap = new HashMap<>();
Enumeration propertyNames = message.getPropertyNames();
// Read all properties and read in Hashmap
while (propertyNames.hasMoreElements()) {
String property = propertyNames.nextElement().toString();
Object propertyObject = message.getObjectProperty(property);
propertyMap.put(property,propertyObject);
}
// Clear any properties from source message
Iterator propertyIterator = propertyMap.entrySet().iterator();
message.clearProperties();
while (propertyIterator.hasNext()) {
Map.Entry propertyElement = (Map.Entry)propertyIterator.next();
message.setObjectProperty(propertyElement.getKey().toString(),propertyElement.getValue());
}
} catch (Exception e) {
System.out.println("Error cloning message.");
e.printStackTrace();
}

    return message;
}
Tagged:

Answers

  • rlawrence
    rlawrence Member, Employee Posts: 14 Solace Employee

    Hi Robert, please see an example implementation for this here

  • Robert
    Robert Member Posts: 58 ✭✭

    @rlawrence thanks for sharing that. I currently build a GUI for moving messages (small JAVA Swing UI). You code sample using FlowReceiver was new to me:
    https://docs.solace.com/Solace-PubSub-Messaging-APIs/API-Developer-Guide/Creating-Flows.htm

    I tested your util and it works as intended. I just see one concern and that is that is fails and rolls back when you find no new messages anymore. I would personally change that as you can provide a count but often you do not exactly know if now 100 or 102 messages in DMQ.
    So likely you would move all back to resend.

    Maybe there was a good reason to stop if the given count is bigger then actual messages in Queue.

  • rlawrence
    rlawrence Member, Employee Posts: 14 Solace Employee

    @Robert thanks for the feedback, my thinking was that if a given message count is greater than the number of messages in the queue then something may be wrong, such as the wrong queue name was given or another application is reading from the queue at the same time, so its probably safer to rollback the transaction in this case. I would always recommend first running the tool with the -nop option to first check that the operation is working as expected. Also enabling DEBUG log level will dump message contents to the screen which can be useful to first check which messages are required to be resent.

  • Robert
    Robert Member Posts: 58 ✭✭

    @rlawrence i think it depends all on difference use cases. Everyone can take your code (open source) and modify to their needs. It was helping me and many thanks for the input.