The XMOJO Project
<< Prev 4. Customer MBean Creation Next >>

 Creating an MBean for Customer 

Let us define the management information that will be required by a customer.  After defining the management information, we will proceed with choosing an appropriate MBean (Standard or Dynamic) and then create the MBean.

 Defining the management information 

A customer would like to add an item to his cart, update an item, or delete an item from his cart.  The ShoppingCart application provides methods for adding an item, updating an item, or deleting an item.  So, the MBean we are going to define should consist of the operations, addItem, updateItem, and deleteItem.   Also, a customer would like to know the number of items purchased and the total price of the purchased items.  The ShoppingCart class contains methods getTotalItemCount and getTotalPrice.  These information can be expressed as attributes in the MBean.

The below given tables summarize the management information that our MBean will expose

Attribute Information
Attribute Name
Attribute Type
Attribute Access
Attribute Description
TotalItemCount java.lang.Integer
read only
The total number of items purchased
TotalPrice java.lang.Integer
read only
The total price of the purchased items


Operation Information
Operation Name
Signature
Parameters
Operation Description
addItem void String petName, Integer quantity
To add a new item to the cart
updateItem void String petName, Integer quantity To update an item
deleteItem void String petName To remove an item from the cart

Since the above management information is going to be static, we can write a Standard MBean, which is simpler and most appropriate.

 Creating the MBean 

A StandardMBean should implement its own MBean interface. Let the name of the StandardMBean class you are going to create be Cart.  The name of the management interface should follow the lexical naming pattern of StandardMBean.  So, the interface name should be CartMBean.

The definition of the interface is shown below :

public abstract interface CartMBean
{
      public Integer getTotalItemCount()
throws Exception ;

      public 
Integer getTotalPrice() throws Exception ;

      public void addItem(
String petName , Integer quantity) throws Exception ;

      public void updateItem(
String petName , Integer quantity) throws Exception ;

      public void deleteItem(
String petName) throws Exception ;
}

We will create a class Cart which implements the above interface (CartMBean).  The implementation details are explained for the attribute TotalPrice and the operation addItem.

The ShoppingCart application itself provides all the methods.  It is enough if we can invoke those methods at appropriate places.  The ShoppingCartApplication class contains a static method getShoppingCartReference which returns an instance of ShoppingCart.  Using this ShoppingCart instance, appropriate methods are invoked as shown below:

import tutorials.application.shoppingcart.ShoppingCartApplication;
import tutorials.application.shoppingcart.ShoppingCart;


public class
Cart implements CartMBean
{
     
private ShoppingCart cart = null;
 

      public Integer getTotalItemCount() throws Exception { ... }

      public 
Integer getTotalPrice() throws Exception {
                cart = ShoppingCartApplication.getShoppingCartReference();
         if (cart == null)
              throw new Exception("null reference got")
;
         return cart.getTotalPrice();
      }


      public void addItem(
String petName , Integer quantity
throws Exception {
                 cart = ShoppingCartApplication.getShoppingCartReference()
;
         if (cart == null)
              throw new Exception
("null reference got");
         cart.addItem(petName , quantity)
;
      } 

      public void updateItem(
String petName , Integer quantity)
throws Exception
      {
        ...

      }


      public void deleteItem(
String petName
throws Exception { ... }
}

Download Cart.java to see the complete implementation details.

<< Prev Home Next >>
Adding JMX Manageability
Shop Keeper MBean Creation