Mercurial > lasercutter
view src/clojure/contrib/jmx.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 ;; Copyright (c) Stuart Halloway, 2009. All rights reserved. The use2 ;; and distribution terms for this software are covered by the Eclipse3 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)4 ;; which can be found in the file epl-v10.html at the root of this5 ;; distribution. By using this software in any fashion, you are6 ;; agreeing to be bound by the terms of this license. You must not7 ;; remove this notice, or any other, from this software.10 (ns ^{:author "Stuart Halloway"11 :doc "JMX support for Clojure13 Requires post-Clojure 1.0 git edge for clojure.test, clojure.backtrace.14 This is prerelease.15 This API will change.16 Send reports to stu@thinkrelevance.com.18 Usage19 (require '[clojure.contrib.jmx :as jmx])21 What beans do I have?23 (jmx/mbean-names \"*:*\")24 -> #<HashSet [java.lang:type=MemoryPool,name=CMS Old Gen,25 java.lang:type=Memory, ...]27 What attributes does a bean have?29 (jmx/attribute-names \"java.lang:type=Memory\")30 -> (:Verbose :ObjectPendingFinalizationCount31 :HeapMemoryUsage :NonHeapMemoryUsage)33 What is the value of an attribute?35 (jmx/read \"java.lang:type=Memory\" :ObjectPendingFinalizationCount)36 -> 038 Can't I just have *all* the attributes in a Clojure map?40 (jmx/mbean \"java.lang:type=Memory\")41 -> {:NonHeapMemoryUsage42 {:used 16674024, :max 138412032, :init 24317952, :committed 24317952},43 :HeapMemoryUsage44 {:used 18619064, :max 85393408, :init 0, :committed 83230720},45 :ObjectPendingFinalizationCount 0,46 :Verbose false}48 Can I find and invoke an operation?50 (jmx/operation-names \"java.lang:type=Memory\")51 -> (:gc)52 (jmx/invoke \"java.lang:type=Memory\" :gc)53 -> nil55 What about some other process? Just run *any* of the above code56 inside a with-connection:58 (jmx/with-connection {:host \"localhost\", :port 3000}59 (jmx/mbean \"java.lang:type=Memory\"))60 -> {:ObjectPendingFinalizationCount 0,61 :HeapMemoryUsage ... etc.}63 Can I serve my own beans? Sure, just drop a Clojure ref64 into an instance of clojure.contrib.jmx.Bean, and the bean65 will expose read-only attributes for every key/value pair66 in the ref:68 (jmx/register-mbean69 (Bean.70 (ref {:string-attribute \"a-string\"}))71 \"my.namespace:name=Value\")"}72 clojure.contrib.jmx73 (:refer-clojure :exclude [read])74 (:use clojure.contrib.def75 [clojure.contrib.string :only [as-str]]76 [clojure.stacktrace :only (root-cause)]77 [clojure.walk :only [postwalk]])78 (:import [clojure.lang Associative]79 java.lang.management.ManagementFactory80 [javax.management Attribute DynamicMBean MBeanInfo ObjectName RuntimeMBeanException MBeanAttributeInfo]81 [javax.management.remote JMXConnectorFactory JMXServiceURL]))83 (defvar *connection* (ManagementFactory/getPlatformMBeanServer)84 "The connection to be used for JMX ops. Defaults to the local process.")86 (load "jmx/data")87 (load "jmx/client")88 (load "jmx/server")90 (defn mbean-names91 "Finds all MBeans matching a name on the current *connection*."92 [n]93 (.queryNames *connection* (as-object-name n) nil))95 (defn attribute-names96 "All attribute names available on an MBean."97 [n]98 (doall (map #(-> % .getName keyword)99 (.getAttributes (mbean-info n)))))101 (defn operation-names102 "All operation names available on an MBean."103 [n]104 (doall (map #(-> % .getName keyword) (operations n))))106 (defn invoke [n op & args]107 (if ( seq args)108 (.invoke *connection* (as-object-name n) (as-str op)109 (into-array args)110 (into-array String (op-param-types n op)))111 (.invoke *connection* (as-object-name n) (as-str op)112 nil nil)))114 (defn mbean115 "Like clojure.core/bean, but for JMX beans. Returns a read-only map of116 a JMX bean's attributes. If an attribute it not supported, value is117 set to the exception thrown."118 [n]119 (into {} (map (fn [attr-name] [(keyword attr-name) (read-supported n attr-name)])120 (attribute-names n))))