Remote JMX 调用

来源:互联网 发布:linux vi退出保存 编辑:程序博客网 时间:2024/05/21 15:45
Reference: 
https://community.jboss.org/wiki/HowDoIGetRemoteAccessToMyMBean

java 代码用法
The RMIAdaptor provides a remote view of the MBeanServer Note: Use the MBeanServerConnection interface rather than RMIAdaptor on the most recent versions of JBoss.  RMIAdaptor should not be used in version 6.0 (M3) or greater (you must use MBeanServerConnection).

//import org.jboss.jmx.adapter.rmi.RMIAdaptor;
import javax.management.MBeanServerConnection;

public void doSomething() throws Exception
{
InitialContext ctx = new InitialContext(); // From jndi.properties
//RMIAdaptor server = (RMIAdaptor) ctx.lookup("jmx/invoker/RMIAdaptor");
MBeanServerConnection server = (MBeanServerConnection) ctx.lookup("jmx/invoker/RMIAdaptor");
System.out.println(server.getAttribute(new ObjectName("MyDomain:key=property"), "AnAttribute"));
server.invoke(new ObjectName("MyDomain:key=property"), "doSomething", new Object[0], new String[0]);
}

// For AS 6.0 (M3) or greater, use the following example
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public void doSomething() throws Exception
{
String serverURL = "service:jmx:rmi:///jndi/rmi://localhost:1090/jmxrmi"
String username = null;
String password = null;
HashMap env = new HashMap();
if (username != null && password != null)
{
String[] creds = new String[2];
creds[0] = username;
creds[1] = password;
env.put(JMXConnector.CREDENTIALS, creds);
}
JMXServiceURL url = new JMXServiceURL(serverURL);
JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
// Remember to call jmxc.close() when you are done with server connection.
MbeanServerConnection server = jmxc.getMBeanServerConnection();
System.out.println(server.getAttribute(new ObjectName("MyDomain:key=property"), "AnAttribute"));
server.invoke(new ObjectName("MyDomain:key=property"), "doSomething", new Object[0], new String[0]);
}

调用shutdown:
server.invoke(new ObjectName("jboss.system:type=server"), "shutdown", new Object[0], new String[0]);