有可能从Clojure创建可写bean吗?我可以从jconsole管理它吗



我正在探索Clojure java.jmx API参考,并尝试其中提到的示例,例如

;; Can I serve my own beans?  Sure, just drop a Clojure ref
;; into an instance of clojure.java.jmx.Bean, and the bean
;; will expose read-only attributes for every key/value pair
;; in the ref:
(jmx/register-mbean
(create-bean
(ref {:string-attribute "a-string"}))
"my.namespace:name=Value")

它工作得很好,bean的属性值在控制台中可见,但它是只读的。

有没有一种方法可以创建一个可写bean(这样它就列在"Operations"文件夹中,并且可以从控制台进行管理(?

看起来clojure.java.jmx代码支持setAttribute。(请参阅中的(deftype Bean ...)https://github.com/clojure/java.jmx/blob/master/src/main/clojure/clojure/java/jmx.clj

最简单的方法似乎只是使用atom而不是ref

如果JMX对其进行了更改,那么您就可以让原子观察器来运行一些代码。也许试试吧。我已经忘记了JMX的大部分内容;(

编辑:很快尝试了一下。看起来该属性仍然是只读的:(我得再往下看。尽管如此,源代码还是很好的,希望很容易理解。

第2版:问题在于build-attribute-info,它将false传递给`MBeanAttributeInfo中的writeable?标志。

你可以猴子补丁:


(import 'java.jmx.MBeanAttributeInfo)
(require '[clojure.java.jmx :as jmx])
(defn build-my-attribute-info
"Construct an MBeanAttributeInfo. Normally called with a key/value pair from a Clojure map."
([attr-name attr-value]
(build-my-attribute-info
(name attr-name)
(.getName (class attr-value)) ;; you might have to map primitive types here
(name attr-name) true true false)) ;; false -> true compared to orig code
([name type desc readable? writable? is?] (println "Build info:" name type readable? writable?) (MBeanAttributeInfo. name type desc readable? writable? is? )))
;; the evil trick one should only use in tests, maybe
;; overwrite the original definition of build-attribute-info with yours
(with-redefs [jmx/build-attribute-info build-my-attribute-info]
(jmx/register-mbean (jmx/create-bean (atom {:foo "bar"})) "my.bean:name=Foo"))
;; write to it
(jmx/write! "my.bean:name=Foo" "foo" "hello world")
;; read it
(jmx/read "my.bean:name=Foo" "foo")
=> "hello world"

不幸的是,Java任务控制仍然无法更改值,但我不知道为什么。MBean信息正确。一定是许可的事情。

相关内容

最新更新