Skip Navigation Links | |
Exit Print View | |
Oracle GlassFish Server 3.1 Add-On Component Development Guide |
1. Introduction to the Development Environment for GlassFish Server Add-On Components
3. Extending the Administration Console
4. Extending the asadmin Utility
5. Adding Monitoring Capabilities
Defining Statistics That Are to Be Monitored
Defining an Event Provider by Writing a Java Class
Defining Event Types in an Event Provider Class
Example of Defining an Event Provider by Writing a Java Class
Defining an Event Provider by Writing an XML Fragment
Packaging a Component's Event Providers
Updating the Monitorable Object Tree
Representing a Component's Statistics in an Event Listener Class
Subscribing to Events From Event Provider Classes
Dotted Names and REST URLs for an Add-On Component's Statistics
6. Adding Configuration Data for a Component
7. Adding Container Capabilities
8. Creating a Session Persistence Module
9. Packaging, Integrating, and Delivering an Add-On Component
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.
|
Example 5-8 Event Provider Class
This example illustrates how to define an event provider as explained in Defining an Event Provider by Writing a Java Class.
The example shows the definition of the ModuleProbeProvider class. The event provider sends events when the request count is increased by 1 or decreased by 1.
This class defines 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 org.glassfish.pluggability.monitoring; import org.glassfish.external.probe.provider.annotations.Probe; import org.glassfish.external.probe.provider.annotations.ProbeParam; import org.glassfish.external.probe.provider.annotations.ProbeProvider; /** * Monitoring count events * Provider interface for module specific probe events. * */ @ProbeProvider(moduleProviderName = "glassfish", moduleName = "mybeanmodule", probeProviderName = "mybean") public class ModuleProbeProvider { /** * Emits probe event whenever the count is incremented */ @Probe(name = "moduleCountIncrementEvent") public void moduleCountIncrementEvent( @ProbeParam("count") Integer count) { } /** * Emits probe event whenever the count is decremented */ @Probe(name = "moduleCountDecrementEvent") public void moduleCountDecrementEvent( @ProbeParam("count") Integer count) { } }
Example 5-9 Bootstrap Class
This example illustrates how to register an event listener as explained in Registering an Event Listener. The example shows the code for registering an instance of the listener class ModuleStatsTelemetry. This instance is added as a child of the server/applications node of the tree.
package org.glassfish.pluggability.monitoring; import org.jvnet.hk2.component.PostConstruct; import org.jvnet.hk2.annotations.Service; import org.jvnet.hk2.annotations.Scoped; import org.jvnet.hk2.component.Singleton; import org.glassfish.external.probe.provider.StatsProviderManager; import org.glassfish.external.probe.provider.PluginPoint; /** * Monitoring Count Example * Bootstrap object for registering probe provider and listener * */ @Service @Scoped(Singleton.class) public class ModuleBootStrap implements PostConstruct { @Override public void postConstruct() { try { StatsProviderManager.register("web-container", PluginPoint.APPLICATIONS, "myapp", new ModuleStatsTelemetry()); } catch (Exception e) { System.out.println("Caught exception in postconstruct"); e.printStackTrace(); } } }
Example 5-10 Listener Class
This example shows how to perform the following tasks:
Creating Event Listeners. The example shows the code of the ModuleStatsTelemetry listener class.
Representing a Component's Statistics in an Event Listener Class. The example shows the code for representing the countmbeancount statistic.
Subscribing to Events From Event Provider Classes. The example shows the code for subscribing to the following types of events from the count component:
moduleCountIncrementEvent
moduleCountDecrementEvent
package org.glassfish.pluggability.monitoring; import org.glassfish.external.statistics.CountStatistic; import org.glassfish.external.statistics.impl.CountStatisticImpl; import org.glassfish.external.probe.provider.annotations.ProbeListener; import org.glassfish.external.probe.provider.annotations.ProbeParam; import org.glassfish.gmbal.ManagedAttribute; import org.glassfish.gmbal.ManagedObject; /** * Monitoring counter example * Telemtry object which listens to probe events and updates * the monitoring stats * */ @ManagedObject public class ModuleStatsTelemetry { private CountStatisticImpl countMBeanCount = new CountStatisticImpl( "CountMBeanCount", "count", "Number of MBeans"); @ManagedAttribute(id = "countmbeancount") public CountStatistic getCountMBeanCount() { return countMBeanCount; } @ProbeListener("count:example:countapp:moduleCountIncrementEvent") public void moduleCountIncrementEvent( @ProbeParam("count") Integer count) { countMBeanCount.increment(); } @ProbeListener("count:example:countapp:moduleCountDecrementEvent") public void moduleCountDecrementEvent( @ProbeParam("count") Integer count) { countMBeanCount.decrement(); } }
Example 5-11 MBean Interface
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() ; }
Example 5-12 MBean Implementation
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 org.glassfish.pluggability.monitoring; /** * Monitoring counter example * ModuleMBean implementation * */ public class Module implements ModuleMBean { private int k = 0; private ModuleProbeProvider mpp = null; @Override public Integer getCount() { return k; } @Override public void incrementCount() { k++; if (mpp != null) { mpp.moduleCountIncrementEvent(k); } } @Override public void decrementCount() { k--; if (mpp != null) { mpp.moduleCountDecrementEvent(k); } } void setProbeProvider(ModuleProbeProvider mpp) { this.mpp = mpp; } }