2014-07-09 49 views
2

,当我用下面的代码做JMX connection通过一个线程,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; 
      } 
     } 
    } 
} 
+0

应该工作,两周前自己也有同样的问题。 closeConnection在finally块中调用它来调用它? –

+0

也尝试打印出任何IOException –

+0

如果从服务器端(远程位置)关闭连接,则比我将jmxc设置为null。 – user2572969

回答

3

你应该做你的任务,则关闭连接

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

     mbServerConnection = jmxc.getMBeanServerConnection(); 

     doYourThing(); 

    } 
    catch (Exception e) 
    { 
     throw e; 

    } finally { 

     closeConnection(); 

    } 

}