package.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
  2. <!-- package.html - describes classes in javax.management package.
  3. Copyright (C) 2007 Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. -->
  32. <html>
  33. <head><title>GNU Classpath - javax.management</title></head>
  34. <body>
  35. <p>
  36. Provides the core classes for the Java Management Extensions (JMX). This API
  37. builds on the notion of Java beans by providing a layer of abstraction between
  38. the beans themselves and the method of accessing them. Instead of being accessed
  39. directly, management beans or <strong>MBeans</strong> are usually accessed via
  40. a management server (an implementation of the @see MBeanServer interface). Thus,
  41. the bean itself may be a simple Java object or it may be something
  42. more complicated (for example, the server may map from Java to SNMP). The server may
  43. also retrieve the bean from some remote location rather than using a local object.
  44. </p>
  45. <p>
  46. Management beans are usually used for monitoring and/or configuration
  47. of a particular entity. For example, the platform management beans
  48. found in the @see java.lang.management package allow the user
  49. to obtain information about the operating system, current memory usage, etc.
  50. as well as turning on and off certain additional facilities. To this end,
  51. an MBean consists of:
  52. </p>
  53. <ul>
  54. <li><emph>attributes</emph> that may be read and/or written to.</li>
  55. <li><emph>operations</emph> which may be performed.</li>
  56. <li><emph>notifications</emph> that may emitted by the bean and listened for by users.</li>
  57. </ul>
  58. <p>
  59. The most common type of management bean is the @see StandardMBean, A standard MBean
  60. relies on the naming patterns established by the JavaBeans framework; the value of an
  61. attribute <code>name</code> is retrieved by an accessor method named <code>getName</code>
  62. and changed by a mutator method called <code>setName</code>. If the mutator is absent,
  63. the attribute is read only. Naming is also used to associate the implementation of a
  64. bean with its interface; an bean <code>Person</code> is assumed to be an implementation
  65. of the interface <code>PersonMBean</code> (and vice versa). To avoid these naming constraints,
  66. the @see StandardMBean class may be used.
  67. </p>
  68. <p>
  69. <h2>Types of Beans</h2>
  70. <p>
  71. The @see StandardMBean class is one example of a @see DynamicMBean where the attributes and
  72. operations of the bean are provided dynamically via the methods provided. With the
  73. @see StandardMBean class, this simply means that the class uses reflection to access the
  74. appropriate methods of the bean implementation. In a more complex scenario, the bean's
  75. data could be supplied from a file or over the network.
  76. </p>
  77. <p>
  78. Once we start talking about accessing beans over network and platform boundaries, we run
  79. in to the issue of how to deal with the types utilised by these beans. Simple types, such
  80. as numbers and strings, are usually fine but more complex types need special treatment.
  81. An <emph>Open MBean</emph> is one that only uses a specific set of types defined in the
  82. @see javax.management.openmbean package, allowing both sides of a remote connection to provide
  83. this subset of types and thus interact. An @see MXBean goes a stage further, and defines
  84. a method whereby a normal Java MBean may become an Open MBean by performing a defined mapping
  85. on the types of the bean. For example, a @see java.util.List or @see java.util.Set of a
  86. particular type becomes an array of the same type.
  87. </p>
  88. <h2>Accessing Beans</h2>
  89. <p>
  90. Although beans can be accessed like normal objects, the normal way of accessing them is
  91. via an @see MBeanServer. This provides the abstraction from the bean's implementation to
  92. a set of attributes, operations and notifications. The server identifies each bean via
  93. an @see ObjectName. This name is unique to a particular bean and is used to identify the
  94. bean when retrieving the value of an attribute or invoking an operation. Essentially, most
  95. methods provided by the server are the same as those provided by the @see DynamicMBean
  96. interface, except that each takes this additional @link ObjectName parameter to identify the
  97. bean being accessed.
  98. </p>
  99. <p>
  100. The @see MBeanServerFactory keeps track of the current MBean servers in use and allows new
  101. ones to be created. A special @see MBeanServer instance, called the <emph>platform MBean
  102. server</emph>, is created when the Java virtual machine is started and a reference to this
  103. may be obtained from the @see ManagementFactory using
  104. @see ManagementFactory#getPlatformMBeanServer(). This primarily exists for the purpose of
  105. creating and registering the platform MBeans, described in @see java.lang.management, which
  106. provide access to information about the underlying operating system, memory usage, the behaviour
  107. of the garbage collector, etc. but is equally suitable for creating and registering your own
  108. beans. Alternatively, a server instance may be obtained from the @see MBeanServerFactory.
  109. </p>
  110. <p>
  111. A bean obtains an @link ObjectName by registering with the server. This operation can be
  112. performed either by passing an existing instance to the @see MBeanServer#registerMBean method
  113. or by using the @see MBeanServer#createMBean method to simultaneously create the bean and
  114. register it with the server. During the registration process, the bean may perform some
  115. arbitrary behaviour if it implements the @link MBeanRegistration interface. The same is
  116. true when unregistering a bean.
  117. </p>
  118. <p>
  119. To actually access the attributes and operations of a bean via the server, we use code
  120. like the following:
  121. </p>
  122. <pre>
  123. // First we obtain the platform MBean server which has the platform MBeans registered
  124. MBeanServer server = ManagementFactory.getPlatformMBeanServer();
  125. // We also need the object name of the memory bean so we can address it
  126. ObjectName name = new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME);
  127. // Next we obtain the value of the 'verbose' attribute
  128. // What actually happens here is that the server invokes the 'isVerbose' method of
  129. // the MemoryMXBean
  130. boolean verbose = server.getAttribute(name, "verbose");
  131. // We can also set the value of verbose. Again the server is actually performing
  132. // a setVerbose(val) on the bean but we don't need to know this.
  133. Attribute attrib = new Attribute("verbose", true);
  134. server.setAttribute(name, attrib);
  135. // We can also invoke the 'gc' operation which calls the garbage collector.
  136. server.invoke(name, "gc", new Object[]{}, new String[]{});
  137. </pre>
  138. <p>
  139. As noted above, the server is simply making basic method calls on the object using
  140. reflection. However, the server provides a layer of abstraction which means that something
  141. more complicated could actually be going on. The lines above are equally applicable, for
  142. example, if <code>server</code> is instead an @see MBeanServerConnection connecting us
  143. to a distant computer.
  144. </p>
  145. <p>
  146. This rather hideous code can be simplified back into simple method calls on an object,
  147. so that we get the best of both worlds. This is achieved using a <emph>MBean proxy</emph>:
  148. <pre>
  149. MBeanServer server = ManagementFactory.getPlatformMBeanServer();
  150. ObjectName name = new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME);
  151. MemoryMXBean bean = JMX.newMBeanProxy(server, name, MemoryMXBean.class);
  152. boolean verbose = bean.isVerbose();
  153. bean.setVerbose(true);
  154. bean.gc();
  155. </pre>
  156. <p>
  157. See how much simpler the operations are? The proxy handles the task of translating the method
  158. calls into appropriate invocations of methods on the server, simplifying the code for the user.
  159. </p>
  160. <p>
  161. Finally, we have assumed in the code above that the @see ObjectName of the bean is known.
  162. If this is not the case, then the server's database can be searched. The @see Query class
  163. provides appropriate operators (e.g. boolean (and,or), value comparison (&gt;, &lt;)) for
  164. building up relatively complex queries. Once constructed, a query may be passed to either
  165. the @see MBeanServer#queryNames or @see MBeanServer#queryMBeans to obtain an appropriate
  166. set of @see ObjectName or MBean instances.
  167. </p>
  168. <h2>Notifications</h2>
  169. <p>
  170. MBeans also have the capability to emit events. Beans which do so implement either the
  171. @see NotificationBroadcaster or @see NotificationEmitter interface (the difference between
  172. the two is simply the existence of a better removal method in the newer
  173. @see NotificationEmitter interface, which otherwise extends @see NotificationBroadcaster),
  174. usually by extending the @see NotificationBroadcasterSupport class. As is usual with event
  175. handling, other classes may <emph>signup</emph> to receive events via the
  176. @see NotificationListener interface. The signup process can include registering a filter
  177. (an implementation of @see NotificationFilter) so that only certain events reach the
  178. listener and others are discarded.
  179. </p>
  180. <h2>Remote Access</h2>
  181. <p>
  182. The subpackage @see javax.management.remote provides facilities to access remote MBean
  183. servers. This consists of a <emph>connector</emph> framework which abstracts the method
  184. of accessing remote servers from the actual implementation, so that the same method is
  185. used to connect to a remote server, regardless of how it is accessed.
  186. </p>
  187. </body>
  188. </html>