How could I delete a queue in activemq from java program? Is there anything like session.delelteQueue()?
Thanks M.
Simple solution that does not use JMX it to cast connection to ActiveMQConnection and use its destroyDestination() method. Simple utility that uses that approach:
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import javax.jms.JMSException;
/**
* simple class to delete a queue form the activeMQ broker
* @author josef.
*/
public class QueueDeleter {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("please specify broker URL and queue name, \nexample: tcp://localhost:61616 queue1");
System.exit(2);
}
ActiveMQConnection conn = null;
try {
conn = (ActiveMQConnection) new ActiveMQConnectionFactory(args[0]).createConnection();
conn.destroyDestination(new ActiveMQQueue(args[1]));
} catch (JMSException e) {
System.out.println("Error connecting to the browser please check the URL" + e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (JMSException e) {
System.out.println("Error closing connection" + e);
}
}
}
}
}
Dependency for Maven
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
If you don't mind using a non-JMS API call then you can cast your Connection object to an ActiveMQConnection and call destroyDestination passing it an instance of the destination you want to remove. Provided there are no active consumers on that Destination it will be removed, otherwise you will get an exception indicating that you can't remove a Destination with an active consumer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With