This example shows a component that monitors the number of requests that a container receives. The following table provides a cross-reference to the listing of each class or interface in the example.
Class or Interface |
Listing |
---|---|
ModuleProbeProvider | |
ModuleBootStrap | |
ModuleStatsTelemetry | |
Module | |
ModuleMBean |
This example illustrates how to define an event provider as explained in Defining an Event Provider.
The example shows the definition of the ModuleProbeProvider interface. The event provider sends events when the request count is increased by 1 or decreased by 1.
This interface declares the following methods:
moduleCountIncrementEvent
moduleCountDecrementEvent
The name of each method is also the name of the event type that is associated with the method.
A parameter that is named count is passed to each method.
package com.example.count.monitoring; import org.glassfish.flashlight.provider.annotations.ProbeParam; /** * Monitoring count eventse * Provider interface for module specific probe events. * */ public interface ModuleProbeProvider { /** * Emits probe event whenever the count is incremented */ public void moduleCountIncrementEvent( @ProbeParam("count") Integer count ); /** * Emits probe event whenever the count is decremented */ public void moduleCountDecrementEvent( @ProbeParam("count") Integer count ); }
This example illustrates how to perform the following tasks:
Registering an Event Provider. The example shows the code for registering the event provider and passing a reference to the event provider implementation to the MBean implementation Module.
Adding Statistics for a Component to the Monitorable Object Tree. The example shows the code for adding the component as a child of the server node of the tree.
Registering an Event Listener. The example shows the code for registering an instance of the listener class ModuleStatsTelemetry.
package com.example.count.monitoring; import java.lang.management.ManagementFactory; import org.jvnet.hk2.component.PostConstruct; import javax.management.MBeanServer; import javax.management.ObjectName; import org.jvnet.hk2.annotations.Inject; import org.jvnet.hk2.annotations.Service; import org.jvnet.hk2.annotations.Scoped; import org.jvnet.hk2.component.Singleton; import org.glassfish.flashlight.MonitoringRuntimeDataRegistry; import org.glassfish.internal.api.Init; import org.glassfish.flashlight.provider.ProbeProviderFactory; import org.glassfish.flashlight.client.ProbeClientMediator; import org.glassfish.flashlight.client.ProbeClientMethodHandle; import java.util.Collection; import org.glassfish.flashlight.datatree.TreeNode; import org.glassfish.flashlight.datatree.factory.TreeNodeFactory; /** * Monitoring Count Example * Bootstrap object for registering probe provider and listener * */ @Service @Scoped(Singleton.class) public class ModuleBootStrap implements Init, PostConstruct { @Inject private MonitoringRuntimeDataRegistry mrdr; @Inject protected ProbeProviderFactory probeProviderFactory; @Inject private ProbeClientMediator pcm; private TreeNode serverNode; public void postConstruct() { try { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("count.example.monitoring:name=countMBean"); Module mbean = new Module(); mbs.registerMBean(mbean, name); ModuleProbeProvider mpp = probeProviderFactory.getProbeProvider( "count", "example", "countapp", ModuleProbeProvider.class); mbean.setProbeProvider(mpp); TreeNode serverNode = getServerNode(); TreeNode countNode = TreeNodeFactory.createTreeNode("count", null, "example"); serverNode.addChild(countNode); ModuleStatsTelemetry modTM = new ModuleStatsTelemetry(countNode); Collection<ProbeClientMethodHandle> handles = pcm.registerListener(modTM); modTM.setProbeListenerHandles(handles); } catch (Exception e) { System.out.println("Caught exception in postconstruct"); e.printStackTrace(); } } private TreeNode getServerNode() { TreeNode serverNode = null; if (mrdr.get("server") != null) { serverNode = mrdr.get("server"); } else { serverNode = TreeNodeFactory.createTreeNode("server", null, "server"); mrdr.add("server", serverNode); } return serverNode; } }
This example shows how to perform the following tasks:
Creating Event Listeners. The example shows the code of the ModuleStatsTelemetry listener class.
Subscribing to Events From Event Provider Classes. This example shows the code for subscribing to the following types of events from the count component:
moduleCountIncrementEvent
moduleCountDecrementEvent
The provider of the component is example. The application name is countapp.
The example also shows the code for processing the events and updating the monitoring statistics. To enable the listener to update statistics in the monitorable object tree, the parent of statistics object in the tree is passed in the constructor of this class.
package com.example.count.monitoring; import java.util.Collection; import org.glassfish.flashlight.client.ProbeClientMethodHandle; import org.glassfish.flashlight.statistics.*; import org.glassfish.flashlight.statistics.factory.CounterFactory; import org.glassfish.flashlight.datatree.TreeNode; import org.glassfish.flashlight.datatree.factory.*; import org.glassfish.flashlight.client.ProbeListener; import org.glassfish.flashlight.provider.annotations.ProbeParam; import org.glassfish.flashlight.provider.annotations.*; /** * Monitoring counter example * Telemtry object which listens to probe events and updates * the monitoring stats * */ public class ModuleStatsTelemetry{ private Counter k = CounterFactory.createCount(); private Collection<ProbeClientMethodHandle> handles; public ModuleStatsTelemetry(TreeNode parent) { k.setName("countMBeanCount"); parent.addChild(k); } @ProbeListener("count:example:countapp:moduleCountIncrementEvent") public void moduleCountIncrementEvent( @ProbeParam("count") Integer count) { k.increment(); } @ProbeListener("count:example:countapp:moduleCountDecrementEvent") public void moduleCountDecrementEvent( @ProbeParam("count") Integer count) { k.decrement(); } public void setProbeListenerHandles(Collection<ProbeClientMethodHandle> handles) { this.handles = handles; // by default, the handles are enabled // following template is provided to enable/disable /* for (ProbeClientMethodHandle handle : handles) { handle.enable(); } */ } }
This example defines the interface for a simple standard MBean that has methods to increase and decrease a counter by 1.
package com.example.count.monitoring; /** * Monitoring counter example * ModuleMBean interface * */ public interface ModuleMBean { public Integer getCount() ; public void incrementCount() ; public void decrementCount() ; }
This example illustrates how to send an event as explained in Sending an Event. The example shows code for sending events as follows:
The moduleCountIncrementEvent method is invoked in the body of the incrementCount method.
The moduleCountDecrementEvent method is invoked in the body of the decrementCount method.
The methods incrementCount and decrementCount are invoked by an entity that is beyond the scope of this example, for example, JConsole.
package com.example.count.monitoring; /** * Monitoring counter example * ModuleMBean implementation * */ public class Module implements ModuleMBean { private int k = 0; private ModuleProbeProvider mpp = null; public Integer getCount() { return k; } public void incrementCount() { k++; if (mpp != null) { mpp.moduleCountIncrementEvent(k); } } public void decrementCount() { k--; if (mpp != null) { mpp.moduleCountDecrementEvent(k); } } void setProbeProvider(ModuleProbeProvider mpp) { this.mpp = mpp; } }