Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveMQ delete queue from java

Tags:

activemq

How could I delete a queue in activemq from java program? Is there anything like session.delelteQueue()?

Thanks M.

like image 742
user1615276 Avatar asked Aug 21 '12 20:08

user1615276


2 Answers

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>
like image 105
JosefN Avatar answered Jan 02 '23 21:01

JosefN


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.

like image 45
Tim Bish Avatar answered Jan 02 '23 21:01

Tim Bish