This is part of the larger task of developing Configuration Manager Proxy (CMP) applications.
It is possible to notify applications whenever commands complete, or whenever changes occur to administered objects. By making use of the OBSERVER design pattern, it is possible to supply the CMP with a handle to a user-supplied object that has a specific method invoked if an object is modified, deleted, or whenever a response to a previously submitted action is returned from the Configuration Manager.
processDelete(…) is invoked if the object with which the listener is registered is completely removed from the Configuration Manager. Supplied to processDelete(…) is one parameter – a handle to the administered object that has been deleted; once this method returns, the administered object handle might no longer be valid. Around the same time that a processDelete(…) event occurs, a processModify(…) event is sent to listeners of the deleted object's parent, to announce a change in the parent's list of subcomponents.
In order to register a listener, each administered object has a registerListener() method that is used to tell the CMP to call the supplied code whenever an event occurs on that object. It is possible to register the same AdministeredObjectListener for notifications from multiple administered objects. In addition, it is possible to register multiple AdministeredObjectListeners against the same administered object.
import com.ibm.broker.config.proxy.*; import com.ibm.broker.config.common.CompletionCodeType; import java.util.List; import java.util.ListIterator; import java.util.Properties; public class MonitorTopology implements AdministeredObjectListener { public static void main(String[] args) { ConfigManagerProxy cmp = null; try { ConfigManagerConnectionParameters cmcp = new MQConfigManagerConnectionParameters( "localhost", 1414, ""); cmp = ConfigManagerProxy.getInstance(cmcp); } catch (ConfigManagerProxyException cmpex) { System.out.println("Error connecting: "+cmpex); } if (cmp != null) { System.out.println("Connected to Config Manager!"); TopologyProxy topology = cmp.getTopology(); listenForChanges(topology); cmp.disconnect(); } } private static void listenForChanges(AdministeredObject obj) { try { if (obj != null) { obj.registerListener(new MonitorTopology()); while(true) { // thread could do something else here instead try { Thread.sleep(10000); } catch (InterruptedException ex) { // ignore } } } } catch(ConfigManagerProxyPropertyNotInitializedException ex) { System.err.println("Comms problem! "+ex); } } public void processActionResponse(AdministeredObject obj, CompletionCodeType cc, List bipMessages, Properties refProperties) { // Event ignored in this example } public void processDelete(AdministeredObject deletedObject) { // Event ignored in this example } public void processModify(AdministeredObject affectedObject, List changedAttributes, List newChildren, List removedChildren) { System.out.println(affectedObject+" has changed:"); ListIterator e = changedAttributes.listIterator(); while (e.hasNext()) { String changedAttribute = (String) e.next(); System.out.println("Changed: "+changedAttribute); } ListIterator e2 = newChildren.listIterator(); while (e2.hasNext()) { String newChildStr = (String) e2.next(); AdministeredObject newChild = affectedObject.getSubcomponentFromString(newChildStr); System.out.println("New child: "+newChild); } ListIterator e3 = removedChildren.listIterator(); while (e3.hasNext()) { String remChildStr = (String) e3.next(); AdministeredObject removedChild = affectedObject.getSubcomponentFromString(remChildStr); System.out.println("Removed child: "+removedChild); } } }
The listenForChanges() method attempts to register an instance of the MonitorTopology class for notifications of topology changes. If successful, the main thread pauses indefinitely to prevent the application from exiting once the method returns. Once the listener is registered, whenever the topology changes - for example, if a broker is added - the processModify() method is called. This displays details of each notification on the screen.