Manage Resources and Applications with JMX Quiz Tutorial

Download everything you need to run this tutorial on Windows for free, including:

If you use a different operating system, please visit the Prerequisites for links to the individual downloads.

This tutorial covers how to write a JMX Management Bean (MBean) to provide administrative support for a J2EE Web application. The Web application uses property values, provided by the MBean to control the visual appearance of the data it displays. The Web application and MBean are deployed to an Oracle Containers for J2EE 10g (10.1.3) environment using the supplied Application Server Control management console.

Approximately 1 hour

Topics

This tutorial covers the following topics:

Configuring Oracle Containers for J2EE (OC4J)
Deploying and Testing the Web Application

Creating an MBean Management Interface

Extending the Web Application to Use the Management Interface of the MBean


Place the cursor over this icon to load and view all the screenshots for this tutorial.
Caution: Because this action loads all screenshots simultaneously, response time may be slow depending on your Internet connection.

Note: Alternatively, you can place the cursor over each individual icon in the following steps to load and view only the screenshot associated with that step.

Overview

A Web application running in an OC4J instance queries and displays employee name and salary information. This tutorial provides the steps to:

Explore the MBeans running inside of OC4J using the JMX MBean Browser in Application Server Control

Develop an MBean to provide properties for altering the visual representation of employee data displayed by the Web application

Deploy the MBean and a modified Web application to an OC4J 10g (10.1.3) instance

Examine and set the MBean properties using Application Server Control
Execute the Web application to view the altered visual appearance, after applying changes to the MBean property values at run time with Application Server Control.

Back to Topic List

An existing Java Web application executes a query to retrieve the names and salaries of employees. Initially, the data is formatted using the default font style without color. The following Web page illustrates the information that is presented:

The business uses a JMX-enabled application that interfaces with an MBean, called FormatMBean, which provides an administrator with a way to dynamically change the following properties for formatting the employee information displayed on the Web application page:

SalaryColor: String specifying a color to display a salary value if it is greater than the SalaryThreshold value

SalaryThreshold: Decimal value specifying the salary threshold value

SalarySymbol: String to set the character(s) for the currency symbol
NameBold: Boolean value. If set to true, it causes the employee name to be displayed in a bold font style.
NameItalic: Boolean value. If set to true, it causes the employee name to be displayed in an italic font style.
Note: Setting NameBold and NameItalic to true causes the name to be displayed in both bold and italic styles.
NameColor: String specifying the color that is used to display the employee names

The MBean properties and operations are exposed to the administrator in the Application Server Control management console, which is provided with OC4J. The administrator examines and changes the MBean properties using Application Server Control. As the developer, you use Oracle JDeveloper to:

Develop the MBean logic

Create the Enterprise Archive (EAR) file, which contains the Web application and the associated MBean

As the administrator, you:

Deploy the EAR file using Application Server Control

Examine and change the MBean properties of the application using Application Server Control

You access the Web application page to execute queries of the employee data and view the changes to the visual appearance of the employee data after the MBean properties are changed with Application Server Control.

Back to Topic List

Prerequisites

Before starting this tutorial, you should have done the following:

1.

Installed Oracle JDeveloper 10g (10.1.3) into a directory on your local machine.

 

2.

Unzipped the Oracle Containers for J2EE 10g (10.1.3) distribution into a directory on your local machine.



For the remainder of this tutorial, this will be assumed to be D:\oc4j1013.

 

3.

Downloaded and unzipped the jmxobe skeleton workspace into a directory on your local machine.



For the remainder of this tutorial, this will be assumed to be D:\jmxobe.

 

4.

Installed the Java 2 Software Development Kit 1.4.2 (JSDK) on the host computer. The PATH system environment variable should include the bin subdirectory of the J2SDK installation.

NOTE: If you are downloaded the full JDeveloper 10.1.3 package for Windows, the JSDK is included. Set your PATH to [jdev_install_dir]\jdk\bin\.

 

Back to Topic List

Configuring Oracle Application Server Containers for J2EE

To configure the OC4J instance and create a data source that is used by the Web application, perform the following steps:

1.

In the command window, install the OC4J instance and set the administrator password:

cd \oc4j1013\j2ee\home
java -jar oc4j.jar -install

When prompted for a password, use the value welcome.

Place the cursor over this icon to see the image

 

2.

To start the OC4J instance while in the D:\oc4j1013\j2ee\home directory, enter the following command in the command window:

java -jar oc4j.jar

Place the cursor over this icon to see the image

The OC4J instance is ready to be used when a message similar to the following appears in the command window:

05/01/06 15:45:21 Oracle Containers for J2EE 10g (10.1.3.0.0) - Developer Preview 3 initialized  

 

3.

To access Application Server Control, start a browser window and enter the following URL:

http://localhost:1810

Place the cursor over this icon to see the image

 

4.

To log in to Application Server Control, enter the User Name admin and the Password welcome.

Click Login.

The Application Server Control OC4J: Home page appears when you have successfully logged in.

 

Back to Topic List

Deploying and Testing the Web Application

A skeleton project with a pre-built Web application has been provided for you. To deploy and test the Web application, perform the following steps:

1.

Launch JDeveloper

 

2.

To open the initial JMX application workspace in JDeveloper, select File > Open:

 

3.

In the Open dialog box, locate the D:\JDeveloper\JDev1012\jdev\mywork\jmxobe directory. Select jmxobe.jws:

Click Open.

If you expand the jmxobe workspace, it contains the following two projects:

App, which is used to create the EAR file for deployment to the Oracle Application Server Containers for J2EE (OC4J) instance

WebApp, which contains the code for the Java Web application to query the EMP table and to format and display the employee name, number, and salary

 

4.

To create the EAR file for deployment, expand the jmxobe > App > resources node in the Applications - Nagivator pane.Then right-click jmxobeapp.deploy and select Deploy to EAR file:

The Log message Deployment window shows the results of the deployment operation. Remember the directory and file name of the EAR file that is created.

The EAR file D:\jmxobe\App\deploy\jmxobeapp.ear is created.

 

5.

Return to the browser window showing the OC4J:Home page with the Administration page visible:

Click the Applications page link.

 

6.

On the Applications page, click Deploy:

 

7.

On the Deploy: Select Archive page, enter D:\jmxobe\App\deploy\jmxobeapp.ear in the Archive Location field in the Archive section.

Note: You can click Browse to navigate to and select the EAR file from the file system directory.

Click Next.

 

8.

On the Deploy: Application Attributes page, enter or select the following field values:

Application Name: jmxobeapp

Parent Application: default

Bind Web Module to Site: http-web-site

Click Next.

 

9.

On the Deploy: Deployment Settings page, click Deploy:

 

10.

Wait for the deployment process to complete successfully:

Click Return.

 

11.

To run the initial Web application, open a new browser window and enter the following URL:

http://localhost:8888/jmxobe/

Click the Query link.

 

12.

The query results window displays the following data:

Note: The employee data is displayed in plain format without color. Clicking the Query link again returns the same data without changing the visual appearance.

 

Back to Topic List

Creating an MBean Management Interface

To create the MBean management interface and extend the Web application, perform the following steps:

1.

On the JDeveloper Applications - Navigator pane, right-click the jmxobe workspace node:

Place the cursor over this icon to see the image

Select New Project.

 

2.

In the New Gallery window, select the Empty Project item from the Projects category:

Place the cursor over this icon to see the image

Click OK.

 

3.

In the Create Project dialog box, enter MBean in the Project Name field:

Note: The last name in the DIrectory Name field uses the same project name.

Click OK.

 

4.

To set the MBean project properties, right-click the MBean node in the Applications - Navigator:

Select Project Properties.

 

5.

In the Project Properties window, select the Libraries node:

Note: Do not close the Project Properties window.

 

6.

Click Add Jar... and select the jmxri.jar file from within your OC4J directory tree (Hint: look in the j2ee\home\lib subdirectory)

Click Open.

 

7.

The JMX library jmxri.jar appears in the list of libraries.

Click OK to close the Project Properties window. Press [Ctrl] + [S] to save the changes to the MBean project.

8.

To create the FormatMBean interface, right-click the MBean node in the Applications - Navigator.

Click New.

 

9.

In the New Gallery window, select the Java Interface item in the General category:

Click OK.

 

10.

In the Create Java Interface dialog box,window, enter the following field values:

Name: FormatMBean

Package: oracle.obe.jmx.mbeans

Click OK.

 

11.

On the Source page of the FormatMBean.java page, define the FormatMBean interface operations inside the inside the braces of the Java interface:

 // Salary attributes 
 public void setSalaryColor(String color);
 public String getSalaryColor();
 public void setSalarySymbol(String symbol);
 public String getSalarySymbol();
 public void setSalaryThreshold(long threshold);
 public long getSalaryThreshold();
 
 // Name attributes 
 public void setNameBold(boolean bold);
 public boolean isNameBold();
 public void setNameItalic(boolean italic);
 public boolean isNameItalic();
 public void setNameColor(String color);
 public String getNameColor();

Press [Ctrl] + [S] to save the changes to the FormatMBean.java file.

 

12.

To create the Format MBean class that implements the FormatMBean interface, right-click the MBean node in the Applications - Navigator.

Click New.

 

13.

In the New Gallery window, select the Java Class item in the General category:

Click OK.

 

14.

In the Create Java Class window, enter the following field values:

Name: Format

Package: oracle.obe.jmx.mbeans

Extends: java.lang.Object

Click OK.

 

15.

With the Format.java file open in the Code Editor Source page, select Tools > Implement Interface:

 

16.

In the Implement Interface window, expand the oracle > obe > jmx > mbeans nodes. Select the FormatMBean interface:

Click OK.

 

17.

The Format.java source code now contains the following skeleton class structure from the interface it implements:

 

18.

On the Source page of the Format.java MBean class, add the following attributes declarations with default values after the opening brace for the class:

public class Format implements FormatMBean
{
  // Attributes of the MBean
  private String salaryColor = "red";
  private long salaryThreshold = 1500;
  private String salarySymbol = "$";
  private boolean nameBold = false;
  private boolean nameItalic = false;
  private String nameColor = "black";
  
  public Format()
  {
  }
  
}    

 

19.

To implement the functionality for Format.java methods, in the Source page modify the code to resemble the following Java source:

  
  public Format()
  {
    System.out.println("-->Format constructor");
  }

  /**
   * Salary methods implementations 
   */
  public void setSalaryColor(String color)
  {
    this.salaryColor = color;
  }

  public String getSalaryColor()
  {
    return salaryColor;
  }

  public void setSalarySymbol(String symbol)
  {
    this.salarySymbol = symbol;
  }

  public String getSalarySymbol()
  {
    return salarySymbol;
  }

  public void setSalaryThreshold(long threshold)
  {
    this.salaryThreshold = threshold;
  }

  public long getSalaryThreshold()
  {
    return salaryThreshold;
  }

  /**
   * Name methods implementation 
   */
  public void setNameBold(boolean bold)
  {
    this.nameBold = bold;
  }

  public boolean isNameBold()
  {
    return nameBold;
  }

  public void setNameItalic(boolean italic)
  {
    this.nameItalic = italic;
  }

  public boolean isNameItalic()
  {
    return nameItalic;
  }

  public void setNameColor(String color)
  {
    this.nameColor = color;
  }

  public String getNameColor()
  {
    return nameColor;
  }

Select File > Save All to save the changes to the Format.java file and the MBean project.

 

20.

To compile the FormatMBean Java interface and Format MBean class, right-click the MBean project node in Applications - Navigator:

Select Make.

 

21.

On the Messages page of the Messages - Log window, verify that the compilation of the Java interface and Java class is successful before you continue.

Compilation error messages, if any, appear on the Compile page. In this case, correct the errors in the source code and select the Make option on the project or file.

 

Back to Topic List

Extending the Web Application to Use the Management Interface of the MBean

To extend the Web Application by modifying the QueryServlet to use the FormatMBean management interface, perform the following steps:

1.

In the JDeveloper Applications - Navigator, right-click the WebApp node:

Place the cursor over this icon to see the image

Select Project Properties.

 

2.

In the Project Properties window, select Dependencies tree node. Check the MBean.jpr check box in the Project Dependencies list:

Place the cursor over this icon to see the image

Click OK.

Note: Do not close the Project Properties window.

 

3.

In the Project Properties window, select Libraries and click on Add Jar ...

Locate the jmxri.jar file from within your OC4J directory tree:

Click Open.

 

4.

After adding the jmxri.jar library to Selected Libraries, click OK to close the Project Properties window:

Press [Ctrl]+[S] to save the changes to the project.

 

5.

 

In the JDeveloper Applications - Navigator, expand the WebApp > Application Sources > oracle.obe.jmx.webapp node. Right-click QueryServlet.java:

Select Open.

 

6.

On the Source page of the Code Editor for QueryServlet.java, to use the JMX Format MBean add the following import statements after the import oracle.obe.jmx.webapp.formatters.*; statement:

import javax.management.*;
import oracle.obe.jmx.mbeans.*;

 

7.

In the QueryServlet class, declare the following instance variables before the private InitialContext m_ic declaration:

  private static final String FORMATMBEAN_NAME = ":name=FormatMBean";

  private MBeanServer m_MBeanServer = null;
  private ObjectName m_FormatMBean = null;

 

8.

In the init() method, to create an MBeanServer and dynamically register the FormatMBean for this application, add the following lines of code after creating the InitialContext object:

  // Create an MBeanServer and dynamically register 
  // the FormatMBean for this application
  m_MBeanServer = MBeanServerFactory.createMBeanServer();
  m_FormatMBean = new ObjectName(FORMATMBEAN_NAME);
  m_MBeanServer.registerMBean(new Format(),m_FormatMBean);

 

9.

In the getSalaryFormatter() method, add the following code to get the salary formatting options from the MBean before the return statement:

  try
  {
    Long threshold = (Long)m_MBeanServer.getAttribute(m_FormatMBean,"SalaryThreshold");
    String color = (String) m_MBeanServer.getAttribute(m_FormatMBean,"SalaryColor");
    String symbol = (String) m_MBeanServer.getAttribute(m_FormatMBean,"SalarySymbol");

    format.setColor(color);
    format.setSymbol(symbol);
    format.setThreshold(threshold.longValue());
  }
  catch(Exception e)
  {
    logException(e);
  }

 

10.

In the getNameFormatter() method, add the following code to get the name formatting options from the MBean before the return statement:


  try
  {
    Boolean italic = (Boolean) m_MBeanServer.getAttribute(m_FormatMBean,"NameItalic");
    Boolean bold = (Boolean) m_MBeanServer.getAttribute(m_FormatMBean,"NameBold");
    String color = (String) m_MBeanServer.getAttribute(m_FormatMBean,"NameColor");

    format.setColor(color);
    format.setBold(bold.booleanValue());
    format.setItalic(italic.booleanValue());
  }
  catch(Exception e)
  {
    logException(e);
  }

 

11.

In the destroy() method, add the following code to deregister and release the MBean instance:

  try
  {
    m_MBeanServer.unregisterMBean(m_FormatMBean);
  }
  catch(Exception e)
  {
    logException(e);
  }

Note: Press [Ctrl]+[S] to save the changes to the QueryServlet source code.

 

12.

To compile the QueryServlet, right-click the QueryServlet node in the Applications - Navigator:

Select Make.

Note: Entering [Ctrl]+[Shift]+[F9] compiles the selected file. Confirm that the compilation is successful by checking the results on the Messages page in the Messages - Log window for the following text line:

Successful compilation: 0 errors, 0 warnings.

Remove any compilation errors and ensure recompilation is successful before continuing with subsequencet steps.

 

Back to Topic List

Redeploying and Testing the Web Application with the MBean Management Interface

To redeploy and test the Web Application with the MBean management interface, perform the following steps:

1.

To create a deployment profile file for the MBean project, right-click the MBean project node in the Applications - Navigator:

Place the cursor over this icon to see the image

Select New.

 

2.

In the New Gallery, expand the General node and select the Deployment Profiles category. Select JAR File in Items:

Place the cursor over this icon to see the image

Click OK.

 

3.

In the Create Deployement Profile - JAR File dialog box, enter FormatMBean in the Deployment Profile Name field:

Click OK.

 

4.

In the JAR Deployement Profiles Properties window, click OK to accept the default settings:

Select File > Save All to save all changes to the MBean project.

 

5.

In the Applications - Navigator, expand the App > Resources node. Right-click jmxobeapp.deploy:

Select Properties.

 

6.

In the EAR Deployment Profile Properties window, select the Application Assembly node. Select the FormatMBean.deploy check box below the MBean.jpr node in the J2EE Modules section:

Click OK. Press [Ctrl]+[S] to save the changes to the deployment profile file.

 

7.

Right-click jmxobeapp.deploy:

Select Deploy to EAR file.

 

8.

In the browser window displaying the Applications page of the OC4J: home page, select the option for the jmxobeapp application:

Click Undeploy.

 

9.

On the Undeploy Application page, click Yes:

 

10.

On the Confirmation page showing that the jmxobeapp has been succesfully undeployed, click Return:

 

11.

On the Administration page of the OC4J: home page, click Deploy:

 

12.

On the Deploy: Select Archive page, in the Archive Location field enter D:\jmxobe\App\deploy\jmxobeapp.ear:

Note: You can click Browse to locate and select the EAR file from the directory in the file system.

Click Next.

 

13.

On the Deploy: Application Attributes page, enter or select the following field values:

Application Name: jmxobeapp

Parent Application: default

Bind Web Module to Site: http-web-site

Click Next.

 

14.

On the Deploy: Deployment Settings page, click Deploy:

 

15.

Wait for the Confirmation page to indicate that deployment is successful:

Click Return.

 

16.

To run the updated Web application, open a new browser window and enter the following URL:

http://localhost:8888/jmxobe/

Click the Query link.

 

17.

The following query response page is displayed:

Note: The values in the Name and Salary columns have their default formating applied. However, clicking the Query link initiates the QueryServlet, which registers the FormatMBean instance in the Application Server J2EE container. By using the OC4J administration interface you can change attributes of the FormatMBean to cause visual format changes to the data displayed by the Web application.

 

18.

In the browser window showing the Applications page for the OC4J: home, click the jmxobeapp link:

 

19.

On the Application: jmxobeapp page, the Home page shows details of the select deployed Web application:

Click the Application Defined MBeans link, under the Related Links heading.

 

20.

On the Application MBeans page, the value jmxobeapp:name=FormatMBean appears in the Name column:

Click the jmxobeapp:name=FormatMBean link.

 

21.

On the MBean:jmxobeapp:name=FormatMBean page, in the Attributes tab page change the following attribute values:

SalaryColor: magenta

SalaryThreshold: 2000

NameBold: true
NameColor: purple

Click Apply Changes.

Note: The MBean:jmxobeapp:name=FormatMBean page is displayed with an Information section containing the following message:

Attributes has been updated successfully.

 

22.

Return to the browser window running the Web application containing the Employees data page:

Click the Query link at the bottom of the page, to requery the employee data.

 

23.

On the resulting Employees Web page, the following format changes have been applied:

The Name column displays employee names in a bold font with a purple color.

The Salary column displays salaries greater than $2,000 in a magenta color.

Note: The changes applied to attributes on the MBean:jmxobeapp:name=FormatMBean administration Web page have been applied by the QueryServlet. This shows how the JMX MBean is used by an administrator to set or get information for an application designed to interact with the MBean.

 

Back to Topic List

In this lesson, you've learned how to:

Install and use Oracle Containers for J2EE 10g (10.1.3).

Explore the OC4J MBeans using the Application Server Control management console

Extend a Web application to use a JMX MBean to obtain settings to use for the visual presentation of application data.

Modify the Web application MBean attributes using Application Server Control .

Deploy and test the Web application and its MBean management interface.

Back to Topic List

To learn more about Oracle Containers for J2EE, refer to the Oracle OTN product page.

For information about JMX, see Java Management Extensions at the Sun Developer Network Web site.

To ask a question about this OBE tutorial, post a query on the OBE Discussion Forum

Back to Topic List

Place the cursor over this icon to hide all screenshots.