Receiving server statistics events
From JPPF 6.2 Documentation
|
Main Page > Customizing JPPF > Receiving server statistics events |
Reminder: if you are not familiar with the server statistics API, please refer to the section Development Guide > The JPPF statistics API of this user guide.
1 Implementation
To receive notifications of changes in the server statistics, you will need to create a class with a no-arg constructor which extends JPPFFilteredStatisticsListener, which is defined as follows:
public abstract class JPPFFilteredStatisticsListener implements JPPFStatisticsListener { // Get an optional filter to associate with this listener public JPPFStatistics.Filter getFilter() }
The method getFilter() returns a JPPFStatististcs.Filter which may be null, and is defined as:
public class JPPFStatistics implements Serializable, Iterable<JPPFSnapshot> { // A filter interface for snapshots public interface Filter { // Determines whether the specified snapshot is accepted by this filter boolean accept(JPPFSnapshot snapshot); } }
The statistics listener will only receive events for the snaphots accepted by the filter. If it is null, then it will receive events for all the snapshots.
JPPFFilteredStatisticsListener implements the JPPFStatisticsListener interface, which provides the following event notification methods:
public interface JPPFStatisticsListener extends EventListener { // Called when a new snapshot is created void snapshotAdded(JPPFStatisticsEvent event); // Called when a snapshot is removed void snapshotRemoved(JPPFStatisticsEvent event); // Called when a snapshot is updated void snapshotUpdated(JPPFStatisticsEvent event); }
Each JPPFStatisticEvent object provides the following information:
public class JPPFStatisticsEvent extends EventObject { // Get the statistics source of this event public JPPFStatistics getStatistics() // Get the snapshot that was created, removed or updated public JPPFSnapshot getSnapshot() }
Note: since updates to the statistics in the server have a very high frequency, it is recommended to make the listener methods as short as possible, with regards to their execution time. This will avoid a significant impact to he server performance. If you can't avoid it, then you may consider posting the events into a queue for asynchronuos processing in a separate thread.
2 Deployment
The deployment of a statistics is done via the Service Provider Interface (SPI):
- create a file named "org.jppf.utils.stats.JPPFFilteredStatisticsListener" in the META-INF/services folder
- in this file add a line containing the fully qualified name of your listener implementation class, for instance "mypackage.MyClass"
- inlcude the jar file or class folder containing both your implementation and service in th eserver's classpath
3 Full example
Let's write a statistics listener which prints all the events it receives to the output console:
package test.stats; import org.jppf.utils.stats.*; public class MyStatisticsListener extends JPPFFilteredStatisticsListener { public MyStatisticsListener() { System.out.println("creating new statistics listener"); } @Override public void snapshotAdded(JPPFStatisticsEvent event) { System.out.printf("added '%s'%n", event.getSnapshot().getLabel()); } @Override public void snapshotRemoved(JPPFStatisticsEvent event) { System.out.printf("removed '%s'%n", event.getSnapshot().getLabel()); } @Override public void snapshotUpdated(JPPFStatisticsEvent event) { System.out.printf("updated '%s'%n", event.getSnapshot().getLabel()); } @Override public JPPFStatistics.Filter getFilter() { System.out.println("in MyStatisticsListener.getFilter()"); return super.getFilter(); } }
Now, for the server to register this listener, we create the file:
"META-INF/services/org.jppf.utils.stats.JPPFFilteredStatisticsListener"
with the following single line: "test.stats.MyStatisticsListener"
Our listener is now ready to be deployed.
Main Page > Customizing JPPF > Receiving server statistics events |