Create and deploy MBeans to manage services using the JMX Console. Expose service attributes and operations to enable administration tasks to be performed.
For information about using the JMX Console, see Monitoring Server Resources Using the JMX Console.
On the Apache Felix platform, you deploy MBeans as OSGi services. When an MBean service is registered in the OSGi Service Registry, the Aries JMX Whiteboard module automatically registers the MBean with the MBean Server. The MBean is then available to the JMX Console which exposes the public attributes and operations.
MBeans that you create for managing AEM resources are based on the javax.management.DynamicMBean interface. To create them you follow the usual design patterns set forth in the JMX specification:
- Create the management interface, including get, set, and is methods to define attributes, and other methods to define operations.
- Create the implementation class. The class must implement DynamicMBean, or extend an implementation class of DynamicMBean.
- Follow the standard naming convention so that the name of the implementation class is the interface name with the MBean suffix.
In addition to defining the management interface, the interface also defines the OSGi service interface. The implementation class implements the OSGi service.
Typically, your MBean is a reflection on the OSGi service that you want to manage. On the Felix platform, you create the MBean as you would for deployment on other Java server platforms. A primary difference is that you can use annotations to specify MBean information:
- Management interface: Defines attributes using getter, setter, and is methods. Defines operations using any other public method. Uses annotations to provide metadata for the BeanInfo object.
- MBean class: Implements the management interface. Extends the AnnotatedStandardMBean class so that it processes the annotations on the interface.
The AEM Community created a detailed article for developer's, see: Creating Adobe Experience Manager Sling Servlets that invoke MBean operations.
When you register MBeans as an OSGi service, they are automatically registered with the MBean Server. To install an MBean on AEM, include it in a bundle and export the MBean service as you would any other OSGi service.
In addition to the OSGi-related metadata, you must also provide metadata that the Aries JMX Whiteboard module requires for registering the MBean with the MBean Server:
- The name of the DynamicMBean interface: Declare that the MBean service implements the javax.management.DynamicMBean interface. This declaration notifies the Aries JMX Whiteboard module that the service is an MBean service.
- The MBean domain and key properties: On Felix, you provide this information as a property of the MBean's OSGi service. This is the same information that you ordinarily provide to the MBean Server in a javax.management.ObjectName object.
When your MBean is a reflection of a singular service, only a single instance of the MBean service is required. In this case, if you use the Felix SCR Maven plugin, you can use the Apache Felix Service Component Runtime (SCR) annotations on the MBean implementation class to specify the JMX-related metatdata. To instantiate several MBean instances, you could create another class that performs that registration of the MBean's OSGi service. In this case, the JMX-related metadata is generated at runtime.
MBeans for which you can define all the attributes and operations at design time can be deployed using SCR annotations in the MBean implementation class. In the following example, the value attribute of the Service annotation declares that the service implements the DynamicMBean interface. The name attribute of the Property annotation specifies the JMX domain and key properties.
Multiple MBean Service Instances
To manage multiple instances of a managed service, you create multiple instances of the corresponding MBean service. Furthermore, MBean service instances should be created or removed when managed instances are started or stopped. You can create an MBean manager class to instantiate MBean services at runtime, and manage the service lifecycle.
Use the BundleContext to register the MBean as an OSGi service. Include the JMX-related information in the Dictionary object that you use as an argument of the BundleContext.registerService method.
In the following code example, the ExampleMBean service is registered programmatically. The componentContext object is the ComponentContext, which provides access to BundleContext.