clojure.java.jmx "0.1" - problem getiing java.lang:type=Threading :AllThreadIds attribute

88 views
Skip to first unread message

zoka

unread,
Feb 22, 2012, 8:47:01 PM2/22/12
to Clojure
I was trying to convert result of JMX attributes query to JSON, and
encountered problem while reading one particular attribute value of
java.lang:type=Threading. Here is the REPL transcript:

demo.server=> (require '[clojure.java.jmx :as jmx])
nil
demo.server=> (jmx/read "java.lang:type=Threading" :AllThreadIds)
#<long[] [J@1bd97d0d>
demo.server=>

This seems to be Java long array reference,

The jmx/mbean function that returns all attribute name as keywords
associated with their values carries this value through. Attempt to
convert such map to JSON string causes exception, since in this case
a clojure vector of longs would be expected instead of Java array.

This particular piece information (list of application ThreadIs) is
not of particular interest to me anyway, so it is easy just to remove
the offending map entry as a workaround.

I have noticed some recent activity in clojure.java.jmx github repo,
so I thought it would be appropriate to rise this issue, since it may
be affecting some other attributes as well.

Regards
Zoka

Pierre-Yves Ritschard

unread,
Feb 23, 2012, 3:08:36 AM2/23/12
to clo...@googlegroups.com
MBeans will let you store serialized java objects, so you can also
find hashmaps, or arbitrary arrays.
When you encounter cases like this one, you can extend
clojure.data.json's functionnality to get the
appropriate behavior (see
http://spootnik.org/blog/2011/08/12/a-bit-of-protocol/ for hints)

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Nick Bailey

unread,
Feb 23, 2012, 11:32:06 AM2/23/12
to clo...@googlegroups.com
I'm not sure that java.jmx should be attempting to do any conversion
of the values returned over jmx. I think it should be the
application's responsibility to convert types appropriately for how
they are needed.

zoka

unread,
Feb 24, 2012, 6:27:53 PM2/24/12
to Clojure
I had a look at the java.jmx code and worked out and tested s simple
fix:

diff --git a/src/main/clojure/clojure/java/jmx.clj b/src/main/clojure/
clojure/java/jmx.clj
index 128e516..3d291a3 100644
--- a/src/main/clojure/clojure/java/jmx.clj
+++ b/src/main/clojure/clojure/java/jmx.clj
@@ -203,9 +203,16 @@
(into-array (map name attrs)))))
(.getAttribute *connection* (as-object-name n) (name attrs))))

+(defn- array-chk
+ "Check if v is Java array and if so, convert it to vector"
+ [v]
+ (if (.isArray (class v))
+ (vec v)
+ v))
+
(def ^{:doc "Read one or more mbean properties."}
read
- (comp objects->data raw-read))
+ (comp array-chk objects->data raw-read))

user=> (require '[clojure.java.jmx :as jmx])
nil
user=> (jmx/read "java.lang:type=Threading" :AllThreadIds)
[11 10 6 3 2 1]

This also makes sure that values returned by read and mbean functions
are immutable.
Reply all
Reply to author
Forward
0 new messages