9.4 Best Practices

Not all of the lessons we have learned with JMX are easily classified in the lifecycle of an MBean or MBeanServer. This section contains the miscellaneous advice we wanted to present to you, so the subsections here are independent of each other.

9.4.1 MBean Granularity

An MBean should be created to represent a functional component of your application. You should not end up with one MBean per component or class in your application, unless your components are very coarse-grained.

When looking at your application and trying to decide how many MBeans you need for which components and classes, resist the temptation to create an MBean for each component. Step back and look at your application from a functional view, remembering that MBeans can manage across components. Ask yourself what those who are managing this application while it is running will want to manage, check the status of, and reconfigure. What would they say the functional areas of your application are?

Let's look at our tack catalog example. In the tack catalog we had an item, category and catalog, but we developed manageability only for the catalog. Now let's expand the tack catalog for online shopping (remember this is a fictitious and simple example) and see how we chose which components to manage. Now we have the following functions that we need to provide to customers:

  1. Catalog, which allows a customer to

    • List products

    • Check availability of a product

  2. Shopping Cart, which collects items to be purchased from the Catalog and allows them to be purchased and shipped.

  3. Order Status Check, which allows a customer using a browser to check the status of the following items related to an order:

    • Product item number and price

    • Order shipping cost shipment number

    • Ordered product status: in stock, back-ordered, picked, packed, shipped

    • Status dates: picked date, packed date, shipped date, estimated arrival date, back-order date.

  4. Shipper Status Check, which allows a customer using a browser to track the shipment from the warehouse through depots to the delivery trucks .

We have seen an example of a catalog application having the following business operations:

 public interface Catalog  {  public  Category createCategory(String name)  throws  CategoryExistsException;  public  Category getCategory(String name);  public  Map getCategories();  public   void  removeCategory(Category category)  throws  NoSuchCategoryException;  public   void  removeCategory(String name)  throws  NoSuchCategoryException  public   void  classify(Item item, Category category);  public   void  declassify(Item item, Category category);  public   void  increaseInventory(Item item,  int  amount);  public   void  decreaseInventory(Item item,  int  amount)  throws  InventoryUnderflowException;  public  Item createItem(String name,  int  price,  int  quantity)  throws  ItemExistsException;  public  List getItems();  public   void  removeItem(Item item)  throws  NoSuchItemException;  public   void  removeItem(  int  catalogno)  throws  NoSuchItemException; } 

Let's look at the Shopping Cart application in more detail. It could have components and methods like these:

  • Cart:

     addItem(itemNumber, quantity)   deleteItem(itemNumber)  listItems()  calculateCartTotal()  resetCart() 
  • Purchase:

     setShipmentAddress(cart, Name, Street, City, Zip, Phone)   setCreditCardInfo(cart, Name, CardNumber, ExpiryDate)  calculateShipping(cart)  calculatePurchaseTotal(cart)  checkWarehouse(cart)  cancelPurchase(cart) 
  • Credit Card Validation:

     authorize(Name, CardNumber, ExpiryDate, Amount) 
  • Order Fullfillment:

     checkWarehouse(cart)   submitOrder(cart)  shipOrder(cart) 

In this case you should create MBeans for the Catalog and Shopping Cart applications. You don't really need an MBean for the Order Status Check and Shipper Status Check functions because they are managed as part of the Shopping Cart application. We've already seen the following catalog MBeanInfo :

  • Attributes:

     TotalSales  NumberOfItems TotalSales Accesses InventorySize AutoCheckpoint 
  • Operations:

     load()  doCheckpoint() 
  • Notifications: none

Other information that could be part of MBeanInfo data includes some time-related attributes about response times:

  • AverageProductListResponseTime

  • ProductListResponseThreshold

You could also have operations to put the catalog online or take it offline:

  • enable()

  • disable()

And you could have notification from the catalog for events that require attention from an administration ”for example,

 "Item list response time exceeded maximum allowable." 

The Shopping Cart MBeanInfo could have similar types of information, operations, and notifications:

  • Attributes:

     NumberOpenShoppingCarts  AverageAgeOfShoppingCarts AverageTimeInPurchasePhase AverageDeliveryTime PercentageLateDeliveries DeliveryThreshold ShoppingCartAgeThreshold 
  • Operations:

     listOpenShoppingCarts()  listShoppingCartsOlderThan(days) listLateOrders() expediteOrder() cancelCart() checkOrderStatus() checkShippingStatus() 
  • Notifications:

     "Credit card validation application unavailable"  "Order delivery date exceeded allowable delivery delay" 

As these examples show, the information you are tracking for the application consists of aggregations rather than instances of carts. This information helps you determine if the application is accessible and functioning well enough to keep customers from feeling restless. This section was meant to stress that there does not need to be a direct correlation between MBeans and important objects or concepts in your application. But rather, you need to decide what the appropriate level of management granularity for your application is, in this case by catalogs and shopping carts.

9.4.2 Application Self-Management

Self-management means that an application's MBeans completely manage the application, requiring no third-party or enterprise management system.

There are two granularities here: First, the application is accompanied by a JMX-based application-specific management application. Second, the application instantiates MBeans, monitor MBeans, and adapters to handle its management. This management can happen entirely local to a particular MBeanServer.

9.4.2.1 Application-Specific Manager

JMX was designed to be a single instrumentation able to feed both enterprise managers and application-specific managers. Most applications of any complexity will need to be accompanied by a management and administrative application. JMX can provide the necessary infrastructure. The advantages of providing an application-specific manager are that you can create a much more intuitive and detailed presentation to the administrator and you can provide much more detailed configuration and recovery support. It is not uncommon for some of the MBeans' attributes to be managed exclusively by the application-specific manager. A subset, often the more coarse-grained or aggregated data, is exposed to enterprise managers.

If the management application is running in the same JVM, you don't even need an adapter to interact with the MBeanServer. If the management system is running in a separate JVM, you will need at least a simple RMI or HTTP adapter to communicate with the remote MBeanServer.

If you write your own application-specific manager, you should consider providing the following functionality:

  • Application configuration management, to display and update application configuration attributes. These attributes should be in one or more MBeans.

  • Application operational management, to display application management operations, allow the specification of parameter values, invoke the operation, and display the response. You should at least provide for lifecycle control: start, stop, and refresh configuration. These operations should be in one or more MBeans.

  • Status display, to display the current status of the application as a whole, as well as its components. This status display should be influenced by notifications received. The status can be as simple as "up" or "down" shown as green and red. It may also provide variations in between, yellow and orange for degradations. Degradation can be detected when monitor notifications or application-specific notifications are received.

  • Notification or notification log display, to display the history of notifications received or notifications for a time period. Like status displays, notification displays can be color-coded by severity.

  • Management configuration, to display the configuration values, both read-only and modifiable. The configuration manager can handle all modification of values, but then it must be sure to implement those changes in the managed application. Some management configuration includes

    “ MBean configuration (caching, persistence, logging)

    “ Monitor MBean configuration (frequency)

    “ Notification forwarding and filtering

    “ Notification automated response

  • Monitor MBean control, to start and stop configured monitor MBeans.

Of course, you can pick any subset of this functionality as well, depending on your application, its criticality, its complexity, and how much you like writing management applications.

9.4.2.2 Local Monitoring and Recovery

Every JMX MBeanServer supports monitoring, timers, and notifications. This means that every MBeanServer is capable of monitoring its local applications and responding to notifications. If you are going to use local monitoring, you can instantiate monitor MBeans (see Chapter 7) to watch values of specific attributes of specific MBeans. These monitor MBeans, as well as your application-specific MBeans, may send notifications. This means that you need to have an instance of NotificationListener implemented. A notification can be another MBean; an adapter to a local manager, e-mail sender, or pager; or just a class of your choosing.

The NotificationListener class that you implement can react any number of ways to a notification. It can do one or more of the following:

  • Send the notification to a log

  • Display the notification to an operator via a console or e-mail message

  • Page an operator

  • Forward the notification to a management or correlation system

  • Interact with the managed resource to correct the problem indicated by the notification

  • Interact with the system to correct the problem indicated by the notification

There is a trade-off between self-management, or local management, and enterprise management. Local management is attractive because it works regardless of the state of the network and has many fewer dependencies on other applications. Because there are fewer dependencies, there are also fewer points of failure in management and recovery. This makes the management much more reliable per failure. You can also monitor and react with a much higher frequency because you do not incur the overhead of going between JVMs on a system or over networks.

The Achilles' heel here is that if the local host is failing, there may not be an external manager to recover any of it. In addition, local management actions have only local knowledge. They cannot know the state of the entire application or enterprise. It may not always be appropriate to recover a resource if other parts of the enterprise are stressed. Another issue to be aware of is double management. This is the case where there is local management, which the enterprise manager is not aware of, and enterprise management. Effectively you end up with two sets of monitors and potentially two duplicate, different, or even conflicting responses.

Our advice here is to use JMX to instrument your application for your own management system as well as enterprise managers. To reduce IT resource use, perform monitoring in the local host. Automatic responses to notifications should be implemented locally only if a larger context is not required to ensure the operation is correct. To reduce redundant management, provide a means to " turn off" management functions that execute locally in favor of that responsibility being taken over and provided by an enterprise management system. Obviously there are trade-offs for each of these decisions, but these are fair guidelines.

9.4.3 Resource Schema

JMX is a model-less manageability architecture. This means that JMX does not require you to use any particular naming conventions for your attributes or operations. Nor does it provide you any standard way to represent management data. The lack of an explicit resource model makes JMX very flexible. It is possible to represent nearly any resource type and data in JMX MBeans. You do not have to try to fit your existing information into someone else's model. Nor do you have to figure out how to extend an existing model when your application doesn't fit neatly into the existing model.

The downside of not having a resource model is that because there is no standard, accepted vocabulary for common management terms and relationships, every MBean may have its own variation for the same type of data. For example, a basic state variable can be named state , status , currentState , or availability . Its values can be boolean , strings as in "up/down" or "available/unavailable", or even multivalued, as in "up/degraded/failed/down". It can become very difficult for a management system to realize that all of these represent the same kind of data and they can all be normalized and used to update colors in a topology screen. It may also be hard for the system to determine how to aggregate the status to get a broader system status. This lack of consistent terminology for management systems ends up requiring custom integration of every MBean and its meanings into management systems through the JMX adapter to that management system. Sometimes this can require some custom integration in the management application itself. One of the goals of JMX was to enable the development of general-purpose management applications that can "understand" the MBeans presented to it without custom development. You can see that the lack of a common model seriously jeopardizes this goal.

More than a few management data models are being developed by standards bodies ”for example, SNMP MIBs from the IETF, and CIM from the DMTF. Of these, CIM is probably the best suited as a basic model for building MBeans. CIM is a closer match to JMX in the concepts of operations, attributes, notifications and metadata. SNMP MIBs represent attributes and events well, but the operations concepts can be difficult to translate intuitively.

CIM addresses the device, system, and networking disciplines very well. The models there are fairly complete, stable, and easy to extend. It is reasonably easy to figure out where your device or system belongs relative to the model. If you are developing an application or middleware, it is more difficult to use the CIM model. Although not yet complete, the models for the application are under active development.

It requires quite a bit of work to understand the CIM model and understand the best way to extend it for your own purposes. Even so, we recommend that you use the CIM model as much as you can. If you cannot use the model as is, at least use the CIM vocabulary for variable names and relationships. For example, CIM uses status for status or state; if at all possible your MBeans should use status for status as well. Following this approach increases the odds that management systems will be able to understand and manage your MBean appropriately. This approach may decrease the amount of development you have to do in JMX adapters or in management applications.

If you have a mismatch in terminology or names but the semantics are very close, you can use the ProtocolMap object in the model MBean descriptors (see Chapter 4) to tell the adapter to relate the attribute in the MBean to the attribute in the CIM model.

If you find a CIM model that is close to your needs, it is relatively easy to generate MBeans from the model. There are tools available from Sun that generate MBeans from both CIM models and SNMP MIBs.

Figure 9.11 shows a CIM UML diagram of the core model and the managed system element taken from the "Common Information Model (CIM Core ) Model, Version 2.4" white paper (available at http://www.dmtf.org/standards/documents/CIM/DSP0111.pdf).

Figure 9.11. The Core Model and the Managed Element

graphics/09fig11.gif

ManagedSystemElement is a basic class that most other CIM classes eventually derive from. Here is a standard-MBean interface that could be generated for ManagedSystemElement :

 public interface ManagedSystemElementMBean {        String getName();       void setName(String newName);       String GetStatus();       void SetStatus(String newStatus);       DateTime getInstallDate();       void setInstallDate(String newDate); } 

ManagedSystemElement MBeans can be members of component and SystemComponent relationships MBeans for starters. We would represent these relationships, or CIM associations, by creating new JMX relationship MBeans. In conclusion, using CIM models as the basis for your MBeans is not a trivial effort. But it yields the following benefits: You are future-proofing your MBeans, you are increasing the odds that your MBeans will be understood by other management systems, and you may get new ideas from the CIM model to improve your management design.

9.4.4 Notifications and Logging

It is important to recognize the difference between messages that should be logged and messages that should be notifications. If you have an existing application, it can be tempting to send a notification for every logged message, but doing so could flood your MBeanServer, adapter, and management systems with notifications that are just thrown away or merely logged again. Messages that should be logged should be logged not only to record problems and failures, but also to provide an audit trail, record the progress of transactions, and record the use of features or invocations of functions.

Logged messages are used later ”usually not while the application is running ”to do problem determination, root failure analysis, usage analysis, and performance analysis. All of these analyses can be used to tune the network, system, or application to improve performance of the more highly used or critical functions. Some of these logged messages should be JMX notifications. Messages about failures or events that require automated responses or recovery, or operator action, should be JMX notifications.

There are several ways that you can send messages from your application as JMX notifications. First, if your application is the MBean, you would simply implement the NotificationBroadcaster interface or extend the NotificationBroadcasterSupport class and send the notification from your application where appropriate. You would find a sendNotification() method invocation wherever you logged a critical message. Another common pattern is to instrument a logging application to send the notification.

Many complex applications use special logging components or utilities. Such a utility can be modified to send a JMX notification when certain messages are being logged. Or JMX logging can be based on the severity of the message. If you have control of the code, you can modify the logging component to accept a flag to indicate if the message should be a notification or not. Some analysis tools run relatively frequently (every hour or so). If the notifications do not require a response immediately or in real time, you can build the sending of JMX notifications into these types of log analysis tools. Daily analysis tools run probably too infrequently for it to make sense to send JMX notifications from them.

9.4.5 Federation Options

When you are developing distributed applications, you may need to have several MBeanServers distributed across your application. You may have one MBeanServer per JVM, host, or functional component. When you try to build your management application, or represent your application to an enterprise manager, you will need to locate and access the MBeans across all the MBeanServers. This is called MBeanServer federation.

Another common scenario in which MBeanServer federation will be necessary is when you are building applications that will execute in JMX-enabled middleware, like application servers and messaging systems. The middleware will have at least one of its own MBeanServers, and you may have your own MBeanServer for your application. In fact, in an application server every application that is executing may have its own MBeanServer instance. The application server's management system will need to recognize its own MBeanServer, as well as all the guest MBeanServers and their MBeans. You will need to deal with MBeanServers designed to talk together in the distributed-application case, as well as those being absorbed unknowingly by another MBeanServer in the middleware case.

Basically you have to decide what your application or manager is going to "see": one MBeanServer ”perhaps the local one ”or a whole raft of MBeanServers with well-defined relationships. In this section we will examine the single-agent view, along with several implementation models, and the multiple-agent view for implementing MBeanServer federation.

9.4.5.1 Single-Agent View

With a single-agent view, the application or manager interacts with one MBeanServer. Application and manager development is much easier because they don't have to find the right MBeanServer to make a request on an MBean. That MBeanServer is responsible for aggregating all of the MBeans in the MBeanServers with which it is federated. This is very convenient because the application or manager can interact with a local MBeanServer that has services and adapters to interact with other local or remote MBeanServers. The location of the federated MBeanServers does not change how the application or manager is programmed to interact with its MBeanServer. For the sake of this discussion, we will refer to the single-agent MBeanServer as the master MBeanServer. The MBeanServers that the master interacts with (and the application does not) will be referred to as subordinate MBeanServers.

You can choose from several federation patterns to support a single-agent-view topology. In fact, applications and managers can't tell the difference among any of the single-agent-view implementations . In all cases they get a large list of MBeans when they invoke the MBeanServer's queryMBeans() method, and then they use the MBean name with the local MBeanServer to access the target MBean.

The following list describes the single-agent-view topologies:

  1. MBean propagation via proxies:

    In this pattern, shown in Figure 9.12, the subordinate MBeanServers have an adapter that listens for MBean lifecycle notifications for creation and deletion. When a new MBean is created, the adapter communicates with the master MBeanServer and creates and registers a proxy for the new MBean in the master MBeanServer. Likewise, when an MBean is removed or deleted, the adapter removes or deletes the proxy in the master MBeanServer. The MBean proxy is designed to communicate with the subordinate MBeanServer and the original MBean so that it stays a mirror image.

    Figure 9.12. MBean Propagation from Downstream MBeanServers

    graphics/09fig12.gif

    Applications, adapters, and managers also treat the proxy MBeans just like local MBeans. The proxy can be created and controlled in one of two ways:

    (1) All of the federation development is done in an adapter on the subordinate MBeanServers. This means that the subordinate MBeanServers have to be configured with or able to discover which MBeanServer is the master MBeanServer and create their proxy MBeans there. The master MBeanServer treats the proxy MBean like any other MBean and doesn't really know that it is potentially invoking a remote MBean owned by another MBeanServer. It also means that the subordinate adapter may need to invoke the master MBeanServer from a remote JVM. The implication here is that the master MBeanServer must have an adapter that permits remote invocations.

    (2) All federation is done in the adapter on the master MBeanServers. The master MBeanServers are configured with or able to discover subordinate MBeanServers, and the subordinate MBeanServer doesn't know that it is being federated. In this case the subordinate MBeanServers must have an adapter that permits remote invocations from master MBeanServers. Usually this adapter will be an RMI adapter, but it could also be a simple HTTP or SOAP-over-HTTP adapter.

    The drawback to this pattern is that there can be scalability issues. If there are many proxy MBeans, managers dealing with the master MBeanServer may find searches and dealing with lists of MBeans unwieldy because of the shear volume of MBeans. MBean naming can become critical in these situations to provide some structure for dealing with the MBeans. MBean names should be used to create application domains and resource type groupings.

  2. Request propagation via broadcast:

    In this pattern the master MBeanServer forwards requests to MBeans to the subordinate MBeanServers, as illustrated in Figure 9.13. In this case no representation or proxy of the MBean is registered with the master MBeanServer. In contrast to MBean propagation, the master MBeanServer is specifically built to take care of federation, and the subordinate MBeanServers are not aware that they are in a federated topology. Once again, because the MBeanServers can be in different JVMs, the subordinate MBeanServer must at least have an adapter that gives the master MBeanServer access. As with the previous topology, this adapter will usually be an RMI, HTTP, or SOAP/HTTP remote MBeanServer adapter.

    Figure 9.13. Broadcast of Requests by the MBeanServer

    graphics/09fig13.gif

    Because the request is broadcast to multiple subordinate MBeanServers, the master MBeanServer needs to deal with multiple responses. It may choose to return any of the following:

    • The first one it gets back ( fastest performer), and throw away the rest

    • An aggregate of the responses in some way: sum, average, median

    • An array of all the raw responses: numbers or strings

    There are a few drawbacks to this pattern. Although the pattern works well for finding MBeans by name and invoking operations on MBeans whose names you have, it makes listing MBeans more complex because all the MBeans from all the federated MBeanServers need to be retrieved and combined. If there are a lot of subordinate MBeanServers or many requests, request broadcasting can seriously increase the amount of network bandwidth being used for management rather than "real work." It also adds to the total overhead of the system because several MBeanServers may unnecessarily execute the request simultaneously .

    In some situations you can use request broadcasting without a defined master MBeanServer. If you have a master MBeanServer, then all requests come from it and are returned to it. The subordinate MBeanServers do not do request forwarding or broadcasting unless they forward to a completely disjointed set of MBeanServers instead of the master's set. Having a master MBeanServer eliminates the possibility of circular request forwarding ”a long-running, slow, infinite loop. If you implement request broadcasting without a master MBeanServer, you must devise a mechanism to identify MBeanServers that have already been broadcast to for the entire set of possible MBeanServers.

  3. Request propagation via directory:

    This pattern is just like request propagation via broadcast, except that instead of broadcasting the request to all MBeanServers, any MBeanServer forwards the request directly to that MBean's MBeanServer. As shown in Figure 9.14, when any MBeanServer gets a request for an MBean that is not registered locally, it checks an MBean directory to find the correct MBeanServer and forwards the request to the correct MBeanServer. This process implies a discovery, synchronization, or update mechanism to ensure that the MBean directory's contents are very accurate.

    Figure 9.14. MBean Location through Central Directory

    graphics/09fig14.gif

    One way to do this is to make sure that all MBeanServers in the federation have adapters that register new MBeans in a central MBean directory. This arrangement implies that all the MBeanServers know which directory they should be registered with.

    Another methodology would be to have the directory periodically list all the MBeans on each MBeanServer to synchronize the directory with the MBeanServers. This approach implies that the directory knows all the MBeanServers it is responsible for.

    A third approach would be to have the directory application dynamically discover all the MBeanServers in all the JVMs and then list all the MBeans. This approach requires the least preconfiguration , but it is not trivial to implement. It presumes that there is a way to discover all JVMs on a system and a way to remotely invoke the findMBeanServer() method in it.

    One nice feature is that it is possible to create a masterless topology of MBeanServers because they all look up from the same directory and forward to the correct MBeanServer. There is no chance of circular request forwarding. This is convenient in that any MBeanServer will give the exact same view of the MBeans. Therefore, you can have multiple management consoles with little or no impact on your MBeanServer federation topology.

    The drawback to this approach is the overhead in maintaining an accurate directory of MBean and MBeanServer associations. The directory also introduces the risk that some newly created MBeans will not be discoverable for a short time, and conversely, that someone might invoke methods on MBeans that no longer exist.

    To be fair, there are ways to use directed request forwarding without using a directory. The MBeanServer itself can maintain an association between an MBean name or MBeanServer domain and the subordinate MBeanServer. It may use another mechanism to discover, at runtime, which MBeanServer has the MBean registered with it. The runtime discovery mechanism may be as simple as a naming convention for the MBean that contains the MBeanServer name, host name, and port. New MBeanServers to be forwarded could be configured in at runtime. Without a central MBean directory, it is much more desirable to use a master MBeanServer again. Using a master MBeanServer simply limits the number of MBeanServers that have to maintain their own independent map of MBeans.

9.4.5.2 Multiple-Agent View

The multiple-agent-view approach to federation, as shown in Figure 9.15, puts the onus for aggregation on the application or manager. This approach adds complexity to the applications managers that use it. They must now deal with finding remote MBeanServers, figuring out which MBeanServer owns the MBean it wants, performing broadcasts if necessary, and aggregating the results. On the other hand, this approach requires no special adapters in subordinate MBeanServers, nor does it require a master MBeanServer that understands how to communicate with and aggregate MBeanServers.

Figure 9.15. Multiple-Agent View

graphics/09fig15.gif

If you decide to have your application see all the MBeanServers, you must have a well-defined mechanism to be able to find all the MBeanServers it should communicate with. This mechanism could be configured into the application, or it could be discovered during runtime. One approach would be to add a small adapter to all MBeanServers that update a simple registry as they initialize.

We recommend implementing a single-agent view for federation. Which implementation option you choose depends on your requirements. If you anticipate having an enormous number of MBeanServers and MBeans, you should use the request propagation via a directory model because it is the most scalable. If you require ease of implementation, the request propagation via broadcast is the easiest to implement reliably. Request propagation via proxy is a good trade-off of performace for ease of implementation.



Java and JMX. Building Manageable Systems
Javaв„ў and JMX: Building Manageable Systems
ISBN: 0672324083
EAN: 2147483647
Year: 2000
Pages: 115

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net