# ActiveMq - wie kann ich eine Queue löschen?



## Lenzen (9. Mai 2012)

Wie ist es möglich eine Queue zu löschen. Ich konnte leider nichts finden. 
Zur Info: ActiveMq läuft auf der lokalen Maschine.

Mein Versuch:

Queue löschen * funktioniert nicht * - was muss ich ändern?

```
public static void delete() {

		try {
			BrokerService broker = new BrokerService(); // hier ist der Broker null
			// configure the broker
			// broker.getBroker();

			ObjectName brokerName = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Broker");
			BrokerViewMBean brokerMbean = (BrokerViewMBean) broker.getManagementContext().newProxyInstance(brokerName, BrokerViewMBean.class, true);

			brokerMbean.removeQueue("meineTestQueue");

			// for (ObjectName name : brokerMbean.getQueues()) {
			// QueueViewMBean bean = (QueueViewMBean)
			// broker.getManagementContext().newProxyInstance(name,
			// QueueViewMBean.class, true);
			// bean.purge();
			// }
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
```
Fehler:

```
Exception in thread "main" java.lang.RuntimeException: javax.management.InstanceNotFoundException: org.apache.activemq:BrokerName=localhost,Type=Broker
	at JMSTest.JMSQueuePrioTest.delete(JMSQueuePrioTest.java:186)
	at JMSTest.JMSQueuePrioTest.main(JMSQueuePrioTest.java:193)
Caused by: javax.management.InstanceNotFoundException: org.apache.activemq:BrokerName=localhost,Type=Broker
	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(Unknown Source)
	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(Unknown Source)
	at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(Unknown Source)
	at javax.management.MBeanServerInvocationHandler.invoke(Unknown Source)
	at $Proxy0.removeQueue(Unknown Source)
	at JMSTest.JMSQueuePrioTest.delete(JMSQueuePrioTest.java:179)
	... 1 more
```

#######################################
Zur Info: Nachricht in eine Queue schreiben funktioniert

```
public static void purge(){		

		try{
			
	      JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi");
	      JMXConnector connector = JMXConnectorFactory.connect(url, null);
	      connector.connect();	      
	      MBeanServerConnection connection = connector.getMBeanServerConnection();	      
	      ObjectName name = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Queue,Destination=meineTestQueue");
	      QueueViewMBean queueMbean = (QueueViewMBean) MBeanServerInvocationHandler.newProxyInstance(connection, name, QueueViewMBean.class, true);
		  queueMbean.purge();
		 	      
			} catch (JMSException e) {
				e.printStackTrace();
			} catch (MalformedObjectNameException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NullPointerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
```


----------



## Lenzen (9. Mai 2012)

Natürlich muss zum senden sendTextMessage() verwendet werden
queueMbean.sendTextMessage("Testnachricht");


----------



## Lenzen (9. Mai 2012)

...jetzt habe ich eine Möglichkeit gefunden

```
package teste;

import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.activemq.broker.jmx.BrokerViewMBean;

public class Test1 {

	public static void main(String[] args) throws Exception {

		try {

			JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"),null);
			connector.connect();
			MBeanServerConnection connection = connector.getMBeanServerConnection();

			ObjectName mbeanName = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Broker");
			BrokerViewMBean mbean = (BrokerViewMBean) MBeanServerInvocationHandler.newProxyInstance(connection, mbeanName,BrokerViewMBean.class, true);

			System.out.println("Statistics for broker " + mbean.getBrokerId() + " - " + mbean.getBrokerName());
			System.out.println("\n-----------------\n");
			System.out.println("Total message count: " + mbean.getTotalMessageCount() + "\n");
			System.out.println("Total number of consumers: " + mbean.getTotalConsumerCount());
			System.out.println("Total number of Queues: " + mbean.getQueues().length);

			mbean.removeQueue("meineTestQueue");

		} catch (Exception x) {
			x.printStackTrace();
		}

	}

}
```


----------

