ActiveMQ从java删除队列

use*_*276 5 activemq-classic

我怎么能从java程序中删除activemq中的队列?有没有像session.delelteQueue()?

谢谢M.

Jos*_*efN 7

简单的解决方案,它不使用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);
        }
     }
   }
  }
}
Run Code Online (Sandbox Code Playgroud)

对Maven的依赖

    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-core</artifactId>
        <version>5.7.0</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)


Tim*_*ish 5

如果您不介意使用非JMS API调用,则可以将Connection对象强制转换为ActiveMQConnection并调用destroyDestination,并将其传递给您要删除的目标实例.如果该目的地上没有活动消费者,则会将其删除,否则您将收到一个异常,表明您无法删除具有活动消费者的目的地.


Ger*_*ton -1

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