Debian Codegraph @

src/docbkx/client/java.xml

raw
1 <?xml version="1.0" encoding="utf-8"?>
2 <!--
3 ~ Copyright 2009-2013 Roland Huss
4 ~
5 ~ Licensed under the Apache License, Version 2.0 (the "License");
6 ~ you may not use this file except in compliance with the License.
7 ~ You may obtain a copy of the License at
8 ~
9 ~ http://www.apache.org/licenses/LICENSE-2.0
10 ~
11 ~ Unless required by applicable law or agreed to in writing, software
12 ~ distributed under the License is distributed on an "AS IS" BASIS,
13 ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ~ See the License for the specific language governing permissions and
15 ~ limitations under the License.
16 -->
17
18 <section id="client-java">
19 <title>Java Client Library</title>
20 <para>
21 The Java client library provides an easy access to the Jolokia
22 agent from within Java. Since JSR-160 connectors themselves
23 provide Java based remote access to MBeans one might wonder about
24 the benefits of a Jolokia Java binding. There are several,
25 though:
26 </para>
27 <itemizedlist>
28 <listitem>
29 It provides a typeless access to remote MBeans. The big
30 advantage is that for any non-OpenMBean access to custom typed
31 objects is still possible without having the type information
32 locally in the classpath.
33 </listitem>
34 <listitem>
35 Jolokia can be used in setups where JSR-160 connectors can not
36 be used. I.e. in firewall secured environments it is much easier
37 to get through to a Jolokia Agent than to an JSR-160 connector
38 using RMI as transport protocol.
39 </listitem>
40 <listitem>
41 <emphasis>Remoteness</emphasis> is explicit in this API instead
42 of JSR-160 connector's seeked <emphasis>transparent
43 remoteness</emphasis>. RMI has some arguable conceptually
44 advantages, but hiding all remote aspects proved to have quite
45 some disadvantages when it comes to the programming
46 model. Explicite awareness of a 'heavy-weight' remote call is
47 better than false transparency in order to know the price tag.
48 </listitem>
49 </itemizedlist>
50 <para>
51 The Java client library follows a strict request-response
52 paradigm, much like the underlying HTTP. It uses generics heavily
53 and can be centered around three classes:
54 <classname>J4pClient</classname> is the client side agent, which
55 has various variants of a <methodname>execute()</methodname> for
56 sending requests. This method takes one or more
57 <classname>J4pRequest</classname> objects as arguments and returns
58 one or more <classname>J4pResponse</classname> objects as result.
59 </para>
60 <sidebar>
61 <title>What the heck is this 'J4p' ?</title>
62 That's a reminiscence to Jolokia's roots which lies in <ulink
63 url="http://www.jmx4perl.org">Jmx4Perl</ulink>. It is always good
64 to remember where one comes from ;-)
65 </sidebar>
66 <para>
67 But before we got into the details, the next section gives a first
68 tutorial to get a feeling how the API can be used.
69 </para>
70
71 <section id="client-java-tutorial">
72 <title>Tutorial</title>
73 <para>
74 As seen in the following example, the usage is quite easy. First
75 a, client object <varname>client</varname> is created pointing
76 to a Jolokia agent at <uri>http://localhost:8080/jolokia</uri>.
77 A read request for querying the heap memory usage from the
78 <classname>MemoryMXBean</classname> is created and then send via
79 the <methodname>execute()</methodname> to the agent. The
80 response returned is of type
81 <classname>J4pReadResponse</classname> and holds the result
82 which finally is printed out to standard output.
83 </para>
84 <programlisting language="java"><![CDATA[
85 import org.jolokia.client.J4pClient;
86 import org.jolokia.client.request.*;
87
88 public class MemoryDemo {
89 public static void main(String[] args) {
90 J4pClient client = new J4pClient("http://localhost:8080/jolokia");
91 J4pReadRequest request =
92 new J4pReadRequest("java.lang:type=Memory","HeapMemoryUsage");
93 request.setPath("used");
94 J4pReadResponse response = client.execute(request);
95 System.out.println("Memory used: " + response.getValue());
96 }
97 }
98 ]]></programlisting>
99 <para>
100 In order to compile and run this sample, two support libraries
101 are required in addition to
102 <filename>jolokia-client-java.jar</filename> (<ulink url="../../download.html">Download</ulink>):
103 </para>
104 <itemizedlist>
105 <listitem>
106 Apache <ulink
107 url="http://hc.apache.org/httpcomponents-client-ga/index.html">HttpClient</ulink>,
108 4.3.3 (<ulink url="http://hc.apache.org/downloads.cgi">Download</ulink>)
109 </listitem>
110 <listitem>
111 <ulink
112 url="http://code.google.com/p/json-simple/">json-simple</ulink>,
113 1.1 (<ulink url="http://code.google.com/p/json-simple/downloads/list">Download</ulink>)
114 </listitem>
115 </itemizedlist>
116 <para>
117 For maven users, the following dependency is sufficient (it will include
118 the other two as transitive dependencies):
119 </para>
120 <programlisting language="xml"><![CDATA[
121 <dependency>
122 <groupId>org.jolokia</groupId>
123 <artifactId>jolokia-client-java</artifactId>
124 <version>1.2.2</version>
125 </dependency>
126 ]]></programlisting>
127 </section>
128 <section id="client-client">
129 <title>J4pClient</title>
130 <para>
131 <classname>J4pClient</classname> is the entry point for sending
132 requests to a remote Jolokia agent. It can be created in multiple
133 ways. For simple cases, public constructors are provided taking
134 the mandatory Jolokia agent URL and optionally a
135 <classname>org.apache.http.client.HttpClient</classname>
136 instance which is used for the HTTP business. The recommended style
137 is to use the J4pClientBuilder, though. This way, all
138 parameters for the HTTP communication can easily be set:
139 </para>
140 <programlisting language="java"><![CDATA[
141 J4pClient j4p = J4pClient.url("http://localhost:8080/jolokia")
142 .user("roland")
143 .password("s!cr!t")
144 .authenticator(new BasicAuthenticator().preemptive())
145 .connectionTimeout(3000)
146 .build();
147 ]]></programlisting>
148 <para>
149 The builder supports the following parameters with the given
150 defaults:
151 </para>
152 <table>
153 <title>J4pClient parameters</title>
154 <thead>
155 <tr>
156 <td>Parameter</td>
157 <td>Description</td>
158 <td>Default</td>
159 </tr>
160 </thead>
161 <tr>
162 <td><constant>url</constant></td>
163 <td>The URL to the Jolokia agent. This is the only mandatory parameter.</td>
164 <td></td>
165 </tr>
166 <tr>
167 <td><constant>user</constant></td>
168 <td>User name when authentication is used. If not set, no
169 authentication is used. If set, <constant>password</constant>
170 must be set, too</td>
171 <td></td>
172 </tr>
173 <tr>
174 <td><constant>password</constant></td>
175 <td>Password used for authentication. Only used when
176 <constant>user</constant> is set.</td>
177 <td></td>
178 </tr>
179 <tr>
180 <td><constant>authenticator</constant></td>
181 <td>Implementation of <classname>J4pAuthenticator</classname>. The Java client comes with
182 one implementation <classname>BasicAuthenticator</classname> for using basic authentication.
183 This class supports also <emphasis>preemptive</emphasis> authentication. Call <methodname>preemptive()</methodname> to
184 switch this on (see above for an example). Basic authentication is the default if no other
185 authenticator is set.Only used when <constant>user</constant> is set, too.</td>
186 <td></td>
187 </tr>
188 <tr>
189 <td><constant>target</constant></td>
190 <td>A JMX JSR-160 ServiceURL which should be used by the agent
191 as the <emphasis>real</emphasis> target. This parameter should
192 be set if the client is used for accessing the agent in <xref
193 linkend="proxy">proxy mode</xref>.
194 </td>
195 <td></td>
196 </tr>
197 <tr>
198 <td><constant>targetUser</constant></td>
199 <td>The JSR-160 user to use when using the proxy mode. If not
200 given (and <constant>target</constant> is set), then no
201 authentication is used for JSR-160 communication.
202 </td>
203 <td></td>
204 </tr>
205 <tr>
206 <td><constant>targetPassword</constant></td>
207 <td>JSR-160 Password to use for the proxy mode.</td>
208 <td></td>
209 </tr>
210 <tr>
211 <td><constant>connectionTimeout</constant></td>
212 <td>The timeout in milliseconds until a connection is
213 established. A timeout value of zero is interpreted as an
214 infinite timeout.
215 </td>
216 <td>20000</td>
217 </tr>
218 <tr>
219 <td><constant>pooledConnection</constant></td>
220 <td>
221 Specifies, that the underlying HttpClient should use pooled
222 connection manager, which is thread safe and can service
223 connection requests from multiples threads
224 simultaneously. This is important if the
225 <classname>J4pClient</classname> is to be used in a multi
226 threaded context. The size of the pool is restricted by the
227 parameter <constant>maxTotalConnection</constant>.
228 <classname>ThreadSafeClientConnManager</classname> is the
229 underlying connection manager. Pooled connections are the
230 default.
231 </td>
232 <td></td>
233 </tr>
234 <tr>
235 <td><constant>singleConnection</constant></td>
236 <td>
237 Specifies that single connection should be used which
238 maintains only one active connection at a time. Even though
239 <classname>J4pClient</classname> is still thread-safe it
240 ought to be used by one execution thread only. The
241 underlying connection manager is
242 <classname>SingleClientConnManager</classname> Pooled
243 connections are the default.
244 </td>
245 <td></td>
246 </tr>
247 <tr>
248 <td><constant>maxTotalConnections</constant></td>
249 <td>
250 Defines the number of total connections to be pooled. It
251 is only used when <constant>pooledConnection</constant> is
252 used.
253 </td>
254 <td>20</td>
255 </tr>
256 <tr>
257 <td><constant>defaultMaxConnectionsPerRoute</constant></td>
258 <td>
259 Defines the number of total connections per route. It
260 is only used when <constant>pooledConnection</constant> is
261 used.
262 </td>
263 <td>20</td>
264 </tr>
265 <tr>
266 <td><constant>maxConnectionPoolTimeout</constant></td>
267 <td>
268 Defines the timeout for waiting to obtain a connection
269 from the pool. This parameter is only used when
270 <constant>pooledConnections</constant> are used.
271 </td>
272 <td>500</td>
273 </tr>
274 <tr>
275 <td><constant>socketTimeout</constant></td>
276 <td>
277 Defines the socket timeout (<code>SO_TIMEOUT</code>) in milliseconds,
278 which is the timeout for waiting for data or, put differently,
279 a maximum period inactivity between two consecutive data packets.
280 A timeout value of zero is interpreted as an infinite timeout.
281 </td>
282 <td>0</td>
283 </tr>
284 <tr>
285 <td><constant>contentCharset</constant></td>
286 <td>
287 Defines the charset to be used per default for encoding
288 content body.
289 </td>
290 <td>ISO-8859-1</td>
291 </tr>
292 <tr>
293 <td><constant>expectContinue</constant></td>
294 <td>
295 Activates <literal>Expect: 100-Continue</literal> handshake
296 for the entity enclosing methods. The purpose of the
297 <literal>Expect: 100-Continue</literal> handshake to allow a
298 client that is sending a request message with a request body
299 to determine if the origin server is willing to accept the
300 request (based on the request headers) before the client
301 sends the request body. The use of the <literal>Expect:
302 100-continue</literal> handshake can result in noticeable
303 performance improvement for entity enclosing requests that
304 require the target server's authentication.
305 </td>
306 <td>true</td>
307 </tr>
308 <tr>
309 <td><constant>tcpNoDelay</constant></td>
310 <td>
311 Determines whether Nagle's algorithm is to be used. The
312 Nagle's algorithm tries to conserve bandwidth by minimizing
313 the number of segments that are sent. When applications wish
314 to decrease network latency and increase performance, they
315 can disable Nagle's algorithm (that is enable
316 <constant>TCP_NODELAY</constant>). Data will be sent
317 earlier, at the cost of an increase in bandwidth
318 consumption.
319 </td>
320 <td>true</td>
321 </tr>
322 <tr>
323 <td><constant>socketBufferSize</constant></td>
324 <td>
325 Determines the size of the internal socket buffer in bytes
326 used to buffer data while receiving and transmitting HTTP
327 messages.
328 </td>
329 <td>8192</td>
330 </tr>
331 <tr>
332 <td><constant>proxy</constant></td>
333 <td>
334 Determines http proxy server. It can be defined as
335 <constant>http://user:password@host:port</constant>. <emphasis>user</emphasis> and
336 <emphasis>password</emphasis> are optional.
337 </td>
338 <td></td>
339 </tr>
340 <tr>
341 <td><constant>useProxyFromEnvironment</constant></td>
342 <td>
343 Set the proxy for this client based on <constant>http_proxy</constant> system environment variable.
344 Expect formats are <constant>http://user:pass@host:port</constant> or <constant>http://host:port</constant>
345 Example: <constant>http://tom:[email protected]:8080</constant>
346 </td>
347 <td></td>
348 </tr>
349 <tr>
350 <td><constant>responseExtractor</constant></td>
351 <td>
352 A response extractor can be used for hooking into the JSON
353 deserialization process when a JSON response is converted
354 into a <classname>J4pResponse</classname> object. By
355 default, the received JSON object is examined for a status
356 code of 200 and only then creates a response
357 object. Otherwise an exception is thrown. An extractor is
358 specified by the interface
359 <classname>J4pResponseExtractor</classname>. Beside the
360 default extractor, an alternate extractor
361 <classname>ValidatingResponseExtractor</classname> can be
362 used, which instead of throwing an exception returns a
363 <code>null</code> object when the response has a status of
364 404. An extractor can be specified as extra argument to the
365 execute method, too.
366 </td>
367 <td></td>
368 </tr>
369 </table>
370 <para>
371 The <classname>J4pClient</classname> provides various variants
372 of a <methodname>execute()</methodname> method, which takes
373 either one single request or a list of requests. For a single
374 request, the preferred HTTP method (GET or POST) can be
375 specified optionally. The <classname>List&lt;R&gt;</classname>
376 argument can be used type only for a homogeneous bulk request,
377 i.e. for multiple requests of the same time. Otherwise an
378 untyped list must be used.
379 </para>
380 <para>
381 Each request can be tuned by giving a map of processing options
382 along with their values to the <methodname>execute</methodname>
383 method. The possible options are shown in table <xref
384 linkend="client-java-queryopts"/>.
385 </para>
386 <table id="client-java-queryopts">
387 <title>J4pClient query parameters</title>
388 <thead>
389 <tr>
390 <td>J4pQueryParameter enum</td>
391 <td>Description</td>
392 </tr>
393 </thead>
394 <tr>
395 <td><constant>MAX_DEPTH</constant></td>
396 <td>
397 Maximum traversal depth for serialization of complex
398 objects. Use this with a "list" request to restrict the
399 depth of the returned meta data tree.
400 </td>
401 </tr>
402 <tr>
403 <td><constant>MAX_COLLECTION_SIZE</constant></td>
404 <td>
405 Maximum size of collections returned during serialization.
406 If larger, a collection is truncated to this size.
407 </td>
408 </tr>
409 <tr>
410 <td><constant>MAX_OBJECTS</constant></td>
411 <td>
412 Maximum number of objects returned in the response's value.
413 </td>
414 </tr>
415 <tr>
416 <td><constant>IGNORE_ERRORS</constant></td>
417 <td>
418 Option for ignoring errors during JMX operations and JSON
419 serialization. This works only for certain operations like
420 pattern reads and should be either <literal>true</literal>
421 or <literal>false</literal>.
422 </td>
423 </tr>
424 <tr>
425 <td><constant>INCLUDE_STACKTRACE</constant></td>
426 <td>
427 Whether to include a stack trace in the response when an
428 error occurs. The allowed values are
429 <literal>true</literal> for inclusion,
430 <literal>false</literal> if no stacktrace should be
431 included or <constant>runtime</constant> if only
432 <constant>RuntimeException</constant>s should be
433 included. Default is <literal>true</literal>.
434 </td>
435 </tr>
436 <tr>
437 <td><constant>SERIALIZE_EXCEPTION</constant></td>
438 <td>
439 Whether to include a JSON serialized version of the
440 exception. If set to <literal>true</literal>, the exception
441 is added under the key <constant>error_value</constant> in
442 the response. Default is <literal>false</literal>.
443 </td>
444 </tr>
445 <tr>
446 <td><constant>CANONICAL_NAMING</constant></td>
447 <td>
448 Whether property keys of <classname>ObjectNames</classname>
449 should be ordered in the canonical way or in the way that
450 they are created. The allowed values are either
451 <literal>true</literal> in which case the canonical key
452 order (== alphabetical sorted) is used or
453 <literal>false</literal> for getting the keys as
454 registered. Default is <literal>true</literal>
455 </td>
456 </tr>
457 </table>
458 </section>
459
460 <section id="client-java-requests">
461 <title>Request types</title>
462 <para>
463 For each request type a dedicated request object is provided
464 which all are subclasses from
465 <classname>J4pRequest</classname>. For all requests it can be
466 specified which HTTP method is to be used by setting the
467 property <varname>preferredHttpMethod</varname> to either
468 <literal>GET</literal> or <literal>POST</literal>.
469
470 Each request type has a corresponding response type which used
471 for the return values of the
472 <methodname>J4pClient.execute()</methodname>.
473 </para>
474 <para>
475 The constructor of each kind of request can take a
476 <classname>J4pTargetConfig</classname> as argument for using a
477 request in <xref linkend="proxy">proxy mode</xref>. This
478 configurational object holds the JMX service url and
479 (optionally) credentials for JSR-160 authentication. When
480 given, this proxy target specification overrides any default
481 proxy configuration set during the initialization of the
482 <classname>J4pClient</classname>.
483 </para>
484 <variablelist>
485 <varlistentry>
486 <term><classname>J4pReadRequest</classname> and
487 <classname>J4pReadResponse</classname></term>
488 <listitem>
489 <para>
490 <classname>J4pReadRequest</classname> is a read request to
491 get one or more attributes from one or more MBeans within
492 a single request. Various constructor variants can be used
493 to specify one or more attributes along with the
494 ObjectName (which can be a pattern). A
495 <varname>path</varname> can be set as property for
496 specifying an <emphasis>inner path</emphasis>, too.
497 </para>
498 <para>
499 <classname>J4pReadResponse</classname> is the
500 corresponding response type and allows typed acces to the
501 fetched value for a single attribute fetch or to multiple
502 values for a multi attribute read. In the latter case, the
503 found object and attributes names can be retrieved as
504 well.
505 </para>
506 <para>
507 For more information on fetching the value of multiple
508 attributes and multiple MBeans at once, please refer to
509 <xref linkend="read"/> or the Javadoc of
510 <classname>J4pReadResponse</classname>.
511 </para>
512 </listitem>
513 </varlistentry>
514 <varlistentry>
515 <term><classname>J4pWriteRequest</classname> and <classname>J4pWriteResponse</classname></term>
516 <listitem>
517 <para>
518 A <classname>J4pWriteRequest</classname> is used to set
519 the value of an MBean attribute. Beside the mandatory
520 object and attribute name the value must be give in the
521 constructor as well. Optionally a <varname>path</varname>
522 can be provided, too. Only certain types for the given
523 value can be serialized properly for calling the Jolokia
524 agent as described in <xref
525 linkend="serialization-request"/>.
526 </para>
527 <para>
528 The old value is returned as <classname>J4pWriteResponse</classname>'s value.
529 </para>
530 </listitem>
531 </varlistentry>
532 <varlistentry>
533 <term><classname>J4pExecRequest</classname> and
534 <classname>J4pExecResponse</classname></term>
535 <listitem>
536 <para>
537 <classname>J4pExecRequest</classname>s are used for
538 executing operation on MBeans. The constructor takes as
539 mandatory arguments the MBean's object name, the operation
540 name and any arguments required by the operation. Only
541 certain types for the given arguments can be serialized
542 properly for calling the Jolokia agent as described in
543 <xref linkend="serialization-request"/>.
544 </para>
545 <para>
546 The returned <classname>J4pExecResponse</classname>
547 contains the return value of the operation called.
548 </para>
549 </listitem>
550 </varlistentry>
551 <varlistentry>
552 <term><classname>J4pSearchRequest</classname> and
553 <classname>J4pSearchResponse</classname></term>
554 <listitem>
555 <para>
556 A <classname>J4pSearchRequest</classname> contains a
557 valid single MBean object name pattern which is used for
558 searching MBeans.
559 </para>
560 <para>
561 The <classname>J4pSearchResponse</classname> holds a
562 list of found object names.
563 </para>
564 </listitem>
565 </varlistentry>
566 <varlistentry>
567 <term><classname>J4pListRequest</classname> and
568 <classname>J4pListResponse</classname></term>
569 <listitem>
570 <para>
571 For obtaining meta data on MBeans a
572 <classname>J4pListRequest</classname> should be used. It
573 can be used with a <emphasis>inner path</emphasis> to
574 obtain only a subtree of the response, otherwise the whole
575 tree as described in <xref linkend="response-list"/> is
576 returned. With the query parameter
577 <constant>maxDepth</constant> can be used to restrict the
578 depth of returned tree.
579 </para>
580 <para>
581 The single value of a
582 <classname>J4pListResponse</classname> is a tree (or
583 subtree) as a JSON object, which has the format described
584 in <xref linkend="response-list"/>.
585 </para>
586 </listitem>
587 </varlistentry>
588 <varlistentry>
589 <term><classname>J4pVersionRequest</classname></term>
590 <listitem>
591 <para>
592 A <classname>J4pVersionRequest</classname> request the
593 Jolokia agent's version information and takes no
594 argument.
595 </para>
596 <para>
597 The <classname>J4pVersionResponse</classname> returns the
598 agent's version (<varname>agentVersion</varname>), the
599 protocol version (<varname>protocolVersion</varname>), the
600 application server product name
601 (<varname>product</varname>), the vendor name
602 (<varname>vendor</varname>) and any extra info
603 (<varname>extraInfo</varname>) specific to the platform
604 the Jolokia is running on.
605 </para>
606 </listitem>
607 </varlistentry>
608 </variablelist>
609 </section>
610
611 <section id="client-java-execptions">
612 <title>Exceptions</title>
613 <para>
614 In case of an error when executing a request a
615 <classname>J4pException</classname> or one its subclass is
616 thrown.
617 </para>
618 <variablelist>
619 <varlistentry>
620 <term><classname>J4pConnectException</classname></term>
621 <listitem>
622 <para>
623 Exception thrown when the connection to the server
624 fails. It contains the original
625 <classname>ConnectException</classname> as nested value.
626 </para>
627 </listitem>
628 </varlistentry>
629 <varlistentry>
630 <term><classname>J4pTimeoutException</classname></term>
631 <listitem>
632 <para>
633 Exception thrown in case of an timeout. The nested
634 exception is of type
635 <classname>ConnectTimeoutException</classname>.
636 </para>
637 </listitem>
638 </varlistentry>
639 <varlistentry>
640 <term><classname>J4pRemoteException</classname></term>
641 <listitem>
642 <para>
643 Generic exception thrown when an exception occurred on the
644 remote side. This is the case when the JSON response
645 obtained is an error response as described in <xref
646 linkend="responses"/>. The error type, error value, the
647 status, the request leading to this error and the remote
648 stacktrace as string) can be obtained from this exception.
649 </para>
650 </listitem>
651 </varlistentry>
652 <varlistentry>
653 <term><classname>J4pBulkRemoteException</classname></term>
654 <listitem>
655 <para>
656 Exception thrown when a bulk request fails on the remote
657 side. This contains a mixed list which contains the
658 <classname>J4pRemoteException</classname> occurred as well
659 as the <classname>J4pResponse</classname> objects for the
660 requests, which succeeded. The list obtained by
661 <methodname>getResults()</methodname> contains these
662 objects in the same order as the list of requests given to
663 <methodname>execute</methodname>. All responses and remote
664 exceptions can also be obtained separately in homogeneous
665 lists.
666 </para>
667 </listitem>
668 </varlistentry>
669 <varlistentry>
670 <term><classname>J4pException</classname></term>
671 <listitem>
672 <para>
673 Base exception thrown, when no other exception fits,
674 i.e. when the exception happened on the client side. The
675 original exception is contained as nested exception.
676 </para>
677 </listitem>
678 </varlistentry>
679 </variablelist>
680 </section>
681 </section>