ActiveMQ delete queue from java

2019-02-14 04:25发布

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

Thanks M.

标签: activemq
5条回答
Emotional °昔
2楼-- · 2019-02-14 04:42

Java by itself does NOT support sessions. So, what exactly do you want to do? Stop your session accessing the queue? Then don't access it and remove (null) the references to it.

查看更多
可以哭但决不认输i
3楼-- · 2019-02-14 04:44

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.

查看更多
Evening l夕情丶
4楼-- · 2019-02-14 04:51

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>
查看更多
\"骚年 ilove
5楼-- · 2019-02-14 04:57
    DefaultHttpClient client = new DefaultHttpClient();

    Config config = Config.getInstance();
    //CONSTRUCT the GET-url to access the queues on the SOI admin site
    HttpGet httpGetQueues = new HttpGet("http://" + config.get(Config.Key.host) + ":" + config.get(Config.Key.port) + "/queue.jsp");  //CHANGE TO VAR THIS IS THE BROKERURL
    String userName = config.get(Config.Key.user);
    String password = config.get(Config.Key.password);
    //add Authentication
    httpGetQueues.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials(userName, password), "UTF-8", false));

    //ACCESS the queues page and SAVE the response  
    HttpResponse httpResponse = client.execute(httpGetQueues);
    HttpEntity entity = httpResponse.getEntity();
    String htmlResponse = EntityUtils.toString(entity);
//System.out.println(htmlResponse);

    //PARSE the response for 'jessioniD'
    String[] parseTemp = htmlResponse.split("css;jsessionid=");
    String temp = parseTemp[1];
    //System.out.println(temp); 
    parseTemp = temp.split("\\)");
    String jsessionID = parseTemp[0];
    System.out.println("JsessionID: " + jsessionID);

    //PARSE the response for 'secret'
    parseTemp = htmlResponse.split("secret=");
    temp = parseTemp[1];
    //System.out.println(temp);     
    parseTemp = temp.split(">");
    temp = parseTemp[0].replace("\"", "");
    parseTemp = temp.split(" ");
    String secret = parseTemp[0];
    System.out.println("Secret: " + secret);
    String consumerQ = config.get(Config.Key.consumerQueue);
    //CONSTRUCT the GET-url to delete the outound queue on the SOI admin site (use 'secret' parsed from response above)
    HttpGet httpDeleteQueue = new HttpGet("http://usatl-s-ssvm08:8161/deleteDestination.action"
            + "?JMSDestination=" + consumerQ
            + "&JMSDestinationType=queue&secret=" + secret);
    //add Authentication
    httpDeleteQueue.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials(userName, password), "UTF-8", false));
    //add cookie using 'jsessionId' parsed from response above
    httpDeleteQueue.addHeader("Cookie", "JSESSIONID=" + jsessionID);

    //Execute deletion url
    client.execute(httpDeleteQueue);
查看更多
欢心
6楼-- · 2019-02-14 04:58

you can do this with JMX via the 'removeQueue' operation...

see this page for a programmatic example:

http://www.consulting-notes.com/2010/08/monitoring-and-managing-activemq-with.html

查看更多
登录 后发表回答