2014-07-09 2 views
2

quand je fais JMX connection par un fil par le code suivant,fils JMX ne ferment pas

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; 
    } 

} 

threads suivants ne sont pas en train de détruire, même si je fermé la connexion et détruit le fil qui crée une connexion JMX

GC Daemon, RMI RenewClean, RMI Scheduler (0) ces threads ne détruisent pas dans la connexion Java JMX.

Code

en lien étroit

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; 
    } 
} 

Tel est le contexte complet

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

devrait fonctionner, avait le même problème moi-même il ya deux semaines. Est-ce que closeConnection est appelé dans un bloc finally pour qu'il soit appelé? –

+0

essayez également d'imprimer une exception IOException –

+0

Si la connexion est fermée côté serveur (emplacement distant), alors jmxc est vide. – user2572969

Répondre

3

Vous devez faire votre tâche, puis fermer la connexion.

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

     mbServerConnection = jmxc.getMBeanServerConnection(); 

     doYourThing(); 

    } 
    catch (Exception e) 
    { 
     throw e; 

    } finally { 

     closeConnection(); 

    } 

} 
Questions connexes