2014-07-09 53 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(); 

    } 

}