Package org.jppf.management.forwarding

Support for forwarding JMX requests to the nodes, along with receiving notifications from them, via the JPPF driver's JMX server.

See: Description

Package org.jppf.management.forwarding Description

Support for forwarding JMX requests to the nodes, along with receiving notifications from them, via the JPPF driver's JMX server. Which nodes are impacted is determined by a user-provided node selector.

This provides two major benefits:

Forwarding management requests

The request forwarding mechanism is based on a built-in driver MBean: JPPFNodeForwardingMBean, which provides methods to invoke methods, or get or set attributes on remote node MBeans. Each of its methods requires a NodeSelector argument and an MBean name, to determine to which nodes, and which MBean in these nodes, the request will be performed. The return value is always a map of node UUIDs to the corresponding value returned by the request (if any) to the corresponding node. If an exception is raised when performing the request on a specific node, then that exception is returned in the map. Here is an example:

 JPPFClient client = ...;
 AbstractJPPFClientConnection conn = (AbstractJPPFClientConnection) client.getClientConnection();
 JMXDriverConnectionWrapper driverJmx = conn.getJmxConnection();
 
 JPPFNodeForwardingMBean proxy =
   driverJmx.getProxy(JPPFNodeForwardingMBean.MBEAN_NAME, JPPFNodeForwardingMBean.class);
 
 // this selector selects all nodes attached to the driver
 NodeSelector selector = new NodeSelector.AllNodes();
 // this selector selects all nodes that have more than 2 processors
 ExecutionPolicy policy = new MoreThan("availableProcessors ", 2);
 NodeSelector selector2 = new NodeSelector.ExecutionPolicySelector(policy);
 
 // invoke the state() method on the remote 'JPPFNodeAdminMBean' node MBeans.
 // note that the MBean name does not need to be stated explicitely.
 Map<String, Object> results = proxy.state(selector);
 // this is an exact equivalent, explicitely stating the target MBean on the remote nodes:
 String targetMBeanName = JPPFNodeAdminMBean.MBEAN_NAME;
 Map<String, Object> results2 = proxy.forwardInvoke(selector, targetMBeanName, "state");
 
 // handling the results
 for (Map.Entry<String, Object> entry: results) {
   if (entry.getValue() instanceof Exception) {
     // handle the exception ...
   } else {
     JPPFNodeState state = (JPPFNodeState) entry.getValue();
     // handle the result ...
   }
 }
 

Forwarding JMX notifications

JPPF provides a way to subscribe to notifications from a set of selected nodes, which differs from the one specified in the JMX API. This is due to the fact that the server-side mechanism for the registration of notification listeners is unspecified and thus provides no reliable way of overriding it.

To circumvent this, the notification listener registration is performed via the JMX client wrapper JMXDriverConnectionWrapper:

The notifications forwarded from the nodes are all wrapped into instances of JPPFNodeForwardingNotification. This class, which inherits from Notification, provides additional APIs to identify from which node and which MBean the notification was emitted.

The following code sample puts it all together:

 JPPFClient client = ...;
 AbstractJPPFClientConnection conn = (AbstractJPPFClientConnection) client.getClientConnection();
 JMXDriverConnectionWrapper driverJmx = conn.getJmxConnection();
 // this selector selects all nodes attached to the driver
 NodeSelector selector = new NodeSelector.AllNodes();
 
 // create a JMX notification listener
 NotificationListener myListener = new NotificationListener() {
   @Override
   public void handleNotification(Notification notification, Object handback) {
     JPPFNodeForwardingNotification notif = (JPPFNodeForwardingNotification) notification;
     System.out.println("received notification from nodeUuid=" + notif.getNodeUuid()
       + ", mBeanName=" + notif.getMBeanName());
     // get the actual notification sent by the node
     TaskExecutionNotification actualNotif = (TaskExecutionNotification) notif.getNotification();
     // handle the notification data ...
   }
 }
 
 // register the notification listener with the JPPFNodeTaskMonitorMBean on the selected nodes
 String listenerID = driverJmx.registerForwardingNotificationListener(
   selector, JPPFNodeTaskMonitorMBean.MBEAN_NAME, listener, null, null);
 
 // ... submit a JPPF job ...
 
 // once the job has completed, unregister the notification listener
 driverJmx.unregisterForwardingNotificationListener(listenerID);
 

Copyright © 2005-2012 JPPF Team.