Legal | Privacy
Managing Resources and Applications with JMX

Managing Resources and Applications with JMX

This tutorial covers how to write a J2EE Management Bean (MBean) and deploy it with an existing J2EE Web application that uses property values, provided by the MBean, to control the visual appearance of the displayed data. The application and MBean are deployed to an Oracle Application Server Containers for J2EE (OC4J) environment by using the administrative interface provided for the stand-alone OC4J instance.

Approximately 1 hour

Topics

This tutorial covers the following topics:

Configuring Oracle Application Server Containers for J2EE (OC4J)

Exploring the Administrative MBean Interface (JMX Management Console)

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 from the EMP table in the SCOTT schema of an Oracle 10g database. This tutorial provides the steps to:

Explore the OC4J Web administration interface for MBeans, known as the JMX Management Console

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

Deploy the MBean and a modified Web application to a stand-alone OC4J instance

Examine and set the MBean properties using the JMX Management Console provided by the OC4J administrative Web interface
Execute the Web application to view the altered visual appearance, after applying changes to the MBean property values at run time with the JMX Management Console.

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 JMX Management Console, which is provided by the OC4J administration Web interface. The administrator examines and changes the MBean properties using the JMX Management Console. 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 the OC4J administration interface

Examine and change the MBean properties in the JMX Management Console

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 in the JMX Management Console.

Back to Topic List

Prerequisites

Before starting this tutorial, you should do the following:

1.

Install Oracle JDeveloper 10g (10.1.2) into a directory D:\JDeveloper\JDev1012.

 

2.

Install Oracle 10g database software including the default seeded database with the service name ORCL. The database instance and listener should be started. The SCOTT schema should have the password tiger.

3.

Install the Java 2 Software Development Kit 1.4.2 (JSDK) or higher on the C: drive. The PATH system environment variable should include the C:\j2sdk1.4.2_x\bin directory as the entry in the path.

Note: Alternatively, you can use the JDK software that comes with JDeveloper in the D:\JDeveloper\JDev1012\jdk\bin directory.

 

4.

Unzip the Oracle Application Server Containers for J2EE (OC4J) 10.1.3 software into the D:\oc4j1013 directory.

 

5.

Right-click this jmxobeapp.zip link to download and save the link or target to a temporary directory. Unzip the contents of the file to the D:\JDeveloper\JDev1012\jdev\mywork directory.

Note: A sub-folder called jmxobe with a JDeveloper workspace and its related project folders and files is created.

 

Back to Topic List

Configuring Oracle Application Server Containers for J2EE

To configure the stand-alone 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 stand-alone OC4J instance and set the administrator password:

D:
cd \oc4j1013\j2ee\home
d:\jdk1.4.2\bin\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:

d:\jdk1.4.2\bin\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:

04/10/21 13:59:52 Oracle Containers for J2EE 10g (10.1.3.0.0) - Developer Preview initialized  

 

3.

To access the OC4J administration page, 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 the OC4J administration page, enter the User Name ias_admin and the Password welcome.

Click Login.

The OC4J Administration home page appears when you have successfully logged in.

 

5.

On the OC4J: Home page, click Administration to start creating a data source:

 

6.

On the Administration page, click the icon in the Go To Task column for the row with the task name JDBC Resources:

 

7.

 

On the JDBC Resources page in the Connection Pools section, click the Example Connection Pool link:

 

8.

On the Home tab of the Connection Pool: "Example Connection Pool" page, enter the following field values:

URL: jdbc:oracle:thin:@localhost:1521:orcl

Username: scott

Select the User Cleartext Password option, and then enter tiger in the Password field.

Click Apply.

 

9.

To return to the JDBC Resources page, click the JDBC Resources link on the Connection Pool: "Example Connection Pool" page:

 

10.

On the JDBC Resources page, click Create in the Data Sources section to create a new data source:

 

11.

On the Create Data Source - Application & Type page, select the following field values:

Application: default

Data Source type: Select the Managed Data Source option.

Click Continue.

 

12.

On the Create Data Source - Managed Data Source page, enter the following field values:

Name: QueryDataSource

JNDI Location : jdbc/QueryDataSource

Transaction Isolation Level: Local Transactions Only
Connection Pool: Example Connection Pool

Click Finish.

The JDBC Resources page is displayed with a Information message confirming the creation of the new data source:

Note: The Name column contains the QueryDataSource entry in the first row of the Data Sources section.

 

13.

On the JDBC Resource page, you can verify that a successful database connection can be made with the new data source. Click the icon in the Test Connection column for the QueryDataSource table entry:

 

14.

On the Test Connection: "QueryDataSource" page, click Test:

If the connection is successfully established, the following line is displayed in the Information section at the top of the JDBC Resources page:

Connection to "QueryDataSource" established successfully

 

15.

Click the OC4J: home link to return to the OC4J: Home Administation page:

The browser displays the Administration page of the OC4J: Home instance:

 

Back to Topic List

Exploring the Administrative MBean Interface (JMX Management Console)

To explore the adminstrative MBean interface in the OC4J JMX Management Console, perform the following steps:

1.

On the Administration page of the OC4J: Home instance, click the Go To Task icon next to the Task Name entry called JMX MBean Browser:

Place the cursor over this icon to see the image

 

2.

On the JMX Management Console page, you can locate the MBean interface for the new data source. To do so, accept MBean Name in the Search option, enter QueryDataSource in the find text field, and then click Find:

Place the cursor over this icon to see the image

Note: The JMX Management Console page contains:

A management tree structure for navigating to system-supplied MBeans on the right.

A section for the Attributes and Operations pages for the current selected MBean. Attributes can be viewed or altered on the Attributes page, and operations can be invoked on the Operations page.

 

3.

On the JMX Management Console page, the management tree is expanded to reveal the location of the QueryDataSource MBean. The section on the right contains the information about the data source attributes. To test a database connection by using the MBean interface for the data source, click the Operations link:

Place the cursor over this icon to see the image

Note: If you explore the Attributes page, attributes with text fields in the Value column can be modified. Otherwise, the attribute is a read-only value (that is, it cannot be modified with the JMX Management Console).

 

4.

On the Operations page for the QueryDataSource MBean, scroll until you see the testConnection entry in the Name column. Click the first testConnection link:

Place the cursor over this icon to see the image

 

5.

n the Parameters section on the Operation Details page, enter the following query in the Value column for the sqlStatement:

select * from dual

Click Invoke.

The informational response returned indicates that the operation executed successfully:

Click Return.

 

6.

In the JMX Management Console, click the OC4J: Home link to return to the Administration page:

The Administration page appears:

 

Back to Topic List

Deploying and Testing the Web Application

To deploy and test the Web application, perform the following steps:

1.

Double-click the JDev1012 desktop icon to start JDeveloper 10g with the BPEL Designer extension:

 

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:\JDeveloper\JDev1012\jdev\mywork\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:\JDeveloper\JDev1012\jdev\mywork\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 Input node in the Common tree. Enter oracle.obe.jmx.mbeans in the Default Package field:

Note: Do not close the Project Properties window.

 

6.

In the Project Properties window under the Profiles > Development node, select the Libraries node:

Click New, under the Selected Libraries field.

 

7.

In the Create Library window, enter the following field values:

Library Name: JMX

Location: User Libraries

Class Path: D:\JDeveloper\JDev1012\j2ee\home\lib\jmxri.jar

Note: You can click Edit next to the Class Path field to locate, and add the D:\JDeveloper\JDev1012\j2ee\home\lib\jmxri.jar entry to the new JMX library.

Click OK.

 

8.

the Project Properties window, the new JMX library appears in the Selected Libaries field:

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

 

9.

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

Click New.

 

10.

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

Click OK.

 

11.

In the Create Java Interface dialog box, enter FormatMBean in the Name field:

Click OK.

 

12.

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.

 

13.

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

Click New.

 

14.

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

Click OK.

 

15.

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

Name: Format

Extends: java.lang.Object

Click OK.

 

16.

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

 

17.

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

Click OK.

 

18.

The Format.java source code contains the following skeleton class structure:

 

19.

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()
  {
  }
  :
}    

 

20.

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.

 

21.

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

Select Make.

 

22.

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 in the Common tree node. Select 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 in the Profiles > Development tree node. Locate and select the JMX entry in the Available Libraries area:

Click the Add icon.

 

4.

After adding the JMXlibrary 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)
  {
    e.printStackTrace();
  }

 

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)
  {
    e.printStackTrace();
  }

 

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)
  {
    Date now = new Date(); 
    System.out.println("\n*** ERROR " + now);
    e.printStackTrace(new PrintWriter(System.out));
    System.out.println("*** /ERROR\n" + now);
  }

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:\JDeveloper\JDev1012\jdev\mywork\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: