我正在尝试远程配置在我的测试代码中运行1.8 JVM和Apache Tomcat 7.xx的64位Linux服务器上运行的露天,但无法弄清楚如何以编程方式触发快照。
我想要做的是连接到远程服务器,开始分析,并从我用Java编写的测试代码中将该服务器性能的快照保存到我的本地机器上。
我已经将JProfiler 9.2安装到linux服务器上,可以通过JProfiler GUI连接并拍摄快照。服务器还需要SSH连接以确保安全性。我想从我的代码中执行此操作,类似于Controller.saveSnapshot(file)如何为本地JVM工作。
这可能吗?
我知道我可以设置触发器并让远程探查器在服务器上保存快照,但这不是我想要的。
此外,我研究了使用命令行控制器,但即使远程VM选项中的参数正确,也无法连接到服务器。
我也试过使用ConnectionFactor.createRemoteConnection(),但是没有看到允许输入密码的参数,所以它失败了。
答案 0 :(得分:1)
您可以通过编程方式访问JProfiler MBean。下面是一个如何做到这一点的例子。我会在远程计算机上运行这样的程序并通过SSH启动它,因为JMX连接很难通过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");
}
}