Guest User

Untitled

a guest
Jan 21st, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.79 KB | None | 0 0
  1. package org.bsnyder.activemq.jmx;
  2.  
  3. import java.io.IOException;
  4. import java.net.MalformedURLException;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12. import javax.management.AttributeNotFoundException;
  13. import javax.management.InstanceNotFoundException;
  14. import javax.management.IntrospectionException;
  15. import javax.management.MBeanAttributeInfo;
  16. import javax.management.MBeanException;
  17. import javax.management.MBeanInfo;
  18. import javax.management.MBeanServerConnection;
  19. import javax.management.MalformedObjectNameException;
  20. import javax.management.ObjectInstance;
  21. import javax.management.ObjectName;
  22. import javax.management.Query;
  23. import javax.management.QueryExp;
  24. import javax.management.ReflectionException;
  25. import javax.management.remote.JMXConnector;
  26. import javax.management.remote.JMXConnectorFactory;
  27. import javax.management.remote.JMXServiceURL;
  28.  
  29. import junit.framework.Test;
  30. import junit.framework.TestCase;
  31. import junit.framework.TestSuite;
  32.  
  33. import org.apache.commons.logging.Log;
  34. import org.apache.commons.logging.LogFactory;
  35.  
  36. /**
  37. * Programmatic JMX example
  38. * @author bsnyder
  39. */
  40. public class AMQJmxExampleTest extends TestCase {
  41. private static final Log log = LogFactory.getLog(AMQJmxExampleTest.class);
  42.  
  43. public String amqJmxUrl = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi";
  44.  
  45. /**
  46. * Create the test case
  47. *
  48. * @param testName name of the test case
  49. */
  50. public AMQJmxExampleTest(String testName) {
  51. super(testName);
  52. }
  53.  
  54. /**
  55. * @return the suite of tests being tested
  56. */
  57. public static Test suite() {
  58. return new TestSuite(JmxExampleTest.class);
  59. }
  60.  
  61. public void testApp() {
  62. MBeanServerConnection connection;
  63.  
  64. String clientServiceName = "org.apache.activemq:BrokerName=localhost,Type=Broker";
  65.  
  66. String topicName = "Boulder.Colorado";
  67.  
  68. try {
  69. // Acquire a connection to the MBean server
  70. connection = connect();
  71.  
  72. // How many MBeans are running?
  73. count(connection);
  74.  
  75. // Query for a single MBean
  76. query(connection, clientServiceName);
  77.  
  78. // Query all MBeans
  79. query(connection, "");
  80.  
  81. // Check for the topic first
  82. Set mbeans = queryForTopic(connection, topicName);
  83.  
  84. log.info("Located [" + mbeans.size() + "] MBeans");
  85.  
  86. if(mbeans.size() > 0) {
  87. // Create a new topic on the broker
  88. createTopic(connection, topicName);
  89. }
  90. else {
  91. // Remove the topic from the broker and then create it
  92. removeTopic(connection, topicName);
  93. createTopic(connection, topicName);
  94. }
  95.  
  96. mbeans = queryForTopic(connection, topicName);
  97.  
  98. log.info("Located [" + mbeans.size() + "] MBeans");
  99.  
  100. } catch (IOException e) {
  101. // TODO Auto-generated catch block
  102. e.printStackTrace();
  103. }
  104. }
  105.  
  106. public MBeanServerConnection connect()
  107. throws IOException {
  108. JMXConnector connector = null;
  109. MBeanServerConnection connection = null;
  110.  
  111. String username = "";
  112.  
  113. String password = "";
  114.  
  115. Map env = new HashMap();
  116. String[] credentials = new String[] { username, password };
  117. env.put(JMXConnector.CREDENTIALS, credentials);
  118.  
  119. try {
  120. connector = JMXConnectorFactory.newJMXConnector(new JMXServiceURL(amqJmxUrl), env);
  121. connector.connect();
  122. connection = connector.getMBeanServerConnection();
  123. } catch (MalformedURLException e) {
  124. // TODO Auto-generated catch block
  125. e.printStackTrace();
  126. } catch (IOException e) {
  127. // TODO Auto-generated catch block
  128. e.printStackTrace();
  129. }
  130.  
  131. return connection;
  132. }
  133.  
  134. public void count(MBeanServerConnection conn)
  135. throws IOException {
  136. int numberOfMBeans = conn.getMBeanCount().intValue();
  137. log.info("Number of MBeans currently running: " + numberOfMBeans);
  138. }
  139.  
  140. public void query(MBeanServerConnection conn, String query)
  141. throws IOException {
  142. if (conn != null && query != null) {
  143. listMBeans(conn, query);
  144. } else if (conn != null && query.equals("")) {
  145. listAllMBeanNames(conn);
  146. } else {
  147. log.fatal("Unable to connect to ServiceMix");
  148. }
  149. }
  150.  
  151. public void listMBeans(MBeanServerConnection conn, String query)
  152. throws IOException {
  153. ObjectName name;
  154. Set names = null;
  155. try {
  156. name = new ObjectName(query);
  157. names = conn.queryMBeans(name, name);
  158. } catch (MalformedObjectNameException e) {
  159. // TODO Auto-generated catch block
  160. e.printStackTrace();
  161. } catch (NullPointerException e) {
  162. // TODO Auto-generated catch block
  163. e.printStackTrace();
  164. }
  165.  
  166. for(Iterator iter = names.iterator(); iter.hasNext(); ) {
  167. ObjectInstance obj = (ObjectInstance) iter.next();
  168. log.info("+ " + obj.getClassName());
  169. }
  170. }
  171.  
  172. public void listAllMBeanNames(MBeanServerConnection conn)
  173. throws IOException {
  174. Set names = getAllMBeanNames(conn);
  175.  
  176. for (Iterator iter = names.iterator(); iter.hasNext();) {
  177. ObjectName objName = (ObjectName) iter.next();
  178. log.info("+ " + objName);
  179. }
  180. }
  181.  
  182. public void listMBeanAttrs(MBeanServerConnection conn, String query)
  183. throws IOException {
  184. ObjectName objName = null;
  185. try {
  186. objName = new ObjectName(query);
  187. log.info("+ " + objName.getCanonicalName());
  188.  
  189. MBeanInfo info = getMBeanInfo(conn, objName);
  190. MBeanAttributeInfo[] attrs = info.getAttributes();
  191.  
  192. for(int i = 0; i < attrs.length; ++i) {
  193. Object obj = conn.getAttribute(objName, attrs[i].getName());
  194. log.info(" - " + attrs[i].getName() + obj);
  195. }
  196. } catch (MalformedObjectNameException e) {
  197. // TODO Auto-generated catch block
  198. e.printStackTrace();
  199. } catch (NullPointerException e) {
  200. // TODO Auto-generated catch block
  201. e.printStackTrace();
  202. } catch (AttributeNotFoundException e) {
  203. // TODO Auto-generated catch block
  204. e.printStackTrace();
  205. } catch (InstanceNotFoundException e) {
  206. // TODO Auto-generated catch block
  207. e.printStackTrace();
  208. } catch (MBeanException e) {
  209. // TODO Auto-generated catch block
  210. e.printStackTrace();
  211. } catch (ReflectionException e) {
  212. // TODO Auto-generated catch block
  213. e.printStackTrace();
  214. }
  215. }
  216.  
  217. public void createTopic(MBeanServerConnection conn, String topicName)
  218. throws IOException {
  219. String brokerNameQuery = "org.apache.activemq:BrokerName=localhost,Type=Broker";
  220. String addTopicOperationName = "addTopic";
  221. Object[] params = { topicName };
  222. String[] sig = { "java.lang.String" };
  223.  
  224. doTopicCrud(conn, topicName, brokerNameQuery, addTopicOperationName, params, sig, "created");
  225. }
  226.  
  227. private void removeTopic(MBeanServerConnection conn, String topicName)
  228. throws IOException {
  229. String brokerNameQuery = "org.apache.activemq:BrokerName=localhost,Type=Broker";
  230. String removeTopicOperationName = "removeTopic";
  231. Object[] params = { topicName };
  232. String[] sig = { "java.lang.String" };
  233.  
  234. doTopicCrud(conn, topicName, brokerNameQuery, removeTopicOperationName, params, sig, "removed");
  235. }
  236.  
  237. private void doTopicCrud(MBeanServerConnection conn, String topicName,
  238. String brokerNameQuery, String operationName, Object[] params, String[] sig, String verb)
  239. throws IOException {
  240. ObjectName brokerObjName;
  241.  
  242. try {
  243. log.info( verb + "ing new topic: [" + topicName + "]");
  244. brokerObjName = new ObjectName(brokerNameQuery);
  245. conn.invoke(brokerObjName, operationName, params, sig);
  246. log.info("Topic [" + topicName + "] has been " + verb);
  247. } catch (MalformedObjectNameException e) {
  248. // TODO Auto-generated catch block
  249. e.printStackTrace();
  250. } catch (NullPointerException e) {
  251. // TODO Auto-generated catch block
  252. e.printStackTrace();
  253. } catch (InstanceNotFoundException e) {
  254. // TODO Auto-generated catch block
  255. e.printStackTrace();
  256. } catch (MBeanException e) {
  257. // TODO Auto-generated catch block
  258. e.printStackTrace();
  259. } catch (ReflectionException e) {
  260. // TODO Auto-generated catch block
  261. e.printStackTrace();
  262. }
  263. }
  264.  
  265.  
  266.  
  267. private void printMBeans(String topicName, Set mbeans) {
  268. if (!mbeans.isEmpty()) {
  269. for (Iterator iter = mbeans.iterator(); iter.hasNext();) {
  270. ObjectInstance mbean = (ObjectInstance) iter.next();
  271. log.info("+ " + mbean.getClassName());
  272. }
  273. }
  274. else {
  275. log.info("Unable to locate MBean for " + topicName);
  276. }
  277. }
  278.  
  279. public Set queryForTopic(MBeanServerConnection conn, String topicName)
  280. throws IOException {
  281. // Was the topic created?
  282. String topicsQuery = "org.apache.activemq:BrokerName=localhost,Type=Topic,*";
  283. // listMBeans(conn, topicsQuery);
  284.  
  285. // Use JMX query expressions
  286. QueryExp queryExp = Query.eq(Query.attr("name"), Query.value(topicName));
  287.  
  288. ObjectName objName;
  289. Set mbeans = null;
  290. try {
  291. objName = new ObjectName(topicsQuery);
  292. log.info("Querying for " + topicName);
  293. mbeans = conn.queryMBeans(objName, queryExp);
  294. } catch (MalformedObjectNameException e) {
  295. // TODO Auto-generated catch block
  296. e.printStackTrace();
  297. } catch (NullPointerException e) {
  298. // TODO Auto-generated catch block
  299. e.printStackTrace();
  300. }
  301.  
  302. return mbeans;
  303. }
  304.  
  305. public void listAllMBeanAttrs(MBeanServerConnection conn, Set names)
  306. throws IOException {
  307.  
  308. for (Iterator iter = names.iterator(); iter.hasNext();) {
  309. ObjectName objName = (ObjectName) iter.next();
  310. log.info("+ " + objName);
  311.  
  312. MBeanInfo info = getMBeanInfo(conn, objName);
  313.  
  314. MBeanAttributeInfo[] attrs = info.getAttributes();
  315.  
  316. if (attrs == null)
  317. continue;
  318.  
  319. for (int i = 0; i < attrs.length; ++i) {
  320. try {
  321. Object obj = conn.getAttribute(objName, attrs[i].getName());
  322. log.info(" - " + attrs[i].getName() + " = " + obj);
  323. } catch (NullPointerException e) {
  324. // TODO Auto-generated catch block
  325. e.printStackTrace();
  326. } catch (AttributeNotFoundException e) {
  327. // TODO Auto-generated catch block
  328. e.printStackTrace();
  329. } catch (InstanceNotFoundException e) {
  330. // TODO Auto-generated catch block
  331. e.printStackTrace();
  332. } catch (MBeanException e) {
  333. // TODO Auto-generated catch block
  334. e.printStackTrace();
  335. } catch (ReflectionException e) {
  336. // TODO Auto-generated catch block
  337. e.printStackTrace();
  338. }
  339. }
  340. }
  341. }
  342.  
  343. public void query(MBeanServerConnection conn, QueryExp queryExp)
  344. throws IOException {
  345. log.info("Not yet implemented");
  346. }
  347.  
  348. // Private ----------------------------------------------------------------
  349.  
  350. private MBeanInfo getMBeanInfo(MBeanServerConnection conn, ObjectName objName)
  351. throws IOException {
  352. MBeanInfo info = null;
  353.  
  354. try {
  355. info = conn.getMBeanInfo((ObjectName) objName);
  356. } catch (InstanceNotFoundException e1) {
  357. // TODO Auto-generated catch block
  358. e1.printStackTrace();
  359. } catch (IntrospectionException e1) {
  360. // TODO Auto-generated catch block
  361. e1.printStackTrace();
  362. } catch (ReflectionException e1) {
  363. // TODO Auto-generated catch block
  364. e1.printStackTrace();
  365. }
  366.  
  367. return info;
  368. }
  369.  
  370. private Set getAllMBeanNames(MBeanServerConnection conn)
  371. throws IOException {
  372. return conn.queryNames(null, null);
  373. }
  374. }
Add Comment
Please, Sign In to add comment