java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4091
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Using Custom MBeans to Extend the GlassFish Administration System E-mail
User Rating: / 19
PoorBest 

This Tech Tip reprinted with permission by java.sun.com

One of the areas significantly enhanced in Java 2 Platform, Standard Edition (J2SE) 5.0 is monitoring and management. Among the key features J2SE 5.0 introduced is an instrumented Java virtual machine (JVM) and a java.lang.management API for monitoring and managing the JVM. The API enables local and remote access to a variety of JVM-related information such as memory usage, thread state, class loading activity, and garbage collection statistics. This enhanced and integrated support for management and monitoring can be used by any J2SE 5.0-based administration application.

GlassFish and MBeans

One tool that takes advantage of these capabilities is GlassFish, an open source application server implementation of Java EE 5. The GlassFish application server leverages all of the J2SE 5.0-based monitoring and management enhancements in its administration infrastructure. This includes the use of the platform MBean server to register application server MBeans. Management Beans (or "MBeans") are Java objects that follow design patterns conforming to the Java Management Extensions (JMX) specification -- they are used for instrumenting resources. This tip assumes that you're familiar with the basics of JMX. For an introduction to JMX, see the Tech Tip Understanding JMX Technology.

The platform MBean server, also introduced in J2SE 5.0, is a JMX-based agent built into the JVM, that gives management applications access to MBeans. The agent can be shared by all managed components running in the JVM. Through the platform MBean server, GlassFish can provide a comprehensive and consolidated monitoring and management view of the system and application-specific MBeans. GlassFish uses this built-in agent instead of creating an instance of its own agent (as was required previous to J2SE 5.0). Because application server MBeans are registered in the platform MBean server repository, tools such as the Java Monitoring and Management Console (JConsole) can display information about all resources instrumented by the application server MBeans.

GlassFish and Custom MBeans

Significantly, you can extend GlassFish's administration features by creating and registering your own custom MBeans. Custom MBeans allow dynamic injection of monitoring and management features. In addition, instrumented custom MBean attributes can be persisted so that they survive even if the application server needs to be restarted. One place where custom MBeans demonstrate their utility is in GlassFish's Self Management Framework. This is a rule-based event management system, where users can add custom actions, implemented as custom MBeans, to the rules that govern the system.

Example: Extending Monitoring and Management Capabilities

This tip presents an example that uses custom MBeans to extend GlassFish's monitoring and management features. The example extends a Java EE application that processes data that it gets periodically from a web service. The administrative extension is to add an event to be monitored and an action for the application to take in response to that event.

The event is an update to a list of email addresses. The action is to send emails to the updated list of email addresses. In this scenario, a custom MBean, which is also a client of the web service, would update the list of email addresses after polling the web service. However, to simplify things in this example, the string of email addresses is an attribute of the custom MBean. A user directly modifies the list using JConsole. When the attribute is modified, a listening custom MBean is notified. The listening custom MBean then processes the event by sending emails to the updated list of email addresses (that is, the updated value of the attribute).

A sample archive accompanies this tip. It contains the source code and compiled classes for the example. It also includes javadoc for the classes used in the example.

Step 1: Setup

Let's start by doing some initial setup. If you haven't already done so, download GlassFish from the GlassFish Downloads page. Then set the following environment variables:

  • GLASSFISH_HOME. This should point to where you installed GlassFish (for example C:\Sun\AppServer)
  • JAVA_HOME. This should point to the location of JDK 5.0 on your system.

Step 2: Start GlassFish

Start GlassFish by entering the following command:

      <GF_install_dir>/bin/asadmin start-domain domain1

where <GF_install_dir> is the directory in which you installed GlassFish.

Step 3: Download the sample archive:

Download the sample archive for the tip and extract its contents:

   jar xvf ttmar2006custmbean.jar

After you extract the contents, you should see a META-INF directory, a techtip directory, and a javadoc directory. The techtip directory contains the source files and compiled classes for the example.

Step 4: Create the custom MBeans

This example uses two custom MBeans, one for the event source, and one to listen for the event (the event "sink"). You can find the source code for the two custom MBeans in the techtip/src directory.

A custom MBean requires an MBean interface and an MBean implementation class. The custom MBean interface for the event source is EventSourceCustomMBean, and its implementation class is EventSourceCustom. The custom MBean interface for the event sink is EventSinkCustomMBean and its implementation class is EventSinkCustom.

After you create the source files for the custom MBeans, you can compile the files into the com.example.mbeans package structure (this is optional -- the compiled classes are in the sample archive). To compile the files, enter the following:

   javac -d . EventSourceCustom.java EventSourceCustomMBean.java 
   javac -d . EventSinkCustom.java EventSinkCustomMBean.java 

When you compile the EventSinkCustom class, you need to make sure that the files mail.jar and activation.jar are in your classpath.

Step 5: Copy the Custom MBean classes

Copy the com/example/mbeans directory and it contents to the MBean classloader directory. The MBean classloader directory is <domain>/applications/mbeans, where <domain> is the domain where the application is deployed. For this example, assume that the application is deployed in the default GlassFish domain, domain1. So copy the com/example/mbeans directory and its contents to <GF_install_dir>/glassfish/domains/domain1/applications/mbeans. This will create the /com/example/mbeans directory and copy the custom MBean class and interface to <GF_install_dir>/glassfish/domains/domain1/applications/mbeans.

Step 6: Deploy the Event Source Custom MBean

Deploy the event source custom MBean, EventSourceCustom, as follows (the command should be entered on one line):

   <GF_install_dir>/bin/asadmin create-mbean --user admin 
   --passwordfile password.txt --port 4848 
   com.example.mbeans.EventSourceCustom  

This registers EventSourceCustom in the platform MBean server.

You can verify that the custom MBean has been deployed by issuing the following command (the command should be entered on one line):

   <GF_install_dir>/bin/asadmin list-mbeans --user admin 
   --passwordfile password.txt  

In response, you should see the following:

   com.example.mbeans.EventSourceCustom 
   user:impl-class-name=com.example.mbeans.EventSourceCustom,
   name=com.example.mbeans.EventSourceCustom Enabled
   Command list-mbeans executed successfully.

Step 7: Deploy the Event Sink Custom MBean

Deploy the event sink custom MBean, EventSinkCustom, as follows (the command should be entered on one line):

   <GF_install_dir>/bin/asadmin create-mbean --user admin 
   --passwordfile password.txt --port 4848 --objectname 
   "user:impl-class-name=com.example.mbeans.EventSinkCustom, 
   name=custom-event-sink" 
   --attributes HostName=<yourmailserver>:Id=<yourId>:
   Password=<yourpw> com.example.mbeans.EventSinkCustom

Notice the HostName, Id, and Password parameters in the command. These parameters need to be specified for connection to a mail server. The parameters are attributes of the custom MBean. Replace <yourmailserver> with the name of your mail server, <yourId> with your user identification, and with your password.

As mentioned earlier, instrumented custom MBean attributes can be persisted so that they survive even if the application server needs to be restarted. So in specifying the parameters in the command, it configures the runtime parameters of the custom MBean. However after the MBean is created, the parameters persist across server restarts.

Again, you can verify that the custom MBean has been deployed by issuing the following command (the command should be entered on one line):

   <GF_install_dir>/bin/asadmin list-mbeans --user admin 
   --passwordfile password.txt 

In response, you should see the following:

   com.example.mbeans.EventSourceCustom 
   user:impl-class-name=com.example.mbeans.EventSourceCustom,
   name=com.example.mbeans.EventSourceCustom Enabled
   com.example.mbeans.EventSinkCustom 
   user:impl-class-name=com.example.mbeans.Event
   SinkCustom, name=custom-event-sink Enabled
   Command list-mbeans executed successfully.      

Step 8: Update the email list

In this example, you use JConsole to update an attribute of the custom MBean that contains a string of email addresses.

Start JConsole by issuing the following command:

   <JAVA_HOME>/bin/jconsole

When you start JConsole, you should see a connection dialog box with tabs for local, remote, and advanced JMX connections. Click the Remote tab, and enter the appropriate host (localhost), port (GlassFish's JMX connection server listens at port 8686), user name (admin), and password (adminadmin) values.


Image

Click the Connect button in the display for the remote tab. This connects to the platform MBean server using an RMI connector. In the subsequent Connection window, click on the MBeans tab. Expand the user branch in the MBean tree in the left pane of the window. Continue expanding the branches below the user branch until you see the server entry below com.example.mbeans.EventSourceCustom. Then click on the server entry. You should see information about the MBean in the Info tab in the right pane of the window.


Image

Click the Attributes tab in the right pane of the window. Then enter your email address in the value field for the attribute EmailRecipientsString.


Image

The event source custom MBean sends a notification when you change the EmailRecipientsString attribute. It listens for the notification as defined by the AttributeChangeNotification class, and fetches the new value of this attribute. Here's the code in EventSinkCustom that takes these actions:

   AttributeChangeNotification attrnotif = 
       (AttributeChangeNotificationnotif;

   String newEmailIdStr = (String)attrnotif.getNewValue();

The event sink custom MBean then sends email to the updated addresses.

Step 9: Check for the email

Check your email to verify that the listening custom MBean sent an email to your address. Your should receive the following message:

This message has been generated and sent as part of the Glassfish Techtip sample example execution

For further information, see the following:

About the Author

Nandini Ektare is a member of Sun Java System Application Server Development group at Sun Microsystems Inc. Her areas of interest include JMX and Java EE.

Kedar Mhaswade is a staff engineer at Sun Microsystems Inc. He is a member of the Expert Group for JSR 003: Java Management Extensions. His areas of interest include open source software, JMX, and Java EE.

Copyright (c) 2004-2005 Sun Microsystems, Inc.
All Rights Reserved.


 Related Tips

 

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.