Middleware
Application Server
$Id: readme.html 592 2006-08-16 00:21:17Z tgrall $ $Date: 2006-08-15 17:21:17 -0700 (Tue, 15 Aug 2006) $
| Date | Who | What |
| 08-10-2006 | sbutton | Initial creation |
In some circumstances, it may be necessary to register MBeans (or perform other JMX operations) at certain places within the OC4J initialization or shutdown lifecycle. This can be accomplished using a combination the OC4J startup/shutdown classes and the JMX API to both create a custom MBeanServer and programmatically register the required MBean classes in the custom MBeanServer.
This how-to needs to be built and deployed before it can be accessed. It is not delivered in a ready-to-deploy manner. However the build process is relatively simple thanks to the wonder of Ant.
First off, let's examine the contents of the how-to distribution.
|
|
The src directory contains the Java code for the OC4JStartup class and the example MBean.
PostDeployMBeanRegister is the class which implements the OC4JStartup interface.
MyExampleMBean is the interface for the MBean we are using.
MyExample is the implementation of the MBean interface.
Since this how-to is demonstrating the use of an OC4JStartup class, its elements are quite simple. An OC4JStartup class is a container level resource and is applied to a server in a simple JAR file. This means there is no need for the use of any deployment descriptors.
The MBean used in this example is only ever used by the OC4JStartup class, so it can be deployed within the same JAR file as the OC4JStartup class.
The build.xml file is the main Ant build file that will compile and package the classes into a JAR file.
Make sure the following properties are configured correctly in the oracle-ant.properties file located in the root directory of this how-to distribution.
NOTE: Some of these properties will default to the values of corresponding environment variables as noted below. If you have these variables setup in your environment you may not have to alter the values in the file. If necessary, modify these variables to the proper values for you environment:
In addition, please make sure that the ant command associated with the OC4J ant distribution is in your execution path ( %ORACLE_HOME%/ant/bin).
>ant
Executing the default all target of the supplied build.xml file performs the following steps:
Once the example has been built and packaged, the server can be configured with the startup class details,
Edit the %ORACLE_HOME%/j2ee/home/config/server.xml file and add the following entries
<init-library path="../how-to-startup-mbeans.jar" />
<startup-classes>
<startup-class classname="
demo.oc4j.startupmbeans.startup.PostDeployMBeanRegister" failure-is-fatal="true" >
<execution-order>0</execution-order>
<init-param>
<param-name>
mbean_server_name</param-name>
<param-value>
StartupMBeanServer</param-value>
</init-param>
</startup-class>
</startup-classes>
|
There are two separate elements in the configuration text to be added:
<init-library> adds a library to OC4J which is used to load startup (and/or shutdown) classes.
<startup-classes> defines a set of classes to be invoked during the container startup phase. Parameters can be passed to startup class instances using the <init-param> tag. In this case, the mbean_server_name parameter is set with a value of StartupMBeanServer. This parameter is retrieved in the startup class and used as the name of the custom MBeanServer that is created.
With the required entries added to server.xml file, OC4J can be started.
Start your OC4J 10.1.3 instance as follows:
>%ORACLE_HOME%/bin/oc4j start
Note that the oc4j.cmd script expects the JAVA_HOME environment variable to point to a full JDK installation.
If the startup class is correctly configured, the console should contain messages showing the startup class operating.
C:\java\oc4j-10131-dev\j2ee\home>java -jar oc4j.jar
06/08/14 15:50:10 demo.oc4j.startupmbeans.startup.PostDeployMBeanRegister::preDeploy
06/08/14 15:50:17
demo.oc4j.startupmbeans.startup.PostDeployMBeanRegister::postDeploy
06/08/14 15:50:17
creating MBeanServer("StartupMBeanServer")
06/08/14 15:50:17
registering MBean(":name=MyExampleMBean")
|
With the startup class configured correctly and OC4J started, the MBeanServer will be created and the MyExampleMBean registered within it.
Using Application Server Control (ASC) the custom MBeanServer and MBean can be viewed.
Open a browser window and enter the URL:
http://localhost:8888/em
Custom MBeanServer instances can be viewed from the Application MBean Browser.
| 1. |
Click in the Applications tab Click the Application Defined MBeans icon for the ascontrol application to display the Application MBean Browser. |
![]() |
|
| 2. |
The Application MBean Browser will show the custom MBeanServer that was created by the startup class. The name of the MBean will match the mbean_server_name parameter, StartupMBeanServer in our example configuration. Expand the StartUpMBeanServer Select the MyExampleBean entry. The MyExample MBean will be displayed, showing the DateAndTime attribute that is exposed by the MBean. |
![]() |
There are several concepts to take into account here.
The area of OC4JStartup classes is documented in the OC4J Developer's Guide -- http://download-west.oracle.com/docs/cd/B25221_03/web.1013/b14433/startclas.htm.
Since a startup class is a container level resource, it does not have automatic access to the application specific MBeanServer created for applications to use. It must create its own MBeanServer if it needs to perform JMX based operations, and register MBeans within it.
public String
postDeploy(Hashtable params, Context arg1) throws Exception {
MBeanServer mbs;
String mbsName = DEFAULT_MBEANSERVER_NAME;
System.out.println(getClass().getName() + "::postDeploy");
// See if the name of the MBeanServer was specified as
// a parameter to the startup-class
if(params.keySet().contains(MBEAN_SERVER_PARAM)) {
mbsName = (String)params.get(MBEAN_SERVER_PARAM);
}
// Create a custom MBeanServer
System.out.println(" creating MBeanServer(\""+mbsName+"\")");
mbs = MBeanServerFactory.createMBeanServer(mbsName);
// Create the object to register as the MBean
MyExample myExample = new MyExample();
// Create the ObjectNme for the MBean
ObjectName mbeanName = new ObjectName(MBEAN_NAME);
// Register the MBean in the MBeanServer
System.out.println(" registering MBean(\""+mbeanName+"\")");
mbs.registerMBean(myExample, mbeanName);
return "postDeploy: success";
}
|
The MBean example used in this application is a trivial example of a Standard MBean.
public
interface MyExampleMBean {
public String getDateAndTime();
}
public
class MyExample implements MyExampleMBean {
static final DateFormat FORMATTER =
DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT);
/**
* Represents the attribute DateAndTime for the MBean
* @return Date and Time in String form
*/
public String getDateAndTime() {
Date now = new Date(System.currentTimeMillis());
return FORMATTER.format(now);
}
}
|