Monitoring and Managing JMX Instrumented Applets with JConsole

In this blog entry I would like to show how Java applets can be instrumented using JMX, run in a web browser with the Mustang Java Plug-in and monitored and managed using Mustang JConsole.

The following steps will take you through an example that shows all the necessary implementation and configuration steps required to achieve this.

NOTE: I have successfully run this example on Firefox version 1.5.0.4 and Internet Explorer version 6.0 with the latest Mustang Java Plug-in.

Instrumenting the Java applet

I have implemented a very simple applet named InstrumentedApplet whose paint() method prints a message on the web browser's screen. I have also overriden the getAppletInfo() method in order to return the version of the Java Plug-in used to run this applet. I have created a Standard MBean named AppletInfoMBean that exposes for management a getAppletInfo() getter that returns the applet.getAppletInfo() string. In the applet's init() method I create a unique ObjectName for each applet's instance and register its associated AppletInfo MBean in the Platform MBean Server. The applet's destroy() method unregisters the AppletInfo MBean from the Platform MBean Server.

Let's have a look at the source code:

AppletInfoMBean.java:

public interface AppletInfoMBean {
    public String getAppletInfo();
}

AppletInfo.java:

public class AppletInfo implements AppletInfoMBean {
    
    private Applet applet;
    
    public AppletInfo(Applet applet) {
        this.applet = applet;
    }
    
    public String getAppletInfo() {
        return applet.getAppletInfo();
    }
}

InstrumentedApplet.java:

public class InstrumentedApplet extends Applet {
    
    private static AtomicInteger id = new AtomicInteger(1);
    
    private MBeanServer mbs;
    private ObjectName name;
    
    public void init() {
        try {
            mbs = ManagementFactory.getPlatformMBeanServer();
            name = ObjectName.getInstance(
                    "com.example.applet:type=InstrumentedApplet,name=" +
                    id.getAndIncrement());
            mbs.registerMBean(new AppletInfo(this), name);
        } catch (Exception e) {
            System.err.println("Error in applet's init method");
            e.printStackTrace(System.err);
        }
    }
    
    public void destroy() {
        try {
            mbs.unregisterMBean(name);
        } catch (Exception e) {
            System.err.println("Error in applet's destroy method");
            e.printStackTrace(System.err);
        }
    }
    
    public void paint(Graphics g) {
        int width = getSize().width;
        int height = getSize().height;
        g.setColor(Color.orange);
        g.fillRect(0, 0, width, height);
        String message = "This is an example of a JMX instrumented applet";
        FontMetrics fm = g.getFontMetrics();
        g.setColor(Color.black);
        g.drawString(message, (width-fm.stringWidth(message))/2, height/2);
    }
    
    public String getAppletInfo() {
        return "InstrumentedApplet is being run with Java Plug-in " +
                System.getProperty("java.version");
    }
}

Signing the applet's jar file

Compile the source files above and create the InstrumentedApplet.jar jar file. Retrieving the reference to the Platform MBean Server requires Java permissions so the jar file needs to be signed.

Have a look at How to Sign Applets Using RSA-Signed Certificates in order to see all the steps you need to carry out to sign your jar file. For testing purposes I would recommend you to use self-signed certificates instead to avoid the hassle of going through a Certification Authority (CA).

The Java applet's HTML file

This is the HTML file containing the APPLET tag that specifies the applet's class name com.example.applet.InstrumentedApplet and the name of the jar file containing the applet and its resources InstrumentedApplet.jar.

InstrumentedApplet.html:

<applet code=com.example.applet.InstrumentedApplet.class archive=InstrumentedApplet.jar width=400 height=200>
</applet>

Deploy the applet in a web server which has the Mustang Java Plug-in installed

I have used the blogs.sun.com web server to deploy the applet, i.e. I have uploaded to the blogs.sun.com website the InstrumentedApplet.html and InstrumentedApplet.jar files.

Click here to run the applet.

As I used a self-signed certificate to sign the applet's jar file it is likely that the web browser will pop up a window where you will have to explicitly trust the certificate before running the application.

Trust the applet's digital signature

Once the applet is loaded you should see something like this:

JMX instrumented applet loaded

As soon as the applet's init() method is called a new AppletInfo MBean is created and registered in the Platform MBean Server.

Run Mustang JConsole and connect to the web browser's JVM

Run Mustang JConsole in the same machine where the web server is running as follows:

% jconsole

Select the PID with an empty name in the Connection dialog. This PID corresponds to the web browser's JVM. Press the Connect button to enable the management agent in the web browser's JVM and connect to it.

Connection dialog: select PID for web browser's JVM

Go to the MBeans tab and you will see the applet's AppletInfo MBean and its AppletInfo getter showing the Java Plug-in version.

MBean for first applet instance

Click here to run a second instance of the applet.

Come back to JConsole and look at the MBeans tab again. A new AppletInfo MBean representing the second applet instance should appear in the MBean tree.

MBean for second applet instance

Let's unload the first applet now by clicking on the Back button in the web browser's window or tab where the first applet is running or just by entering a new dummy URL in the Address bar.

Come back to JConsole and look at the MBeans tab again. The AppletInfo MBean representing the first applet instance is no longer there because the applet's destroy() method has been called and the AppletInfo MBean has been unregistered from the Platform MBean Server.

Remove MBean for  first applet instance

How to make this work if the web browser runs with a Tiger Java Plug-in

In case your web browser still runs with a Tiger Java Plug-in then you will have to configure the Java Applet Runtime Settings in the Java Control Panel in order to supply the following java runtime parameter -Dcom.sun.management.jmxremote that will explicitly enable the management agent.

Run the Java Control Panel, go to the Java tab and click on the View button in the Java Applet Runtime Settings. Select the JRE your web browser will use to run your Java applets and specify the system property in the Java Runtime Parameters field. Click on the OK button and restart your web browser to make these changes take effect.

Java Applet Runtime Settings

Load this page in your web browser again and click here to run the applet.

Then run Mustang JConsole in the same machine where the web server is running and select the PID with an empty name in the Connection dialog. Press the Connect button to connect to the management agent.

Go to the MBeans tab and you will see the applet's AppletInfo MBean and its AppletInfo getter showing the Java Plug-in version. Verify that the AppletInfo getter returns a string saying that the applet is being run with the Tiger Java Plug-in.

MBean for first applet instance

Summing up, I hope you have encountered this example useful although I know the JMX instrumentation implemented in this applet might be useless from a management standpoint but the aim of this example is not to show what is the useful or meaningful information an applet should expose for management (I let this job to real applet developers) but how an applet can be instrumented using JMX, registered in the Platform MBean Server and monitored and managed using JConsole just by writing a few lines of code.


Feel free to download the resource zip file in attachment and play with it or tailor it to your specific application needs.


Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

lmalvent

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today