2012-11-06 2 views
1

이것의 동기는 JMX를 사용하여 지역 서비스를 관리하는 것입니다.프로세스 ID를 알고 로컬 JMX 서버에 연결하는 방법은 무엇입니까?

각 서비스는 -Dcom.sun.management.jmxremote으로 시작됩니다. 즉, "JVM이 로컬 (동일한 컴퓨터 전용) JMX 서버로 작동하도록 구성되어 있음"을 의미합니다. (좋은 설명은 here을 참조하십시오).

나는 Attach API을 시도했지만 Java SE6에 번들로 제공되지 않았기 때문에이를 결정했다. 그리고이를 maven과 통합 할 수 없었다.

답변

2

@ 팀 쎄

ConnectorAddressLink.importFrom 반환 null의 경우, VM에 관리 - agent.jar를로드하십시오. https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/test/sun/management/jmxremote/bootstrap/TestManager.java

private static void startManagementAgent(String pid) throws IOException { 
    /* 
    * JAR file normally in ${java.home}/jre/lib but may be in ${java.home}/lib 
    * with development/non-images builds 
    */ 
    String home = System.getProperty("java.home"); 
    String agent = home + File.separator + "jre" + File.separator + "lib" 
      + File.separator + "management-agent.jar"; 
    File f = new File(agent); 
    if (!f.exists()) { 
     agent = home + File.separator + "lib" + File.separator + 
      "management-agent.jar"; 
     f = new File(agent); 
     if (!f.exists()) { 
      throw new RuntimeException("management-agent.jar missing"); 
     } 
    } 
    agent = f.getCanonicalPath(); 

    System.out.println("Loading " + agent + " into target VM ..."); 

    try { 
     VirtualMachine.attach(pid).loadAgent(agent); 
    } catch (Exception x) { 
     throw new IOException(x.getMessage()); 
    } 
} 
4

여기 (Q & A)를 보지 못했기 때문에 솔루션을 공유하는 질문을 게시하고 있습니다. 여기에서 중요한 것은 ConnectorAddressLink.importFrom(pid)을 사용하여 주소를 얻는 것입니다.

public static MBeanServerConnection getLocalMBeanServerConnectionStatic(int pid) { 
    try { 
     String address = ConnectorAddressLink.importFrom(pid); 
     JMXServiceURL jmxUrl = new JMXServiceURL(address); 
     return JMXConnectorFactory.connect(jmxUrl).getMBeanServerConnection(); 
    } catch (IOException e) { 
     throw new RuntimeException("Of course you still have to implement a good connection handling"); 
    } 
} 
+0

에서 기능 startManagementAgent 같은 뭔가 예를 들어

가끔 작동하는 것 같다,하지만 때로는'ConnectorAddressLink.importFrom'는'null'을 반환 –

관련 문제