![]() |
|||
![]() |
![]() ![]() |
![]() |
![]() ![]() |
![]() |
![]() ![]() |
![]() |
| |||||||
Chapter 24Legacy Proxy MechanismAs of version 5.1 of Java Dynamic Management Kit (Java DMK), proxies are generated using the java.lang.reflect.Proxy interface defined by the Java 2 Platform, Standard Edition (J2SE). However, if you want to implement proxies with the legacy connectors, you must use the legacy proxy mechanism described in this chapter. This information is still valid, and is provided for backwards compatibility. In Protocol Adaptors, we saw how to access a remote agent and interact with its MBeans through connectors. Java DMK provides additional functionality that makes the remoteness of an agent and the communication layer even more transparent: proxy objects for registered MBeans. A proxy is an object instance that represents an MBean, that mirrors the methods of the MBean, and whose methods are called directly by the calling process. The proxy transmits requests to the MBean, through the MBean server, possibly through a connector, and returns any responses to the calling process. Proxy objects can also register listeners for notifications that the MBean might emit. The advantage of a proxy object is that it enables applications to have an instance of an object that represents an MBean, instead of accessing the MBean's management interface through methods of the MBean server or through a connector client. This can simplify both the conceptual design of a management system and the source code needed to implement that system. The code samples in this chapter are from the files in the legacy/SimpleClients example directory located in the main examplesDir (see "Directories and Classpath" in the Preface). This chapter covers the following topics:
24.1 Legacy Proxy MechanismProxy objects simplify the interactions between an application and the MBeans it wants to manage. The purpose of a proxy is to call the methods that access the attributes and operations of an MBean, through its MBean server. These method calls can be rather tedious to construct at every invocation, so the proxy performs this task for the calling process. For example to call an operation on an MBean, an application must call the invoke method of the MBean server and provide the MBean's object name, the operation name string, an array of parameter objects, and a signature array. The proxy is a class that codes this whole sequence, meaning that the application can call the reset method directly on the proxy instance. Conceptually, a proxy instance makes the MBean server and a protocol connector completely transparent. Except for MBean registration and connector connection phases, all management requests on MBeans can be fully served through proxies, with identical results. However, all functionality of the Java DMK is available without using proxies, so their usage is never mandatory. By definition, a proxy object has the same interface as its MBean: the proxy can be manipulated as if it were the MBean instance, except that all requests are transmitted through the MBean server to the actual MBean instance for processing. A standard MBean proxy exposes getters, setters, and operation methods. It can also register listeners for all notifications broadcast by their corresponding MBean. A dynamic MBean proxy, also known as a generic proxy, exposes generic methods that are identical to those of the DynamicMBean interface. In addition, standard proxies are commonly called proxy MBeans, because they are themselves MBeans. They are generated with an MBean interface, and can therefore be registered as standard MBeans in an MBean server. This feature enables one agent to expose resources whose MBeans are actually located in another agent. An equivalent functionality is described in Chapter 14, Cascading Service. 24.1.1 Legacy Local and Remote ProxiesProxies can also be bound to objects called handlers that necessarily implement the ProxyHandler interface. The methods of this interface are a subset of MBean server methods, as listed in 21.2.2 RemoteMBeanServer Interface. These are the only methods that a proxy needs to access its corresponding MBean and fulfill all management requests. In versions of Java DMK prior to 5.1, the RemoteMBeanServer interface extends the ProxyHandler interface, meaning that proxy objects can be bound to any of the legacy connector clients. These are called remote proxies, because they are instantiated in an application that is distant from the agent and its MBean instances. The implementation of the MBeanServer interface also implements the ProxyHandler interface, so that proxy objects can be bound to the MBean server itself. These are called local proxies because they are located in the same application as their corresponding MBeans. Local proxies help preserve the management architecture by providing the simplicity of performing direct method calls on MBeans, while still routing all operations through the MBean server. The symmetry of remote and local proxies complements the symmetry that enables management components to execute either in an agent or in a management application. Provided that all proxy classes are available, management components that use MBean proxies can be instantiated in an agent and rely on the MBean server or can be instantiated in a remote manager where they interact with a connector client. Except for communication delays, the results of all operations are identical, and the same notifications are received, whether obtained through a local proxy or a remote proxy (see 22.1.3 Adding a Listener Through the Connector). Figure 24-1 shows local proxies that are instantiated in an agent and bound to the MBean server, and the same classes instantiated as remote proxies in a management application and bound to the connector client. Management components located in either the agent or the management application can interact with the local or remote proxies, respectively. Management components can also access the MBean server or the connector client directly, regardless of whether proxies are being used. Figure 24-1 Interacting with Local and Remote Proxies ![]() This diagram shows all possible relations between management components, proxies and their MBeans. Standard proxies can only represent a specific standard MBean, and generic proxies can represent any standard or dynamic MBean. In a typical management scenario, the management components are located in only one application, and for simplicity, they rarely instantiate more than one type of proxy. Throughout the rest of this chapter, we do not distinguish between local and remote proxies. A proxy object, either standard or generic, is used in exactly the same way regardless of whether it is instantiated locally or remotely. 24.1.2 Legacy Proxy InterfaceIn addition to the methods for accessing the attributes and operations of its MBean, all proxies implement the methods of the Proxy interface. This interface contains the methods for binding the proxy instance to the proxy handler that can fulfill its requests. The setServer method binds the proxy to the handler object. Setting the server to null effectively unbinds the proxy object. The result of the getServer method can indicate whether or not a proxy is bound and if so, it will return a reference to the handler object. The following list provides the functionality of the Proxy interface methods. See the Javadoc API of the Proxy interface for details.
Standard proxies can also implement the NotificationBroadcasterProxy interface if their corresponding MBean is a notification proxy. This interface contains the same addNotificationListener and removeNotificationListener methods that the MBean implements from the NotificationBroadcaster interface. Applications that use proxies therefore have two ways to detect notification broadcasters. The first way relies on the implementation of the NotificationBroadcasterProxy interface that can be detected in the proxy's class inheritance. The second and more standard way is to look at the notifications listed in the MBean's metadata obtained by the getMBeanInfo method either from the server or through the proxy. Generic proxies do not implement the NotificationBroadcasterProxy interface, so calling processes must use the MBean metadata for detecting broadcasters. In addition, generic proxies cannot register notification listeners, calling processes must do this directly through the server. 24.2 Standard MBean ProxiesA standard MBean proxy class is specific to its corresponding MBean class. Furthermore, a proxy instance is always bound to the same MBean instance, as determined by the object name passed to the proxy's constructor. Binding the proxy to its ProxyHandler object can be done through a constructor or set dynamically through the methods of the Proxy interface. The methods of a standard proxy have exactly the same signature as those of the corresponding standard MBean. Their only task is to construct the complete management request, that necessarily includes the object name, and to transmit it to the MBean server or connector client. They also return any result directly to the caller. Because the contents of all proxy methods are determined by the management interface of the MBean, the proxy classes can be generated automatically. 24.2.1 Generating Legacy Proxies for Standard MBeansThe deprecated proxygen tool provided with the Java DMK takes the class files of an MBean and its MBean interface and generates the Java source code files of its corresponding proxy object and proxy MBean interface. You then need to compile the two proxy files with the javac command and include the resulting class files in your application's classpath. The proxygen compiler is fully documented in the Java Dynamic Management Kit 5.1 Tools Reference Guide and in the Javadoc API for the ProxyGen class. Its command--line options enable you to generate read-only proxies where all setter methods are suppressed and to define a package name for your proxies. For the purpose of the examples, we generate the default proxies without any of these options. See the section on 24.2.3 Running the Legacy Standard Proxy Example. Example 24-1 shows part of the code generated for the SimpleStandard MBean used in the SimpleClients examples. Example 24-1 Code Generated for the Legacy SimpleStandardProxy Class
You can modify the generated code if you want to customize the behavior of your proxies. However, customization is not recommended if your MBean interfaces are still evolving, because all modifications will need to be redone every time the proxies are generated. | |||||||
| |||||||
![]() |