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
This tutorial covers the following topics:
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.
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. | ||
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.
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.
|
| 3. |
Downloaded and unzipped the
jmxobe skeleton workspace
into a directory on your local machine.
|
| 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\.
|
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.
|
| 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 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
|
| 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.
|
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:
|
|||||||||
| 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:
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.
|
|||||||||
To create the MBean management interface and extend the Web application, perform the following steps:
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: Select Project Properties.
|
| 2. |
In the Project Properties window, select Dependencies tree node. Check the MBean.jpr check box in the Project Dependencies list: Click OK. Note: Do not close the Project Properties window.
|
| 3. | In the Project Properties window, select Libraries and
click on Add Jar ... 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.
|
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: Select New.
|
||||||||||||
| 2. |
In the New Gallery, expand the General node and select the Deployment Profiles category. Select JAR File in Items: 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:
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:
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:
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.
|
||||||||||||
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. |
| 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 |
Place
the cursor over this icon to hide all screenshots.