📄 jmxwrapper.java
字号:
XmlBlasterSecurityManager.createSecurityManager(glob); try { if (registryPort > 0) { // Start a 'rmiregistry' if desired try { java.rmi.registry.LocateRegistry.createRegistry(registryPort); log.info("Started RMI registry on port " + registryPort); } catch (java.rmi.server.ExportException e) { // Try to bind to an already running registry: try { java.rmi.registry.LocateRegistry.getRegistry(registryHost, registryPort); log.info("Another rmiregistry is running on port " + DEFAULT_REGISTRY_PORT + " we will use this one. You could change the port with e.g. '-xmlBlaster/jmx/rmiregistry/port 1122' to run your own rmiregistry."); } catch (RemoteException e2) { String text = "Port " + DEFAULT_REGISTRY_PORT + " is already in use, but does not seem to be a rmiregistry. Please can change the port with e.g. -xmlBlaster/jmx/rmiregistry/port 1122 : " + e.toString(); throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_UNAVAILABLE, ME, text, e2); } } } } catch (Exception e) { throw new XmlBlasterException(this.glob, ErrorCode.RESOURCE_UNAVAILABLE, ME, " could not initialize RMI registry", e); } if (log.isLoggable(Level.FINE)) log.fine("Initialized RMI registry"); } /** * Reorganize the registration for a new parent node. * Newer variant * @param oldName The existing registry, is used to lookup all matching sub-entries. * For example "org.xmlBlaster:nodeClass=node,node=clientSUB1" * @param oldRootClassname The existing parent node like * "org.xmlBlaster:nodeClass=node,node=clientjoe1" * Note: Instance names may not contain commas "," for example "joe,Smith" is not valid * @return The resulting registration like * "org.xmlBlaster:nodeClass=node,node=heron,clientClass=connection,connection=jack,queueClass=queue,queue=connection-99" */ public synchronized int renameMBean(String oldName, String oldRootClassname, final ContextNode newRootNode) { if (this.mbeanServer == null) return 0; if (useJmx == 0) return 0; if (oldName == null || newRootNode == null) return 0; String newRootName = newRootNode.getAbsoluteName(ContextNode.SCHEMA_JMX); int count = 0; if (log.isLoggable(Level.FINER)) log.finer("JMX rename registration from '" + oldName + "' to new root '" + newRootName + "'"); try { // e.g. "org.xmlBlaster:nodeClass=node,node=clientSUB1,*" ObjectName query = new ObjectName(oldName+",*"); QueryExp queryExp = null; Set mbeanSet = this.mbeanServer.queryMBeans(query, queryExp); if (mbeanSet.size() == 0) { if (log.isLoggable(Level.FINE)) log.fine("JMX rename registration from '" + oldName + "' to '" + newRootName + "', nothing found for '" + query + "'"); return count; } Iterator it = mbeanSet.iterator(); while (it.hasNext()) { ObjectInstance instance = (ObjectInstance)it.next(); ObjectName tmp = instance.getObjectName(); JmxMBeanHandle mbeanHandle = (JmxMBeanHandle)this.mbeanMap.get(tmp.toString()); if (mbeanHandle == null) { log.severe("Internal problem: Can't find registration of MBean '" + tmp.toString() + "'"); continue; } // /node/heron/connection/joe/session/2 ContextNode newRoot = ContextNode.valueOf(newRootName); // /node/clientjoe1/service/Pop3Driver ContextNode current = ContextNode.valueOf(tmp.toString()); if (current == null) continue; // /node/clientjoe1 ContextNode parent = current.getParent(oldRootClassname); if (parent == null) continue; // service/Pop3Driver ContextNode[] childs = parent.getChildren(); if (childs.length != 1) continue; this.mbeanServer.unregisterMBean(tmp); this.mbeanMap.remove(tmp.toString()); // take this leg and attach it to the newRoot ContextNode result = newRoot.mergeChildTree(childs[0]); if (log.isLoggable(Level.FINE)) log.fine("Renamed '" + oldName + "' to '" + result.getAbsoluteName(ContextNode.SCHEMA_JMX) + "'"); if (result != null) { registerMBean(result, mbeanHandle.getMBean(), mbeanHandle); this.mbeanMap.put(mbeanHandle.getObjectInstance().getObjectName().toString(), mbeanHandle); } else { //Thread.dumpStack(); log.warning("Renamed '" + current.getAbsoluteName(ContextNode.SCHEMA_JMX) + "' to '" + newRoot.getAbsoluteName(ContextNode.SCHEMA_JMX) + "' failed, JMX bean is not registered"); } count++; } return count; } catch (Exception e) { log.severe("JMX rename registration problem from '" + oldName + "' to '" + newRootName + "': " + e.toString()); e.printStackTrace(); return count; } } /** * Reorganize the registration for a new parent node. * Can we set this to deprecated? * @param oldName The existing registry, is used to lookup all matching sub-entries. * For example "org.xmlBlaster:nodeClass=node,node=clientSUB1" * @param newNodeClass The new parent node like * "org.xmlBlaster:nodeClass=node,node=heron" * Note: Instance names may not contain commas "," for example "joe,Smith" is not valid * @return The resulting registration like * "org.xmlBlaster:nodeClass=node,node=heron,clientClass=connection,connection=jack,queueClass=queue,queue=connection-99" */ public synchronized int renameMBean(String oldName, String classNameToChange, String instanceName) { if (this.mbeanServer == null) return 0; if (useJmx == 0) return 0; if (oldName == null || classNameToChange == null) return 0; int count = 0; if (log.isLoggable(Level.FINER)) log.finer("JMX rename registration from '" + oldName + "' to '" + classNameToChange + "=" + instanceName + "'"); try { // e.g. "org.xmlBlaster:nodeClass=node,node=clientSUB1,*" ObjectName query = new ObjectName(oldName+",*"); QueryExp queryExp = null; Set mbeanSet = this.mbeanServer.queryMBeans(query, queryExp); if (mbeanSet.size() == 0) { if (log.isLoggable(Level.FINE)) log.fine("JMX rename registration from '" + oldName + "' to '" + classNameToChange + "=" + instanceName + "', nothing found for '" + query + "'"); return count; } Iterator it = mbeanSet.iterator(); while (it.hasNext()) { ObjectInstance instance = (ObjectInstance)it.next(); ObjectName tmp = instance.getObjectName(); JmxMBeanHandle mbeanHandle = (JmxMBeanHandle)this.mbeanMap.get(tmp.toString()); if (mbeanHandle == null) { log.severe("Internal problem: Can't find registration of MBean '" + tmp.toString() + "'"); continue; } this.mbeanServer.unregisterMBean(tmp); this.mbeanMap.remove(tmp.toString()); ContextNode renamed = ContextNode.valueOf(tmp.toString()); renamed.changeParentName(classNameToChange, instanceName); if (log.isLoggable(Level.FINE)) log.fine("Renamed '" + oldName + "' to '" + renamed.getAbsoluteName(ContextNode.SCHEMA_JMX) + "'"); registerMBean(renamed, mbeanHandle.getMBean(), mbeanHandle); this.mbeanMap.put(mbeanHandle.getObjectInstance().getObjectName().toString(), mbeanHandle); count++; } return count; } catch (Exception e) { log.severe("JMX rename registration problem from '" + oldName + "' to '" + classNameToChange + "=" + instanceName + "': " + e.toString()); e.printStackTrace(); return count; } } /** * Registers the specified mbean into the mbean server. * A typical registration string is * <tt>org.xmlBlaster:nodeClass=node,node="heron",clientClass=client,client="joe",queueClass=queue,queue="subject665",entryClass=entry,entry="1002"</tt> * which doesn't conform to the ObjectName recommendation as it does not * contains the 'type=...' property. * We have chosen this as it creates a nice hierarchy in the jconsole GUI tool. * @param contextNode The unique name for JMX observation * Note: Instance names may not contain commas "," for example "joe,Smith" is not valid * @param mbean the MBean object instance * If mbean implements MBeanRegistration:preRegister() we don't need the type, name * @return The JmxMBeanHandle with object name used to register or null on error * Note: You may not take a clone of it as we may change attributes of this instance * during renaming operations. */ public synchronized JmxMBeanHandle registerMBean(ContextNode contextNode, Object mbean) { return registerMBean(contextNode, mbean, null); } public static String getObjectNameLiteral(Global global, ContextNode contextNode) { String hierarchy = (contextNode == null) ? ("org.xmlBlaster:type=" + global.getId()) : contextNode.getAbsoluteName(ContextNode.SCHEMA_JMX); return hierarchy; } private synchronized JmxMBeanHandle registerMBean(ContextNode contextNode, Object mbean, JmxMBeanHandle mbeanHandle) { if (this.mbeanServer == null) return null; if (useJmx == 0) return null; if (contextNode == null) return null; // Remove? code below handles a null if (log.isLoggable(Level.FINER)) log.finer("registerMBean(" + contextNode.getAbsoluteName(ContextNode.SCHEMA_JMX) + ")"); String hierarchy = getObjectNameLiteral(this.glob, contextNode); ObjectName objectName = null; try { objectName = new ObjectName( getObjectNameLiteral(this.glob, contextNode)); ObjectInstance objectInstance = this.mbeanServer.registerMBean(mbean, objectName); if (mbeanHandle == null) { mbeanHandle = new JmxMBeanHandle(objectInstance, contextNode, mbean); this.mbeanMap.put(objectName.toString(), mbeanHandle); } else { // Update the mbeanHandle of the registrar wit the new ObjectName: mbeanHandle.setObjectInstance(objectInstance); } if (log.isLoggable(Level.FINE)) log.fine("Registered MBean '" + objectName.toString() + "' for JMX monitoring and control"); return mbeanHandle; } catch (javax.management.InstanceAlreadyExistsException e) { if (objectName != null) { log.warning("JMX entry exists already, we replace it with new one: " + e.toString()); // this.mbeanMap.remove(objectName.toString()); is done in unregisterMBean unregisterMBean(objectName); return registerMBean(contextNode, mbean); } log.warning("Ignoring JMX registration problem for '" + ((objectName==null)?hierarchy:objectName.toString()) + "': " + e.toString()); return null; } catch (Exception e) { log.severe("JMX registration problem for '" + ((objectName==null)?hierarchy:objectName.toString()) + "': " + e.toString()); e.printStackTrace(); return null; } } /** * Similat to invokeAction but the command does not need to start with * "/InvokeAction//", additionally the "set" feature is activated. * @param command for example "org.xmlBlaster:nodeClass=node,node="heron"/action=getFreeMemStr" * @return The methods return value * @see #invokeAction(String) */ public Object invokeCommand(final String command) { String tmp = "/InvokeAction//"+command.trim(); System.setProperty("jmx.invoke.getters", "set"); System.out.println("Invoking: " + tmp); Object obj = invokeAction(tmp); System.getProperties().remove("jmx.invoke.getters"); return obj; } /** * Invoke xmlBlaster core JMX bean operations and getter/setter. * To invoke getter/seeter you need to start with<br /> * <code> * java -Djmx.invoke.getters=set ... org.xmlBlaster.Main * </code> * @param args could be somethig like: * '/InvokeAction//org.xmlBlaster:nodeClass=node,node="izar",contribClass=contrib,contrib="Heini" * /action=myNiceMethodName?action=myNiceMethodName&p1+java.lang.String=arg1&p2+java.lang.String=arg1' * or * 'org.xmlBlaster:nodeClass=node,node="heron",topicClass=topic,topic="watchDog"/action=getNumOfHistoryEntries' * or * 'org.xmlBlaster:nodeClass=node,node="heron",topicClass=topic,topic="watchDog"/action=usage' * or * 'j org.xmlBlaster:nodeClass=node,node="heron",topicClass=topic,topic="watchDog"/action=peekHistoryMessages?p1+int=1 * * or much simpler (will return the usage() string): * * org.xmlBlaster:nodeClass=node,node="heron"/action=usage */ public Object invokeAction(final String args) {/*Eamonn, Piyush: Maybe I am confused. Whereas what Eamonn says is right, (withregards to the invoke.getters property etc.) in your original codedidn't you have "state" as the name of the attribute? If that is the case, the method to invoke is "setstate" and not
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -