Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow setting attributes on beans created with java.jmx.

Patch for JMX-16 by Michael Nussbaum.
  • Loading branch information...
commit 546139c5ed86b040a1d5b6904fbd0091de40b931 1 parent 1a595d6
@nickmbailey nickmbailey authored
View
38 src/main/clojure/clojure/java/jmx.clj
@@ -76,7 +76,7 @@
(:import [clojure.lang Associative]
java.lang.management.ManagementFactory
[javax.management Attribute AttributeList DynamicMBean MBeanInfo
- ObjectName RuntimeMBeanException MBeanAttributeInfo]
+ ObjectName RuntimeMBeanException MBeanAttributeInfo MBeanServerConnection]
[javax.management.remote JMXConnectorFactory JMXServiceURL]))
(def ^{:dynamic true
@@ -235,9 +235,9 @@
"Write an attribute value."
[n attr value]
(.setAttribute
- *connection*
- (as-object-name n)
- (Attribute. (name attr) value)))
+ ^MBeanServerConnection *connection*
+ ^ObjectName (as-object-name n)
+ (Attribute. ^String (name attr) ^String value)))
(defn ^{:skip-wiki true} attribute-info
"Get the MBeanAttributeInfo for an attribute."
@@ -333,10 +333,36 @@
(let [result (AttributeList.)]
(doseq [attr attrs]
(.add result (Attribute. attr (.getAttribute _ attr))))
- result)))
+ result))
+ (setAttribute [_ attr]
+ (let [attr-name (.getName attr)
+ attr-value (.getValue attr)
+ state-update {(keyword attr-name) attr-value}]
+ (condp = (type state-ref)
+ clojure.lang.Agent
+ (await (send state-ref (fn [state state-update] (merge state state-update)) state-update))
+
+ clojure.lang.Atom
+ (swap! state-ref merge state-update)
+
+ clojure.lang.Ref
+ (dosync
+ (ref-set state-ref
+ (merge @state-ref state-update))))))
+ (setAttributes [_ attrs]
+ (let [attr-names (map (fn [attr]
+ (.setAttribute _ attr)
+ (.getName attr))
+ attrs)]
+ (.getAttributes _ (into-array attr-names)))))
+
(defn create-bean
"Expose a reference as a JMX bean. state-ref should be a Clojure
- reference (ref, atom, agent) containing a map."
+ reference (ref, atom, agent) containing a map.
+
+ Using an agent for the state-ref is not recommended when the bean may
+ be modified with the setAttribute(s) methods. The setAttribute(s) methods
+ will block on the agent to complete all submitted actions (via await)."
[state-ref]
(Bean. state-ref))
View
21 src/test/clojure/clojure/java/test_jmx.clj
@@ -175,6 +175,27 @@
1 (.getAttribute bean "a")
2 (.getAttribute bean "b"))))))
+(deftest test-setAttribute
+ (doseq [reftype [ref atom agent]]
+ (let [state (reftype {:a 1 :b 2})
+ bean (jmx/create-bean state)]
+ (testing (str "setting values on a " (class state))
+ (.setAttribute bean (Attribute. "a" 3))
+ (are [result expr] (= result expr)
+ 3 (.getAttribute bean "a")
+ 2 (.getAttribute bean "b"))))))
+
+(deftest test-setAttributes
+ (doseq [reftype [ref atom agent]]
+ (let [state (reftype {:r 5 :d 4})
+ bean (jmx/create-bean state)
+ atts (.setAttributes bean (AttributeList. [(Attribute. "r" 6)
+ (Attribute. "d" 5)]))]
+ (are [x y] (= x y)
+ AttributeList (class atts)
+ ["r" "d"] (map (memfn getName) (seq atts))
+ [6 5] (map (memfn getValue) (seq atts))))))
+
(deftest test-bean-info
(let [state (ref {:a 1 :b 2})
bean (jmx/create-bean state)
Please sign in to comment.
Something went wrong with that request. Please try again.