在一台机器上通过JMX进行跨JVM的本地调用

本文介绍了如何在同一个机器上利用JMX技术实现跨JVM的本地调用,通过创建MBeanServerConnection,可以在不开启新端口的情况下进行高效便捷的交互。

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

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
/**
 * 
 * 通过jmx本地连接
 */
public class JmxLocalConnector {

    private VirtualMachine virtualmachine;
    private JMXConnector connector;

    /**
     * 
     *pid为连接的进程id
     */
    public MBeanServerConnection connect(String pid)
            throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
        String address = attachJmx(pid);

        JMXServiceURL serviceURL = new JMXServiceURL(address);
        connector = JMXConnectorFactory.connect(serviceURL);
        return connector.getMBeanServerConnection();
    }

    public void disConnect() {
        if (virtualmachine != null) {
            try {
                virtualmachine.detach();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (connector != null) {
            try {
                connector.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private String attachJmx(String pid)
            throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
        virtualmachine = VirtualMachine.attach(pid);
        String javaHome = virtualmachine.getSystemProperties().getProperty("java.home");
        String agentPath = javaHome + File.separator + "jre" + File.separator + "lib" + File.separator
                + "management-agent.jar";
        File file = new File(agentPath);
        if (!file.exists()) {
            agentPath = javaHome + File.separator + "lib" + File.separator + "management-agent.jar";
            file = new File(agentPath);
            if (!file.exists())
                throw new IOException("Management agent not found");
        }

        agentPath = file.getCanonicalPath();
        virtualmachine.loadAgent(agentPath, "com.sun.management.jmxremote");

        Properties properties = virtualmachine.getAgentProperties();
        String address = (String) properties.get("com.sun.management.jmxremote.localConnectorAddress");
        virtualmachine.detach();
        return address;

    }
}

通过以上方式创建出MBeanServerConnection ,即可通过它对另一个JVM进行JMX调用,并不需要新开端口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值