WebSphereTM Application Server V5 has at its core a JavaTM Management Extensions (JMX) MBeanServer that provides a standard interface to manage the application server components that are defined by the set of MBeans shipped with the product. These MBeans are adequate for administering the features that are included in WebSphere Application Server. But what if you have a new WebSphere application that requires new administration functions that you would like to integrate with the existing WebSphere administration functions shipped with the product? That is the subject of Part 4 in this article series on System Administration for WebSphere Application Server V5.
Accessing the embedded JMX MBeanServer
As mentioned in Part 1 and Part 2 of this series, WebSphere Application Server V5 administration is built around the JMX specification. Every WebSphere Application Server process includes an embedded JMX Agent. The main aspect of the JMX Agent is the MBeanServer that serves as a registry of the management interfaces (MBeans) that are available within the local JVM. Figure 1 shows this internal JMX architecture for a single Java Virtual Machine. The embedded JMX MBeanServer for WebSphere processes can be accessed and used to register your own MBeans directly.
Figure 1: JMX architecture
The following is an example of how code, running within a WebSphere Application Server process, can access the embedded MBeanServer and register its own MBean:
MBeanServer mbs = AdminServiceFactory.getMBeanFactory().getMBeanServer();
mbs.registerMBean(myObjectReference, myObjectName);
In this example,
myObjectReference
is a variable holding a reference to the implementation of your MBean, and
myObjectName
is the JMX ObjectName that this MBean will be registered to, and by which it will be identified. The
registerMBean
method is just the standard method defined by the JMX specification. Note that a reference to the embedded WebSphere MBeanServer can also be obtained by using the standard JMX MBeanServerFactory
findMBeanServer
API.
While this approach of accessing the embedded JMX MBeanServer and using its standard API is the most simple to implement, it is not the recommended approach for extending the WebSphere administration infrastructure for reasons that will be discussed later in this article.
If you run WebSphere Application Server with Java 2 Security enabled, the code sample above will throw an exception because the embedded JMX code is protected against use by any non-privileged code base. What this means is that in order to use JMX code in a WebSphere Application Server process (indeed, in order to use any WebSphere administration APIs at all), you must grant your code the necessary permissions in a policy file read by the Java 2 Security infrastructure. The JMX code in WebSphere Application Server is protected by two layers of permissions:
-
WebSphere management permissions that include
com.ibm.websphere.security.WebSphereRuntimePermission "AdminPermission". WithoutAdminPermission, your code cannot execute the method on the AdminServiceFactory to obtain a reference to the MBeanFactory instance for this server, as shown in the example above. -
TivoliTM TMX4J permissions, which include
com.tivoli.jmx.MBeanServerPermission "MBeanServer.*" and "MBeanServerFactory.*".
The following is an example of a policy stanza that you could add to the
was.policy
file for your application, if it contained code that accessed the embedded JMX MBeanServer.
grant codeBase "file:${application}" {
permission com.ibm.websphere.security.WebSphereRuntimePermission "AdminPermission";
};
grant codeBase "file:${application}" {
permission com.tivoli.jmx.MBeanServerPermission "MBeanServer.*";
};
WebSphere-specific ObjectName key properties
The WebSphere Application Server administration infrastructure provides some important extensions to the standard, single JVM architecture specified by JMX. In addition to greater security protections, one of the ways WebSphere extends standard JMX is by providing multi-node, multi-process, distributed MBean request routing and event notification. Part of what makes this distributed extension work is a convention for some special key properties in the ObjectName to be assigned to every MBean registered in the MBeanServer used by WebSphere Application Server.
The table below shows the special key properties that the WebSphere Application Server infrastructure uses, and expects to exist in the ObjectName for an MBean. If these key properties are not set in the MBean ObjectName, the MBean can still be registered in the MBeanServer, but no distributed services provided by WebSphere will be usable for this MBean. Without these key properties the MBean will only be accessible within the JVM where it is registered.
| ObjectName key property names in WebSphere Application Server | |
type
| resource type that the MBean represents |
name
| identifier for the individual instance of the MBean |
cell
| name of the cell in which the MBean is executing |
node
| name of the node in which the MBean is executing |
process
| name of the process in which the MBean is executing |
You can create the ObjectName for your MBean to include these key properties with their values set appropriately. Or, you can leverage the components of the WebSphere AdminService to create and register the MBean ObjectName on your behalf, as do all of the runtime MBeans shipped with WebSphere Application Server.
The AdminService components that are used for this are the MBeanFactory and an XML file that contains a description of your MBean. You call the
activateMBean
method of the MBeanFactory and pass it the XML MBean Descriptor file for your MBean. The MBeanFactory takes care of creating an ObjectName with all the necessary key properties in it (and any other key properties you specify), and registering your MBean with that ObjectName in the MBeanServer. After that, your MBean becomes accessible through the network defined by the entire WebSphere distributed cell.
WebSphere XML MBean Descriptors
It's worth spending some time at this point to examine the MBean XML Descriptors.
WebSphere MBean Descriptors declare all of the attributes, operations, and notifications for a given MBean type. The format of the MBean Descriptor file must match what is defined in the
MbeanDescriptor.dtd
file, which can be found in the properties directory under the root install directory of any WebSphere Application Server V5 installation. The MBeanFactory reads and parses an MBean Descriptor, and creates a JMX ModelMBean with the attributes, operations, and notifications declared in the descriptor. The MBeanFactory creates an ObjectName for the MBean, and then calls back to a specific piece of your code (your RuntimeCollaborator, explained later in this article) for any last minute adjustments before registering the MBean with the MBeanServer.
Figures 2 and 3 show two examples of MBean Descriptor files that will be used by examples in this article:
Figure 2. SnoopMBean MBean Descriptor
Figure 3. Voter MBean Descriptor
The MBean Descriptor in Figure 2 declares an MBean type of
SnoopMBean
that we will use to extend the SnoopServlet with administration capabilities. This MBean:
-
supports one attribute:
idenitification, which is a read-only string attribute. -
declares one operation:
snoopy, which takes a single string parameter.
The MBean Descriptor in Figure 3 declares an MBean type of
Voter
that:
-
supports two attributes:
-
identification, a read-only string attribute (read-only because there is no setMethod defined). -
age, a read-write integer attribute.
-
-
supports one operation:
setBallots, which takes two parameters:- an agent name string
- an array of ballot names.
The MBeanFactory and runtime collaborators
Your code needs to call the
activateMBean
method of the MBeanFactory in order for your MBean XML Descriptor to be parsed and for the MBean to be registered. There are several signatures of the
activateMBean
method for the WebSphere MBeanFactory. The javadoc for the MBeanFactory interface is part of the public javadoc that can be installed with the WebSphere Application Server, and is also available in the InfoCenter (see
Resources
). As you can see from the snippet of javadoc in Figure 4, there are three possible ways to call
activateMBean
.
Figure 4. Javadoc snippet for MBeanFactory
All forms of
activateMBean
take the same first three parameters:
-
type- the MBean type to be activated -
collaborator- theRuntimeCollaboratorinstance to be used with this MBean (more on this later) -
configId- an optional string that identifies the configuration information for the MBean, if applicable.
In addition to these three common parameters, two forms of
activateMBean
take a
descriptor
parameter, which identifies the name of the MBean XML Descriptor file and its location on the classpath. A third form of
activateMBean
takes a
keyProperties
parameter that allows you to optionally add arbitrary key properties to the ObjectName for the MBean.
Let's add some code to the SnoopServlet sample to activate the SnoopMBean so that we can see how all the pieces come together. Figure 5 shows the code that needs to be added to the SnoopServlet in order to activate the SnoopMBean.
Figure 5. SnoopServlet code to activate the SnoopMBean
To activate the SnoopMBean (Figure 5):
-
Declare an inner class,
Collabthat extends theRuntimeCollaboratorabstract class, and implements the operations and attributes declared for the MBean in the MBean Descriptor (Figure 2). -
In the service method for the servlet, obtain a reference to the
MBeanFactoryusing the staticgetMBeanFactory()method. Create an instance of thecollabRuntimeCollaborator, and call theactivateMBeanmethod, passing in the necessary parameters. -
Recompile the servlet. Note that you will also need to add a Java import statement for the package
com.ibm.websphere.management.*so that the classes referenced in the code can be located. You will also need to ensure that theadmin.jarfile from WebSphere Application Server is in the classpath. -
Place the following three files in the
classesdirectory for the deployed Web application
(installedApps\<nodename>\DefaultApplication.ear\DefaultWebApplication.war\):
WEB-INF\classes-
SnoopServlet.class(recompiled with new activateMBean code) -
SnoopServlet$Collab.class(inner class newly defined) -
SnoopMBean.xml(Figure 2).
-
- Stop and restart the DefaultApplication.
-
Point your browser to the Snoop servlet (
http:// hostname :9080/snoop). Look in theSystemOut.logfile for the server and you should see the trace output that the MBean has been activated:[3/12/03 23:12:29:260 CST] 3154ece9 SystemOut O SnoopMBean activated. -
When the MBean has been activated, you can use the
wsadminprogram to access and manage the MBean. Startwsadminand enter the commands below (in bold) to obtain a variable reference for SnoopMBean:wsadmin> set snoop [$AdminControl queryNames *:*,type=SnoopMBean] WebSphere:cell=bluehill,name=snoopMBeanId,mbeanIdentifier=snoopMBeanId, type=SnoopMBean,node=bluehill,process=server1 wsadmin> $AdminControl getAttributes $snoop {identification snoopy} wsadmin> $AdminControl invoke $snoop snoopy lookit
-
After invoking the
snoopyoperation on the MBean, look back in theSystemOut.logfile for the server and you will see:
which shows that the string[3/12/03 23:14:13:850 CST] 26856cef SystemOut O paramater is: lookitlookit, which was passed to the invoke call inwsadmin, was received and used by the SnoopMBean code that implements thesnoopyoperation.
This example above shows how to activate an MBean implemented in your J2EE application code. Before taking this approach, consider that your MBean will not be registered until some application client (browser or EJB client code) makes the first client request on your application. This may be okay, depending on your scenario, but rather than waiting for the first client request, it is often desireable for the MBean to be activated at server startup time. If this is preferable in your case, the following example shows one way this can be accomplished.
An ExtensionMBeanProvider is a code library that supplies a WebSphere MBean Descriptor and its associated code to implement the functions declared in the MBean Descriptor. Adding the configuration information for an ExtensionMBeanProvider library to a WebSphere Application Server will help the server's MBeanFactory locate the MBean descriptor file so it can be loaded and parsed.
Follow these general steps to add the ExtensionMBeanProvider configuration and the actual Extension MBean:
- The link to the admin console page for defining an ExtensionMBeanProvider is in the Additional Properties section of the server's Administration Services console page (Figure 6).
Figure 6. Link in Administration Services page to ExtensionMBeanProvider configuration
-
Select the
Extension MBean Providers
link. There are no Extension MBean Providers defined by default, so this page will initially be empty. To define a new Extension MBean Provider, select
New
. The information you will enter to define the new provider is:
-
Classpath (optional, but recommended) - This allows the classloader used by the MBeanFactory to locate the classes and XML MBean Descriptor file, if your code is not already placed in one of the default locations on the server's classpath (such as the
/classesdirectory under the installation root). - Description (optional) - Any useful text that can help distinguish this Extension MBean Provider from others.
- Name (required) - the name to use for identifying this specific Extension MBean Provider within the configuration. This name does not have to be related to the actual MBean type or name itself, since this name only identifies the Extension MBean Provider configuration data.
-
Classpath (optional, but recommended) - This allows the classloader used by the MBeanFactory to locate the classes and XML MBean Descriptor file, if your code is not already placed in one of the default locations on the server's classpath (such as the
-
Figure 7 shows the Extension MBean Provider, named
VoterMBeanProvider, being defined for this example. Our example MBean code and WebSphere XML MBean Descriptor file are stored in the JAR file located at/build/newmbean/newmbean.jar.
Figure 7. Defining a new ExtensionMBeanProvider
- Select Apply to store this new Extension MBean Provider definition in your temporary configuration workspace. You can Save the definition from your temporary workspace to the master configuration repository after applying it, or you can wait until you have completed defining the Extension MBean that is supplied by this provider.
- Once the Extension MBean Provider definition has been applied (whether saved to the master repository or not), it will show up in the Extension MBean Providers page for this server (Figure 8). You can now create the actual MBean definition. To do this, click on the link for the newly defined provider to return to the configuration page.
Figure 8. The Extension MBean Providers page after a new provider has been defined
- Figure 9 shows the Extension MBean Provider page for the new Voter MBean Provider. Select the ExtensionMBeans link at the bottom of the page to define the Voter MBean that this provider supports.
Figure 9. The Voter Extension MBean Provider page
- The list of Extension MBeans that displays will initially be empty. Select New to define a new MBean for this provider. See Figure 10.
Figure 10. Configuring the Voter Extension MBean
-
An Extension MBean definition requires two pieces of information:
-
descriptorURI - the location, relative to the provider classpath, where the MBean XML Descriptor file is located. Since we will put the descriptor file in the root of our JAR file, the value for our example is simply the name of the file,
VoterMBean.xml. - type - the type to be used for registering this MBean. This must match the type declared in the MBean descriptor file which, in our example, is Voter .
-
descriptorURI - the location, relative to the provider classpath, where the MBean XML Descriptor file is located. Since we will put the descriptor file in the root of our JAR file, the value for our example is simply the name of the file,
- Select Apply to add the new MBean definition to your temporary configuration workspace, and then Save the change to the master configuration repository. Once the changes have been saved, the definition of the new Extension MBean should look like Figure 11.
Figure 11. The new Voter Extension MBean
-
The result of adding the new Extension MBean Provider and Extension MBean definition will be an entry in the
server.xmlfile that looks like the following:
Figure 12. Server.xml fragment
Now that the configuration data for the Extension MBean Provider and Extension MBean have been added, the code that calls the WebSphere MBeanFactory to activate this MBean will not need to make sure that the XML MBean descriptor file is in the classpath for the process. However, there still needs to be code on the server that calls the
activateMBean
method in the first place. There are several possible approaches for activating the Extension MBean:
- You could simply put it in your J2EE application code and wait until an application client request first comes in for your application.
- You could employ a startup servlet or startup bean with your application, so that the Extension MBean is activated when your application is started.
- You can also use a WebSphere CustomService to extend the server run time itself with a piece of code that activates the MBean. In fact, the CustomService could even be the implementation of your MBean if that arrangement fits your situation.
The next section illustrates an example of using a CustomService to activate the Extension MBean.
CustomServices and JMXManageable interfaces
A CustomService is a piece of code that extends the WebSphere Application Server run time, regardless of whether this piece of code has a JMX MBean to represent its management interface. CustomServices were supported in WebSphere Application Server, Version 4.0, and have been enhanced for easier JMX support in V5. To develop a CustomService, you need to create a class that implements the
com.ibm.websphere.runtime.CustomService
interface. This interface has two methods:
-
initialize- called by the server run time during startup -
shutdown- called by the server run time during normal shutdown processing.
The CustomService interface is very simple, as illustrated below.
public interface CustomService {
/**
* The initialize method is called by the application server runtime
* during server startup. The Properties object passed in on this
* method must contain all configuration information necessary for
* this service to initialize properly.
*/
void initialize(java.util.Properties configProperties)
throws Exception;
/** * The shutdown method is called by the application server runtime
* when the server begins it shutdown processing.
*/ void shutdown() throws Exception;
}
When you add the definition of your CustomService to a server configuration, the server run time will call the initialize method of your CustomService during server startup (call the shutdown method of your CustomService during server shutdown). Your CustomService code effectively becomes an extension of the server run time.
Using a CustomService is a good way to add an MBean to the system. The logic in a CustomService is executed during server startup and can activate an MBean without requiring some external application client to make a request to the server. To make the use of JMX even easier for CustomServices, WebSphere Application Server V5 supports the JMXManageable interface. If the class that implements the CustomService interface also claims to implement the JMXManageable interface, then much of the loading and activating of your MBean will be automatic.
To set up a CustomService:
- First, you must define the new CustomServices. On the WebSphere Application Server admin console, select the Custom Services link from the Additional Properties section (Figure 12).
- Select New on the Custom Services page to create a new CustomService definition. Figure 13 shows the CustomService configuration settings for this example.
Figure 12. Link to Custom Services definition in admin console server page
Figure 13. New Custom Service Definition
-
The important configuration values for Custom Services:
- Startup - very important; unless this box is checked, the Custom Service will not be initialized during server startup.
- External Configuration URL - optional URL to a file that contains configuration data for this Custom Service. This data can be in any format you want, depending on the implementation of your Custom Service logic.
- Classname (required) - Provides the fully-qualified package name and class name for the class that implements the CustomService interface. The server will load this name during startup and invoke this name to initialize.
- Display Name (required) - Identifies the configuration of this service.
- Description - Any text that is useful to distinguish this Custom Service from others.
- Classpath (required) - The location of the JAR file containing the implementation code for this Custom Service.
- Select Apply to add this new Custom Service definition to the configuration, then Save to save the change to the master configuration repository. When you return to the Custom Services page for this server, you should see a new Custom Service, as shown in Figure 14.
Figure 14. Custom Services Page
Example code: Distributed WebSphere Extension MBean
The configuration for our CustomService and Extension MBean is now complete. It is now time time to look at the implementation code.
1 package wsdd.extmbean.voter;
2
3 import java.util.Properties;
4
5 import com.ibm.websphere.management.JMXManageable;
6 import com.ibm.websphere.runtime.CustomService;
7
8 public class CustomServiceMBean implements CustomService, JMXManageable
9 {
10 private int age = 21;
11
12 // CustomService methods
13 public void initialize(Properties props) throws Exception
14 {
15 System.out.println("CustomServiceMBean.initialize("+props+")");
16 }
17 public void shutdown()
18 {
19 System.out.println("CustomServiceMBean.shutdown()");
20 }
21
22 // JMXManageable methods
23 public String getType()
24 {
25 System.out.println("CustomServiceMBean.getType()"); return "Voter";
26 }
27 public Properties getMBeanProperties()
28 {
29 System.out.println("CustomServiceMBean.getMBeanProperties()");
30 Properties props = new Properties();
31 props.put("Identification", "voter1");
32 return props;
33 }
34
35 // MBean attributes
36 public String getIdentification()
37 {
38 System.out.println("CustomServiceMBean.getIdentification()");
39 return "voter1";
40 }
41 public int getAge()
42 {
43 System.out.println("CustomServiceMBean.getAge()");
44 return age;
45 }
46 public void setAge(int age)
47 {
48 System.out.println("CustomServiceMBean.setAge("+age+")");
49 this.age = age;
50 }
51
52 // MBean operations
53 public void setBallots(String agent, String[] names)
54 {
55 System.out.println("CustomServiceMBean.setBallots() agent: "+ agent);
56 System.out.println("CustomServiceMBean.setBallots() ballot names"+ names);
57 }
58 }
Here's a general explanation of the code flow:
- Lines 1 through 10: package, import, and class declaration statements.
-
Lines 12 through 21: implement the two methods defined by the
CustomServiceinterface. In this example, they simply print out a trace to show that the code is actually executed during server startup and shutdown. -
Lines 22 through 33: implement the two methods defined by the
JMXManageableinterface. The type of this MBean has been declared asVoterso that is what thegetTypemethod returns. -
Lines 35 through 40: implement the getter method for the read-only
identificationMBean attribute declared in the MBean Descriptor shown earlier. -
Lines 41 through 50: implement the getter and setter methods for the read-write
ageattribute. -
Lines 52 through 57: implement trivial logic for the
setBallotsoperation declared for this MBean.
After this code is compiled and packaged in the JAR file (located at
/build/newmbean/newmbean.jar
, as specified in the example configuration), start up the server that is configured to include this extension. After the server has completed initialization, look in the server's
SystemOut.log
file. You should see the following trace from the CustomService initialization:
[3/11/03 23:03:04:127 CST] 6dbdc7b6 JMSRegistrati A MSGS0601I: WebSphere Embedded Messaging has not been installed
[3/11/03 23:03:07:712 CST] 6dbdc7b6 SystemOut O CustomServiceMBean.initialize({})
[3/11/03 23:03:07:712 CST] 6dbdc7b6 SystemOut O CustomServiceMBean.getType()
[3/11/03 23:03:07:712 CST] 6dbdc7b6 SystemOut O CustomServiceMBean.getMBeanProperties()
[3/11/03 23:03:07:873 CST] 6dbdc7b6 JMXSoapAdapte A ADMC0013I: SOAP connector available at port 8880
Now, execute the
wsadmin
WebSphere administrative scripting program to prove that the system has been extended with a new MBean, and to manage that MBean using WebSphere administration tools. From the
wsadmin
prompt, enter:
wsadmin>
An
ObjectName
for your Voter Extension MBean should be returned:
"WebSphere:cell=bluehill,Identification=voter1,name=Voter Service, type=Voter,node=bluehill,process=server1,mbeanIdentifier=..."
Store that
ObjectName
in a Tcl variable:
wsadmin>
set voter [$AdminControl queryNames *:*,type=Voter]
You should see the same long
ObjectName
returned from this command as the previous one. Now, check to see what the
wsadmin
built-in
$Help
function can tell us about this new MBean. Enter:
wsadmin>
You should see something like the following:
Name: "WebSphere:cell=bluehill,Identification=voter1,name=Voter Service,mbeanIde ntifier=cells/bluehill/nodes/bluehill/servers/server1/server.xml#CustomService_1 ,type=Voter,node=bluehill,process=server1" Description: null Class name: javax.management.modelmbean.RequiredModelMBean AttributeTypeAccess identification java.lang.String RO ageint RW Operation java.lang.String getIdentification() int getAge() void setAge(int) void setBallots(java.lang.String, [Ljava.lang.String;) Notifications jmx.attribute.changed Constructors |
What this means is that the meta information about the MBean declared in the MBean Descriptor file was successfully parsed and registered with the system. Note that the identification attribute is read-only and the age attribute is read-write. Now let's manipulate the live MBean executing in the server.
wsadmin>
$AdminControl getAttributes $voter
You should see the two attributes for the MBean:
{age 21}
{identification voter1}
To alter an attribute in the running memory of the MBean dynamically:
wsadmin>
Check that the change took effect:
wsadmin>
$AdminControl getAttribute $voter age
33
You can continue to use
wsadmin
to manage the new Extension MBean. Look in the
SystemOut.log
file again to see the traces from the various MBean implementation methods being called as a result of
wsadmin
commands:
[3/11/03 23:27:07:623 CST] 59abc7a4 SystemOut O CustomServiceMBean.getIdentification()
[3/11/03 23:27:07:623 CST] 59abc7a4 SystemOut O CustomServiceMBean.getAge()
[3/11/03 23:29:47:262 CST] 59a8c7a4 SystemOut O CustomServiceMBean.getAge()
[3/11/03 23:29:47:613 CST] 59c2c7a4 SystemOut O CustomServiceMBean.setAge(33)
[3/11/03 23:31:02:711 CST] 59abc7a4 SystemOut O CustomServiceMBean.getAge()
You have now extended the WebSphere Application Server V5 administration infrastructure with a new MBean that was implemented as a server extension CustomService. There are many directions you can go in from here, enhancing the features of this Extension MBean.
In this article, we covered the details involved in extending the WebSphere administrative system with your own JMX MBean. Whether the new administrative logic is associated with a J2EE application or some other server extension, such as a CustomService, the use of JMX by WebSphere Application Server V5 makes the system accessible and easy to extend in a standard way.
Please continue to look for other articles in this series on WebSphere Application Server administration. Planned future topics include:
- How to use the WebSphere 5.0 ConfigService APIs.
- How to programmatically install and manage your applications.
-
How to use Jython as the scripting language for
wsadmin.
Let us know what subjects you would like to see covered in a future article.
| Name | Size | Download method |
|---|---|---|
| newmbean.ZIP | 0.1 MB | FTP |
Information about download methods
-
WebSphere Application Server V5 InfoCenter
- WebSphere Application Server javadoc
- JMX javadoc (from Tivoli's TMX4J JMX implementation)
- WebSphere MBean javadoc
-
Java Management Extensions home page
-
Tivoli TMX4J JMX implementation
-
JMX books:
Leigh Williamson is a STSM Software Architect for IBM. As System Management Architect for WebSphere, he currently leads the evolution of WebSphere Application Server product management capabilities. He can be reached at leighw@us.ibm.com.
Comments (Undergoing maintenance)





