Samples JDK
FirstTest.java
1 package com.freemindcafe.jmx.sample3;
2 
3 import javax.management.MBeanServerConnection;
4 import javax.management.MBeanServerInvocationHandler;
5 import javax.management.ObjectName;
6 import javax.management.remote.JMXConnector;
7 import javax.management.remote.JMXConnectorFactory;
8 import javax.management.remote.JMXServiceURL;
9 
10 import org.apache.activemq.broker.jmx.BrokerViewMBean;
11 import org.apache.activemq.broker.jmx.QueueViewMBean;
12 import org.junit.Test;
13 
14 public class FirstTest {
15 
16  @Test
17  public void connect_to_activemq_mbean_objects() throws Exception{
18  JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://nikhil-vm.emeter.com:1099/jmxrmi");
19  JMXConnector jmxc = JMXConnectorFactory.connect(url);
20  MBeanServerConnection conn = jmxc.getMBeanServerConnection();
21  ObjectName activeMQ = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost");
22  //ObjectName activeMQ = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Broker");
23  BrokerViewMBean mbean = (BrokerViewMBean) MBeanServerInvocationHandler
24  .newProxyInstance(conn, activeMQ,
25  BrokerViewMBean.class, true);
26  for (ObjectName name : mbean.getQueues()) {
27  QueueViewMBean queueViewMBean = (QueueViewMBean) MBeanServerInvocationHandler
28  .newProxyInstance(conn, name, QueueViewMBean.class,
29  true);
30  try {
31  queueViewMBean.purge();
32  } catch (Exception e) {
33  e.printStackTrace();
34  throw e;
35  }
36  }
37  }
38 
39 }