【问题标题】:ActiveMQ delete queue from javaActiveMQ 从 java 中删除队列
【发布时间】:2012-08-17 06:14:21
【问题描述】:

如何从 java 程序中删除 activemq 中的队列?有没有类似 session.deleteQueue() 的东西?

谢谢 M.

【问题讨论】:

标签: activemq


【解决方案1】:

不使用 JMX 将连接转换为 ActiveMQConnection 并使用其 destroyDestination() 方法的简单解决方案。 使用该方法的简单实用程序:

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);
        }
     }
   }
  }
}

Maven 的依赖

    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-core</artifactId>
        <version>5.7.0</version>
    </dependency>

【讨论】:

    【解决方案2】:

    如果您不介意使用非 JMS API 调用,那么您可以将 Connection 对象转换为 ActiveMQConnection 并调用 destroyDestination,将您要删除的目标实例传递给它。如果该 Destination 上没有活动的消费者,它将被删除,否则您将收到一个异常,指示您无法删除具有活动消费者的 Destination。

    【讨论】:

      【解决方案3】:

      您可以通过“removeQueue”操作使用 JMX 执行此操作...

      有关程序示例,请参见此页面:

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

      【讨论】:

        【解决方案4】:
            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);
        

        【讨论】:

          【解决方案5】:

          Java 本身不支持会话。那么,你到底想做什么?停止访问队列的会话?然后不要访问它并删除(null)对它的引用。

          【讨论】:

          • 感谢您的回复。实际上我想要的是:我创建了一个队列:Queue destination = session.createQueue("ActiveMQ.DLQ");现在我想删除它(比如从控制台删除一个队列,但是从 java 代码中删除)。感谢您的帮助。
          猜你喜欢
          • 2021-03-08
          • 2018-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-17
          • 1970-01-01
          • 2013-11-22
          相关资源
          最近更新 更多