Skip to content

Instantly share code, notes, and snippets.

@srkiNZ84
Last active June 20, 2019 23:23
Show Gist options
  • Save srkiNZ84/5277c9e26209e7f4772234b68cca5766 to your computer and use it in GitHub Desktop.
Save srkiNZ84/5277c9e26209e7f4772234b68cca5766 to your computer and use it in GitHub Desktop.
Groovy script to connect to an ActiveMQ server and receive messages from the default Dead Letter Queue
#!/bin/groovy
// NOTE: The jar "activemq-all-5.15.8.jar" needs to be put under $HOME/.groovy/lib
// for this script to work
import javax.jms.*
import org.apache.activemq.*
def amqURL = "failover:tcp://localhost:61616"
println "Connecting to ActiveMQ at URL " + amqURL
def connFactory = new ActiveMQConnectionFactory(amqURL)
def conn = connFactory.createConnection()
conn.start()
println "Creating session"
def session = conn.createSession(true, ActiveMQSession.CLIENT_ACKNOWLEDGE)
def dest = session.createQueue("ActiveMQ.DLQ")
def consumer = session.createConsumer(dest)
def duplicateLineIDCount = 0;
def otherDLQMessageCount = 0;
println("Waiting for messages...");
while(true) {
msg = consumer.receive();
//println "Received message " + msg.toString()
if( msg instanceof TextMessage ) {
if(msg.getProperty("dlqDeliveryFailureCause") && msg.getProperty("dlqDeliveryFailureCause").indexOf("NonUniqueResultException: Unique expecting 0 or 1 results but got") > 0){
println "Got duplicate LineID message with values: " + msg.getText()
msg.acknowledge()
session.commit()
duplicateLineIDCount++
}
else {
println "#------------------#"
println "Got an unfamiliar DLQ messge: " + msg
println "#------------------#"
session.rollback()
otherDLQMessageCount++
}
}
println "Total Duplicate Line ID: " + duplicateLineIDCount + ", total Other DLQ Messages: " + otherDLQMessageCount
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment