|
76 | 76 | (:import [clojure.lang Associative]
|
77 | 77 | java.lang.management.ManagementFactory
|
78 | 78 | [javax.management Attribute AttributeList DynamicMBean MBeanInfo
|
79 |
| - ObjectName RuntimeMBeanException MBeanAttributeInfo] |
| 79 | + ObjectName RuntimeMBeanException MBeanAttributeInfo MBeanServerConnection] |
80 | 80 | [javax.management.remote JMXConnectorFactory JMXServiceURL]))
|
81 | 81 |
|
82 | 82 | (def ^{:dynamic true
|
|
235 | 235 | "Write an attribute value."
|
236 | 236 | [n attr value]
|
237 | 237 | (.setAttribute
|
238 |
| - *connection* |
239 |
| - (as-object-name n) |
240 |
| - (Attribute. (name attr) value))) |
| 238 | + ^MBeanServerConnection *connection* |
| 239 | + ^ObjectName (as-object-name n) |
| 240 | + (Attribute. ^String (name attr) ^String value))) |
241 | 241 |
|
242 | 242 | (defn ^{:skip-wiki true} attribute-info
|
243 | 243 | "Get the MBeanAttributeInfo for an attribute."
|
|
333 | 333 | (let [result (AttributeList.)]
|
334 | 334 | (doseq [attr attrs]
|
335 | 335 | (.add result (Attribute. attr (.getAttribute _ attr))))
|
336 |
| - result))) |
| 336 | + result)) |
| 337 | + (setAttribute [_ attr] |
| 338 | + (let [attr-name (.getName attr) |
| 339 | + attr-value (.getValue attr) |
| 340 | + state-update {(keyword attr-name) attr-value}] |
| 341 | + (condp = (type state-ref) |
| 342 | + clojure.lang.Agent |
| 343 | + (await (send state-ref (fn [state state-update] (merge state state-update)) state-update)) |
| 344 | + |
| 345 | + clojure.lang.Atom |
| 346 | + (swap! state-ref merge state-update) |
| 347 | + |
| 348 | + clojure.lang.Ref |
| 349 | + (dosync |
| 350 | + (ref-set state-ref |
| 351 | + (merge @state-ref state-update)))))) |
| 352 | + (setAttributes [_ attrs] |
| 353 | + (let [attr-names (map (fn [attr] |
| 354 | + (.setAttribute _ attr) |
| 355 | + (.getName attr)) |
| 356 | + attrs)] |
| 357 | + (.getAttributes _ (into-array attr-names))))) |
| 358 | + |
337 | 359 |
|
338 | 360 | (defn create-bean
|
339 | 361 | "Expose a reference as a JMX bean. state-ref should be a Clojure
|
340 |
| - reference (ref, atom, agent) containing a map." |
| 362 | + reference (ref, atom, agent) containing a map. |
| 363 | +
|
| 364 | + Using an agent for the state-ref is not recommended when the bean may |
| 365 | + be modified with the setAttribute(s) methods. The setAttribute(s) methods |
| 366 | + will block on the agent to complete all submitted actions (via await)." |
341 | 367 | [state-ref]
|
342 | 368 | (Bean. state-ref))
|
0 commit comments