db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Db-derby Wiki] Update of "DerbyJMX" by JohnHEmbretsen
Date Thu, 10 Apr 2008 11:09:29 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Db-derby Wiki" for change notification.

The following page has been changed by JohnHEmbretsen:
http://wiki.apache.org/db-derby/DerbyJMX

The comment on the change is:
Some more Java example code

------------------------------------------------------------------------------
  
  Besides using generic tools as described above, it is also possible to access Derby's MBeans
programmatically, that is from a Java application. How to do this may depend on how you configure
the JVM running Derby, for example how you configure user authentication and authorization,
or from which host(s) you want to access the MBeans. 
  
- Below is some example code which should help you get started. It is assumed that the client
JVM will be supporting J2SE 5.0 or newer. Most examples include code which throws exceptions
that would have to be caught or re-thrown when appropriate (not shown).
+ Below is some example code which should help you get started. It is assumed that the client
JVM will be supporting J2SE 5.0 or newer (you will find the JMX classes you need in the packages
`javax.management` and `javax.management.remote`). Most examples include code which throws
exceptions that would have to be caught or re-thrown when appropriate (not shown).
+ 
+ ''Note that you do '''not''' need any Derby libraries in the JMX client application's classpath
(unless MBean proxies are used)''.
  
  === Connecting to the MBean Server ===
  
@@ -248, +250 @@

      }
  }}}
  
+ [[Anchor(SysID)]]
  === Obtaining the system identifier ===
  
  The system identifier is a unique String distinguishing one running Derby system from another.
All MBeans that are instantiated by Derby include the system identifier in their object names.
  
  One way to access an MBean is to fully specify its object name when contacting the MBean
server. For this, you need to know the current system identifier. (Alternative ways to access
MBeans include querying the MBean server for all MBeans, or MBeans which object names match
a specific pattern.)
  
- '''The following example shows how to obtain the system identifier by accessing the `ManagementMBean`:'''
+ '''The following example shows how to obtain the system identifier by accessing a `ManagementMBean`:'''
  {{{#!java
      // assuming we already have a reference to the ManagementMBean's object name
-     String sysID = (String) mbeanServerConn.getAttribute(mgmtObjName, "SystemIdentifier");
+     String systemID = (String) mbeanServerConn.getAttribute(mgmtObjName, "SystemIdentifier");
  }}}
  
  '''The following example shows how to obtain the system identifier from a Derby MBean's
object name:'''
  {{{#!java
      // assuming we already have a reference to the ObjectName of an MBean registered by
Derby, 
      // for example the Derby-registered ManagementMBean
-     // TODO
+     String systemID = derbyMgmtObjectName.getKeyProperty("system");
  }}}
  
+ === Accessing a specific Derby-registered MBean ===
  
- In the above examples you have already seen how to read a single MBean attribute, and how
to invoke an MBean operation...
+ In the above examples you have already seen how to read a single MBean attribute, and how
to invoke an MBean operation. In order to do this, you usually need a reference to the MBean's
!ObjectName. 
+ 
+ If you have access to the (Javadoc) [http://db.apache.org/derby/javadoc/publishedapi/jdbc4/
API of Derby's MBeans] as well as the [#SysID system identifier] of the Derby system you are
accessing through JMX, you have all the information you need to be able to instantiate an
`javax.management.ObjectName` object directly, by fully specifying its String representation
(see the `ObjectName API` for details).
+ 
+ '''This example code shows how to obtain a reference to the VersionMBean for derby.jar:'''
+ {{{#!java
+     // Assuming we already know the system idenfifier (see examples above), systemID.
+     // A list of key properties is available is each MBean's Javadoc API.
+     Hashtable<String, String> keyProps = new Hashtable<String, String>();
+     keyProps.put("type", "Version");
+     keyProps.put("jar", "derby.jar");
+     keyProps.put("system", systemID);
+     // MBeans registered by Derby always belong to the "org.apache.derby" domain
+     ObjectName versionObjectName = new ObjectName("org.apache.derby", keyProps);
+ 
+     // we can now use the object name to read an attribute
+     String versionString = (String) mbeanServerConn.getAttribute(versionObjectName, "VersionString");
+     System.out.println("VersionString: " + versionString);
+ }}}
+ 
+ Example output:
+ {{{
+ VersionString: 10.4.1.1 - (645238)
+ }}}
+ 
- ----
+ -----
  
  [[AttachInfo]]
  

Mime
View raw message