Receiving node connection events in the server
From JPPF 6.3 Documentation
|
Main Page > Customizing JPPF > Receiving node connection events |
This extension point allows you to register a listener for receiving notifications when a node is connected to, or disconnected from the server. These notifications can be received either locally from within the server JVM, or remotely via a JMX listener
1 Local notifications
As for other JPPF extensions, it relies on the Service Provider Interface (SPI) mechanism to enable an easy registration.
To implement this extension, you first need to create an implementation of the NodeConnectionListener interface, defined as follows:
public interface NodeConnectionListener extends EventListener { // Called when a node is connected to the server void nodeConnected(NodeConnectionEvent event); // Called when a node is disconnected from the server void nodeDisconnected(NodeConnectionEvent event); }
Each notification method receives instances of the NodeConnectionEvent class, which is defined as:
public class NodeConnectionEvent extends EventObject { // Get the node information for this event public JPPFManagementInfo getNodeInformation() }
As we can see, these event objects are simple wrappers carrying detailed information about the node, via the class JPPFManagementInfo:
public class JPPFManagementInfo implements Serializable, Comparable<JPPFManagementInfo> { // Get the host on which the node is running public String getHost() // Get the port on which the node's JMX server is listening public int getPort() // Get the system information associated with the node at the time // it established the connection public JPPFSystemInformation getSystemInfo() // Get the node's unique id (UUID) public String getId() // Determine whether this information represents another driver, // connected as a peer to the current driver public boolean isDriver() // Determine whether this information represents a real node public boolean isNode() }
For details on the available information, we encourage you to read the Javadoc for the class JPPFSystemInformation.
Note: from the nodeConnected() method, you may refuse the connection by throwing a RuntimeException. This will cause the JPPF driver to terminate the connection.
To deploy the extension:
- create a file named org.jppf.server.event.NodeConnectionListener in the META-INF/services folder
- in this same file, add the fully qualified class name of your NodeConnectionListener implementation, for example: mypackage.MyNodeConnectionListener. This is the service definition file for the extension.
- create a jar with your code and and service definition file and add it to the driver's classpath, or simply add your classes folder to the driver's classpath.
2 JMX notifications
JMX notifications of node connection and disconnection events are sent by a server MBean which implements the interface JPPFNodeConnectionNotifierMBean, defined as follows:
public interface JPPFNodeConnectionNotifierMBean extends NotificationEmitter { // The name of this MBean, used when it is registered with an MBean server String MBEAN_NAME = "org.jppf:name=nodeConnectionNotifier,type=driver"; // The type of notification which indicates that a node is connected String CONNECTED = "connected"; // The type of notification which indicates that a node is disconnected String DISCONNECTED = "disconnected"; }
As we can see, this MBean doesn't have any method or attribute: if only sends notifications. The notifications received from this MBean are of the standard type Notification and should be interrpeted as follows:
- the notification's getUserData() method returns a JPPFManagementInfo object and the return value can be safely cast to this type.
- the getType() method returns one of the two dedicated constants defined in JPPFNodeConnectionNotifierMBean: CONNECTED or DISCONNECTED.
- the getSource() method returns the value of the MBEAN_NAME constant.
Here is an example of notification listeners that processes node connection events via JMX:
public class MyNodeConnectionListener implements NotificationListener { @Override public void handleNotification(Notification notif, Object handback) { // retrieve the information on the node JPPFManagementInfo info = (JPPFManagementInfo) notif.getUserData(); switch (notif.getType()) { case JPPFNodeConnectionNotifierMBean.CONNECTED: // process node connected event ... break; case JPPFNodeConnectionNotifierMBean.DISCONNECTED: // process node disconnected event ... break; } } }
All that remains to do is to register this listener with a JMX connection, as in this example:
// create the client try (JPPFClient client = new JPPFClient()) { // obtain a working JMX connection to the server JMXDriverConnectionWrapper jmx = client.awaitWorkingConnectionPool().awaitWorkingJMXConnection(); // register the JMX notification listener NotificationListener myListener = new MyNodeConnectionListener(); jmx.addNotificationListener(JPPFNodeConnectionNotifierMBean.MBEAN_NAME, myListener); ... } catch(Exception e) [ e.printStackTrace(); }
Main Page > Customizing JPPF > Receiving node connection events |