2016-07-12 2 views
1

테스트 코드에서 1.8 JVM 및 Apache Tomcat 7.xx를 실행하는 64 비트 Linux 서버에서 실행중인 alfresco를 원격으로 프로파일하려고하지만 프로그램 적으로 스냅 샷을 트리거하는 방법을 알 수 없습니다 .로컬에서 원격 서버의 스냅 샷 트리거

원격 서버에 연결하고 프로파일 링을 시작한 다음 Java로 작성된 테스트 코드에서 로컬 서버로 성능의 스냅 샷을 저장합니다.

이미 JProfiler 9.2를 Linux 서버에 설치했으며 JProfiler GUI를 통해 스냅 샷을 연결하고 가져갈 수 있습니다. 또한 서버는 보안을 위해 SSH 연결이 필요합니다. Controller.saveSnapshot (file)이 로컬 JVM에서 작동하는 것과 비슷한 코드에서이 작업을 수행하고 싶습니다.

이것이 가능합니까?

나는 트리거를 설정할 수 있고 원격 프로파일 러가 서버에 스냅 샷을 저장할 수 있지만 이것이 내가 원하는 것은 아님을 알고있다.

또한 명령 줄 컨트롤러를 사용했지만 원격 VM 옵션의 올바른 인수가있는 경우에도 서버에 연결하지 못했습니다.

또한 ConnectionFactor.createRemoteConnection()을 사용하려고했지만 암호 입력을 허용하는 인수가 표시되지 않으므로 실패합니다.

답변

0

프로그래밍 방식으로 JProfiler MBean에 액세스 할 수 있습니다. 다음은이를 수행하는 방법에 대한 예제입니다. JMX 연결은 SSH를 통해 터널링하기가 어렵 기 때문에 원격 시스템에서 이러한 프로그램을 실행하고 SSH를 통해 시작합니다.

import javax.management.MBeanServerConnection; 
import javax.management.ObjectName; 
import javax.management.remote.JMXConnector; 
import javax.management.remote.JMXConnectorFactory; 
import javax.management.remote.JMXServiceURL; 
import java.util.Collections; 
import java.util.HashMap; 
import java.util.Map; 

// Shows how to connect to the the JProfiler MBean programatically 
// The profiled process has to be in offline profiling mode and the JMX server 
// has to be started by passing -Djprofiler.jmxServerPort=[port] to the profiled JVM. 
// This will not work in nowait mode because the MBean is not registered in that case. 
public class MBeanProgrammaticAccessExample { 

    public static void main(String[] args) throws Exception { 
     if (args.length == 0) { 
      System.out.println("Specify the port as an argument that was passed to " + 
        "the profiled JVM with the VM parameter " + 
        "-Djprofiler.jmxServerPort=[port]"); 
     } 
     String port = args[0]; 
     // In this case the connection is made to a process on localhost, but it could 
     // be on a remote system as well. Note that the connection is made via JMX which 
     // does not work well with firewalls 
     System.out.println("Connecting to localhost:" + port); 
     JMXServiceURL jmxUrl = new JMXServiceURL(
       "service:jmx:rmi:///jndi/rmi://localhost:" + port + "/jmxrmi"); 
     JMXConnector connector = JMXConnectorFactory.newJMXConnector(jmxUrl, 
       Collections.<String, Object>emptyMap()); 

     Map<String, Object> env = new HashMap<>(); 

     // If you have protected the JMX server with a JMX password file by passing 
     // -Djprofiler.jmxPasswordFile=[file] to the profiled JVM, you can specify 
     // the password like this: 
     //env.put(JMXConnector.CREDENTIALS, new String[] {"username", "password"}); 

     connector.connect(env); 
     MBeanServerConnection connection = connector.getMBeanServerConnection(); 
     ObjectName objectName = new ObjectName(
       "com.jprofiler.api.agent.mbean:type=RemoteController"); 
     if (!connection.isRegistered(objectName)) { 
      throw new RuntimeException("JProfiler MBean not found."); 
     } 

     RemoteControllerMBean mbeanProxy = JMX.newMBeanProxy(connection, 
       objectName, RemoteControllerMBean.class, true); 

     // You can look up all available operations in the javadoc of 
     // com.jprofiler.api.agent.mbean.RemoteControllerMBean 
     System.out.println("Recording CPU data for 5 seconds ...."); 
     mbeanProxy.startCPURecording(true); 
     // If you do not want a dependency on the JProfiler classes 
     // you can make the above call like this: 
     //connection.invoke(objectName, "startCPURecording", new Object[] {true}, 
     // new String[] {Boolean.TYPE.getName()}); 

     Thread.sleep(5000); 
     System.out.println("Saving snapshot to the working directory " + 
       "of the profiled JVM ...."); 
     mbeanProxy.saveSnapshot("snapshot.jps"); 

     connector.close(); 
     System.out.println("Success"); 

    } 
}