Debian Codegraph @

src/docbkx/jmx.xml

raw
1 <?xml version="1.0" encoding="utf-8"?>
2
3 <chapter id="jmx">
4 <title>Jolokia JMX</title>
5 <para>
6 The main focus of Jolokia is to allow easy access to JMX MBeans
7 from everywhere. MBeans can be provided by the JVM itself, by an
8 application server or an application itself, where each MBean is
9 registered at a specific MBeanServer. Multiple MBeanServers can
10 co-exist in a single JVM. The so called
11 <emphasis>PlatformMBeanServer</emphasis> is always present and is
12 created by the JVM during startup. Especially application servers
13 often create additional MBeanServers for various purposes. When
14 accessing an MBean remotely via JSR-160, the MBeanServer holding
15 the requested MBean must be known before. Jolokia instead
16 <emphasis>merges</emphasis> all MBeanServers it can find to give a
17 single view on all MBeans. The merging algorithm is described in
18 <xref linkend="mbeanserver-merging"/>.
19 </para>
20 <para>
21 For application specific MBeans, Jolokia provides an own, so
22 called <emphasis>Jolokia MBeanServer</emphasis> which is treated
23 specially by the Jolokia agent. The Jolokia MBeanServer and its
24 features are explained in <xref linkend="jolokia-mbeanserver"/>.
25 </para>
26 <para>
27 Developing application specific MBeans is easy, especially if
28 <ulink
29 url="http://docs.oracle.com/javase/tutorial/jmx/mbeans/standard.html">Standard
30 MBeans</ulink> are used. However, for Spring users there is even a
31 easier, more <ulink
32 url="http://static.springsource.org/spring/docs/3.2.1.RELEASE/spring-framework-reference/html/jmx.html">declarative
33 way</ulink> for turning POJOs into MBeans. On top of this Jolokia
34 provides an easy, declarative way for firing up a Jolokia JVM
35 agent merely by including some Jolokia specific Spring
36 configuration. This is described in <xref linkend="jvm-spring"/>.
37 </para>
38
39 <section id="jolokia-mbeanserver">
40 <title>Jolokia MBeanServer</title>
41 <sidebar>
42 <title>JBoss 7 Gotcha</title>
43 For JBoss 7 there is a slight issue when creating a new MBeanServer. For this to work,
44 a <filename>jboss-deployment-structure</filename> with a dependency on
45 <constant>org.jboss.as.jmx</constant> must be added. For an example see the
46 <ulink url="https://github.com/rhuss/jolokia/blob/master/it/war/src/main/webapp/WEB-INF/jboss-deployment-structure.xml">integration test war</ulink>, the location
47 to where to put this file is explained in the <ulink url="https://docs.jboss.org/author/display/AS7/Developer+Guide#DeveloperGuide-JBossDeploymentStructureFile">JBoss documentation</ulink>
48 </sidebar>
49 <para>
50 The Jolokia MBeanServer can be easily created and used with a locator:
51 </para>
52 <programlisting language="java"><![CDATA[
53 MBeanServer jolokiaServer = JolokiaMBeanServerUtil.getJolokiaMBeanServer();
54 ]]></programlisting>
55 <para>
56 This server is treated specially by a Jolokia Agent:
57 </para>
58 <itemizedlist>
59 <listitem>
60 Every MBean registered at the Jolokia MBeanServer will never
61 show up remotely via JSR-160. The Jolokia MBeanServer is never
62 exposed over JSR-160.
63 </listitem>
64 <listitem>
65 Each Jolokia MBeanServer registered MBean will shadow any
66 MBean with the same ObjectName in any other MBeanServer
67 present. See below for more details.
68 </listitem>
69 <listitem>
70 The Jolokia MBeanServer is also responsible for managing so
71 called <emphasis role="bold">JSON MBeans</emphasis>. These are MBeans
72 annotated with <classname>@JsonMBean</classname> on the class
73 level. JSON MBean are explained in <xref
74 linkend="json-mbean"/>
75 </listitem>
76 </itemizedlist>
77 <section id="mbeanserver-merging">
78 <title>MBeanServer merging</title>
79 <para>
80 Jolokia tries hard to detect as many MBeanServer as available
81 in a JVM. Beside the always present
82 <classname>PlatformMBeanServer</classname> many
83 application servers create own MBeanServer which not always
84 can be found with standard mechanisms. Therefore Jolokia comes
85 with so called <classname>ServerDetector</classname>s for many
86 known brands of applications server. These server detectors
87 know how to find MBeanServer by application server specific
88 means.
89 </para>
90 <para>
91 The set of available of MBeanServers is detected during
92 startup and kept, except for the Jolokia MBeanServer which can
93 kick in and out at any time. For Jolokia operations, all these
94 MBeanServers are tried according the order given below.
95 </para>
96 <itemizedlist>
97 <listitem>
98 The <emphasis role="bold">Jolokia MBeanServer</emphasis> is queried first,
99 if available.
100 </listitem>
101 <listitem>
102 Next every MBeanServer as detected by the <emphasis role="bold">server
103 detectors</emphasis> a queried in turn.
104 </listitem>
105 <listitem>
106 All MBeanServers returned by
107 <code>MBeanServerFactory.findMBeanServer(null)</code> are
108 called if not already tried previously.
109 </listitem>
110 <listitem>
111 Finally, the
112 <code>ManagementFactory.getPlatformMBeanServer()</code> is
113 used (also, if not found in a former step).
114 </listitem>
115 </itemizedlist>
116
117 <para>
118 All MBeans contained in all detected MBeanServers are merged
119 to give a single view on the set of available MBeans.
120 For MBeans registered with the same name at different
121 MBeanServers, MBeans registered in later MBeanServers are not
122 visible. These hidden MBeans will never be called on
123 <constant>READ</constant>, <constant>WRITE</constant> or
124 <constant>EXEC</constant> operations. Also, for
125 <constant>LIST</constant> operations only the meta data of the
126 visible MBeans is returned.
127 </para>
128
129 <para>
130 This hiding mechanism is used by
131 <constant>@JsonMBean</constant> to provide a different view of
132 an MBean for JSR-160 connectors (see below).
133 </para>
134 </section>
135 </section>
136
137 <section id="json-mbean">
138 <title>@JsonMBean</title>
139
140 <para>
141 JMX 1.4 introduced <ulink
142 url="http://docs.oracle.com/javase/6/docs/api/javax/management/MXBean.html">MXBeans</ulink>
143 which allows for nearly arbitrary data to be translated into so
144 called OpenData which are accessible via JMX. For example,
145 arbitrary Java Beans are translated into a <ulink
146 url="http://docs.oracle.com/javase/6/docs/api/javax/management/openmbean/CompositeData.html"><classname>CompositeData</classname></ulink>
147 structure with property names as keys and their values in
148 OpenData values.
149 </para>
150
151 <para>
152 Jolokia provides an annotation <classname>@JsonMBean</classname>
153 for marking an MBean as a JSON MBean. Such an MBean, if
154 registered at the <emphasis>Jolokia MBeanServer</emphasis>
155 creates a proxy on the <emphasis>PlatformMBeanServer</emphasis>
156 where every complex value gets translated into plain strings in
157 JSON format. This is true for attributes, operation return
158 values and arguments. That way, a JSR-160 based console (like
159 <command>jconsole</command>) can easily access complex data
160 type exposed by custom MBeans. Json MBeans work for Java 6 and
161 newer.
162 </para>
163
164 <figure id="fig-jconsole-json-mbean">
165 <title>A JsonMBean in jconsole</title>
166 <mediaobject>
167 <imageobject>
168 <imagedata fileref="jconsole-json-mbean.png"/>
169 </imageobject>
170 </mediaobject>
171 </figure>
172
173 <para>
174 JsonMBean and MXBean are quite similar as both do a translation
175 from complex data types to a standard format (OpenType for
176 MXBeans, JSON strings for JsonMBean). However, there are also
177 differences:
178 </para>
179 <itemizedlist>
180 <listitem>
181 MXBeans are a standard mechanism which are available on every
182 JVM since 1.5.<footnote id="jbos-mxbean">
183 <para>
184 For JBoss prior to version 7 there are some slight issues
185 since JBoss used to replace the standard MBeanServer with an
186 own variant. See this <ulink
187 url="https://community.jboss.org/thread/167796">discussion</ulink>
188 for details.
189 </para></footnote>
190 </listitem>
191 <listitem>
192 Serialisation of complex Java Beans is more powerful with
193 JsonMBeans, e.g. Jolokia can detect self (or cyclic) object
194 references. MXBeans will cause an error in this case.
195 </listitem>
196 <listitem>
197 JsonMBeans must be added to the Jolokia MBeanServer to
198 work. MXBeans work with the PlatformMBeanServer, too.
199 </listitem>
200 <listitem>
201 JsonMBean work also with JMX support libraries which use
202 <classname>ModelMBean</classname>s unde the hood. E.g. <ulink
203 url="http://static.springsource.org/spring/docs/3.0.x/reference/jmx.html">Spring
204 JMX</ulink> uses a ModelMBean for
205 <classname>@ManagedResource</classname> annotated
206 MBeans. <classname>@JsonMBean</classname> can be easily added,
207 whereas <classname>@MXBean</classname> wouldn't work here.
208 </listitem>
209 </itemizedlist>
210
211 <para>
212 The Jolokia MBeanServer and the
213 <classname>@JsonMBean</classname> annotation are contained in the
214 Maven module <constant>jolokia-jmx</constant>.
215 </para>
216 </section>
217
218 <section id="jvm-spring">
219 <title>Spring Support</title>
220 <para>
221 A Jolokia agent can be easily integrated into a Spring application
222 context. A dedicated artifact
223 <constant>jolokia-spring</constant> can be used, which
224 comes with a custom Spring configuration syntax.
225 </para>
226 <para>
227 For Maven based projects, a simple dependency declaration is
228 sufficient:
229 </para>
230 <programlisting language="xml"><![CDATA[
231 <dependency>
232 <groupId>org.jolokia</groupId>
233 <artifactId>jolokia-spring</artifactId>
234 <version>1.1.0</version>
235 </dependency>
236 ]]></programlisting>
237 <section id="jvm-spring-agent">
238 <title>JVM agent</title>
239 <para>
240 With this in place, the following configuration can be used to
241 fire up a Jolokia JVM based agent using the HTTP server which
242 comes with OpenJDK/Oracle JVMs (Version 6 or later).
243 </para>
244 <programlisting language="xml"><![CDATA[
245 <beans xmlns="http://www.springframework.org/schema/beans"
246 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
247 xmlns:jolokia="http://www.jolokia.org/jolokia-spring/schema/config"
248 xsi:schemaLocation="
249 http://www.springframework.org/schema/beans
250 http://www.springframework.org/schema/beans/spring-beans.xsd
251 http://www.jolokia.org/jolokia-spring/schema/config
252 http://www.jolokia.org/jolokia-spring/schema/config/jolokia-config.xsd
253 ">
254
255 <jolokia:agent lookupConfig="false" systemPropertiesMode="never">
256 <jolokia:config
257 autoStart="true"
258 host="0.0.0.0"
259 port="8778"
260 ....
261 />
262 </jolokia:agent>
263 </beans>
264 ]]></programlisting>
265 <para>
266 There are two directives available:
267 <constant>&lt;jolokia:agent&gt;</constant> declares a Jolokia
268 server with a configuration as defined in an embedded
269 <constant>&lt;jolokia:config&gt;</constant> configuration
270 section.<sidebar>
271 <title>IDE support</title>
272 With a decent IDE like IntelliJ IDEA you get completion support
273 on the configuration attributes so it can be easily determined
274 which configuration options are available. Even better, there is also
275 some documentation for each attribute (e.g. by using "Quick
276 documentation" with ^Q in IDEA with).
277 </sidebar>
278 </para>
279 <para>
280 <constant>&lt;jolokia:agent&gt;</constant> has an attribute
281 <constant>lookupConfig</constant>. If set to
282 <constant>true</constant>, externally defined
283 <constant>&lt;jolokia:config&gt;</constant> sections will be
284 looked up, too and merged with the embedded configuration. A
285 <constant>&lt;jolokia:config&gt;</constant> has an
286 <constant>order</constant> attribute, which determines the
287 config merge order: The higher order configs will be merged
288 later and hence will override conflicting parameters. By
289 default, external config lookup is disabled.
290 </para>
291 <para>
292 The attribute <constant>systemPropertiesMode</constant>
293 determines, how system properties with a prefix
294 <constant>jolokia.</constant> can be used as configuration
295 values. There are three modes available:
296 </para>
297 <table id="jvm-spring-system-properties">
298 <title>System properties modes</title>
299 <thead>
300 <tr>
301 <td>Mode</td>
302 <td>Description</td>
303 </tr>
304 </thead>
305 <tr>
306 <td><literal>never</literal></td>
307 <td>
308 No lookup is done on system properties as all. This is the
309 default mode.
310 </td>
311 </tr>
312 <tr>
313 <td><literal>fallback</literal></td>
314 <td>
315 System properties with a prefix
316 <constant>jolokia.</constant> are used as fallback
317 configuration values if not specified locally in the
318 Spring application
319 context. E.g. <constant>jolokia.port=8888</constant> will
320 change the port on which the agent is listening to 8888 if
321 the port is not explicitly specified in the
322 configuration.
323 </td>
324 </tr>
325 <tr>
326 <td><literal>override</literal></td>
327 <td>
328 System properties with a prefix
329 <constant>jolokia.</constant> are used as configuration
330 values even if they are specified locally in the Spring
331 application
332 context. E.g. <constant>jolokia.port=8888</constant> will
333 change the port on which the agent is listening to 8888 in
334 any case.
335 </td>
336 </tr>
337 </table>
338 <para>
339 <constant>&lt;jolokia:config&gt;</constant> takes as attributes all
340 the configuration parameters for the JVM agent as described in
341 <xref linkend="agent-jvm-config"/>. In addition, the is an extra
342 attribute <constant>autoStart</constant> which allows for
343 automatically starting the HTTP server during the initialization
344 of the application context. By default this is set to
345 <constant>true</constant>, so the server starts up automatically
346 by default.
347 </para>
348 <para>
349 Just in case you don't want to use the Jolokia Spring namespace you can also
350 use plain beans to configure a JVM agent. The following examples shows the example above with
351 only base Spring bean configurations (including an Spring EL expression) :
352 </para>
353 <programlisting><![CDATA[
354 <beans xmlns="http://www.springframework.org/schema/beans"
355 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
356 xmlns:context="http://www.springframework.org/schema/context"
357 xmlns:util="http://www.springframework.org/schema/util"
358 xsi:schemaLocation="
359 http://www.springframework.org/schema/beans
360 http://www.springframework.org/schema/beans/spring-beans.xsd
361 http://www.springframework.org/schema/context
362 http://www.springframework.org/schema/context/spring-context.xsd
363 http://www.springframework.org/schema/util
364 http://www.springframework.org/schema/util/spring-util.xsd">
365
366 <bean name="server" id="jolokia" class="org.jolokia.jvmagent.spring.SpringJolokiaAgent">
367 <property name="lookupConfig" value="false"/>
368 <property name="systemPropertiesMode" value="never"/>
369 <property name="config">
370 <bean class="org.jolokia.jvmagent.spring.SpringJolokiaConfigHolder">
371 <property name="config">
372 <util:map>
373 <entry key="autoStart" value="true"/>
374 <entry key="host" value="0.0.0.0"/>
375 <entry key="port" value="#{configuration['jmx.jolokiaPort']}"/>
376 ...
377 </util:map>
378 </property>
379 </bean>
380 </property>
381 </bean>
382
383 </beans>
384 ]]></programlisting>
385 <para>
386 This style however is only recommended if there are some issues with the Jolokia spring configuration setup
387 (like using Spring EL expressions in Jolokia versions earlier than 1.2.4). Otherwise, the Jolokia configuration
388 namespace is much easier to read.
389 </para>
390 </section>
391 <section id="jvm-spring-mbean-server">
392 <title>Jolokia MBeanServer</title>
393 <para>
394 With <constant>&lt;jolokia:mbean-server&gt;</constant> the Jolokia
395 MBeanServer can be specified. This is especially useful for
396 adding it to <constant>&lt;context:mbean-export&gt;</constant>
397 so that this MBeanServer is used for registering
398 <constant>@ManagedResource</constant> and
399 <constant>@JsonMBean</constant>. Remember, MBean registered at
400 the Jolokia MBeanServer never will show up in an JSR-160 client
401 except when annotated with <constant>@JsonMBean</constant>.
402 </para>
403 <programlisting><![CDATA[
404 <beans xmlns="http://www.springframework.org/schema/beans"
405 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
406 xmlns:context="http://www.springframework.org/schema/context"
407 xmlns:jolokia="http://www.jolokia.org/jolokia-spring/schema/config"
408 xsi:schemaLocation="
409 http://www.springframework.org/schema/beans
410 http://www.springframework.org/schema/beans/spring-beans.xsd
411 http://www.jolokia.org/jolokia-spring/schema/config
412 http://www.jolokia.org/jolokia-spring/schema/config/jolokia-config.xsd
413 http://www.springframework.org/schema/context
414 http://www.springframework.org/schema/context/spring-context.xsd
415 ">
416
417 <context:mbean-export server="jolokiaServer"/>
418 <jolokia:mbean-server id="jolokiaServer"/>
419
420 </beans>
421 ]]></programlisting>
422
423 </section>
424
425 <section id="jvm-spring-plugin">
426 <title>Jolokia Spring plugin</title>
427 <para>
428 There is an even simpler way to startup a Jolokia JVM agent
429 with a default setup if you use a variant of the
430 <constant>jolokia-spring</constant> module with the classifier
431 <constant>plugin</constant>. This artefact contains a
432 predefined Spring configuration for starting up Jolokia with
433 default values automatically:
434 </para>
435 <programlisting language="xml"><![CDATA[
436 <dependency>
437 <groupId>org.jolokia</groupId>
438 <artifactId>jolokia-spring</artifactId>
439 <classifier>plugin</classifier>
440 <version>1.1.0</version>
441 </dependency>
442 ]]></programlisting>
443 <para>
444 Beside putting this jar into the classpath (along with its
445 dependencies) the only requirement is, that the Spring
446 application context needs to pickup
447 <filename>classpath:META-INF/spring/jolokia.xml</filename>. Luckily,
448 many Spring based containers like the <ulink
449 url="http://camel.apache.org/camel-maven-plugin.html">Camel
450 Maven Plugin</ulink> do this automatically for you, nothing
451 has to be configured here. Otherwise this
452 application context path has to be added manually, but in this case it is
453 probably easier to use the non-plugin version (without
454 classifier) and declare the Jolokia server explicitly in an
455 existing Spring configuration file as described above.
456 </para>
457 <para>
458 By default, the Jolokia agent starts on port 8778 on every
459 IP-Address of the host <emphasis>without</emphasis>
460 security.
461 </para>
462 <para>
463 The configuration can be tweaked via system properties as
464 described in <xref
465 linkend="jvm-spring-system-properties"/>. I.e. the plugin
466 doesn't specify any configuration on its own and uses a
467 <constant>systemPropertiesMode</constant> of
468 "<constant>fallback</constant>".
469 </para>
470 <para>
471 As an alternative, the default settings can be customized by
472 providing a standalone
473 <constant>&lt;jolokia:config&gt;</constant> somewhere in the
474 Spring application context. An <constant>order</constant>
475 attribute can be used if multiple config declarations are
476 present: the higher the order, the higher the priority. But
477 then again, instead of using the plugin with an external
478 configuration it is probably better to us an explicite
479 <constant>&lt;jolokia:agent&gt;</constant> declaration, since
480 you have to add to a Spring configuration file anyway.
481 </para>
482 </section>
483 </section>
484 </chapter>