Remote JMX 调用

本文提供了一个详细的指南,展示了如何使用RMI适配器进行远程访问MBean,包括引入必要的包、初始化上下文、查找适配器并执行属性获取和方法调用的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Reference: 

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]);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值