java - JMX 线程未关闭

标签 java multithreading jmx

当我通过以下代码通过线程进行JMX连接时,

private  JMXConnector initConnection() throws Exception{
    JMXServiceURL serviceURL = null;

    try {
        String URL = MessageFormat.format(connectorURL, new Object[]{hostName, port});
        serviceURL = new JMXServiceURL(URL);

        final Map<String, String[]> environment = new HashMap<String, String[]>();
        environment.put(JMXConnector.CREDENTIALS, new String[]{userName, password});

        return JMXConnectorFactory.connect(serviceURL, environment);
    }
    catch (Exception e)
    {
        throw e;
    }

}

即使我关闭了连接并销毁了创建 jmx 连接的线程,以下线程也不会被破坏

GC Daemon、RMI RenewClean、RMI Scheduler(0) 这些线程不会在 Java JMX 连接中销毁。

连接关闭中的代码

public void closeConnection() {
    if(jmxc != null){
        try{
            jmxc.close();
        }
        catch (IOException e) {
            jmxc = null;
        }
    }
}



public void createMBeanServerConnection() throws Exception
{
    try
    {  
        jmxc  = initConnection();

        mbServerConnection = jmxc.getMBeanServerConnection();
    }
    catch (Exception e)
    {
        throw e;
    }  
 }

这是完整的上下文

public class Test1 
{
    public static void main(String[] args) throws Exception 
    {

        Thread.sleep(120000);
        Test t = new Test();
        t.main(args);
        Thread.sleep(120000);
    }

}







public class Test
{

    private String hostName = "";
    private String port = "";
    private String userName = "";
    private String password = "";
    private String connectorURL = "service:jmx:rmi:///jndi/rmi://{0}:{1}/jmxrmi";
    private JMXConnector jmxc = null;
    public static void main(String []args) throws Exception
    {
        Test t = new Test();
        t.hostName = args[0];
        System.out.println(args[1]);
        t.port = args[1];
        t.jmxc = t.initConnection(); 
        MBeanServerConnection mbsc = t.jmxc.getMBeanServerConnection();
        mbsc.queryMBeans(new ObjectName("*.*:*"), null);
        t.closeConnection();
    }

    private  JMXConnector initConnection() 
    {

        JMXServiceURL serviceURL = null;

        try 
        {

            String URL = MessageFormat.format(connectorURL, new Object[]{hostName, port});
            System.out.println(URL);
            serviceURL = new JMXServiceURL(URL);
            final Map<String, String[]> environment = new HashMap<String, String[]>();
            environment.put(JMXConnector.CREDENTIALS, new String[]{userName, password});
            System.out.println(serviceURL);

            return JMXConnectorFactory.connect(serviceURL, environment);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }

    }


    public void closeConnection() 
    {

        if(jmxc != null)
        {
            try
            {
                jmxc.close();
            }
            catch (IOException e) {
                jmxc = null;
            }
        }
    }
}

最佳答案

您应该完成任务,然后关闭连接

public void createMBeanServerConnection() throws Exception
{
    try
    {  
        jmxc  = initConnection();

        mbServerConnection = jmxc.getMBeanServerConnection();

        doYourThing();

    }
    catch (Exception e)
    {
        throw e;

    }  finally {

        closeConnection();

    }

 }

关于java - JMX 线程未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24648509/

相关文章:

java - maven-ear-plugin 忽略模块的解包选项

c# - 具有 Java 和 C# 后端的 JQuery 前端 : design choices

java - 通配符子域的单独 IP 地址

c++ - 更改线程标准输入和输出

java - JMX - Pivotal Cloud Foundry

java - 使用 JSch 通过 SSH 隧道访问生产中的 JMX

jmx - 在 Perfmon 中查看的 Java 应用程序性能计数器

java - 是否需要 Java 才能运行某些基于 .NET 的程序?

Java 多线程类方法?

java - 在自己的构造函数中调用 thread.start()