gpt4 book ai didi

java - 从本地触发远程服务器上的快照

转载 作者:行者123 更新时间:2023-11-30 10:41:55 27 4
gpt4 key购买 nike

我正在尝试从我的测试代码远程分析在运行 1.8 JVM 和 Apache Tomcat 7.xx 的 64 位 Linux 服务器上运行的 alfresco,但无法弄清楚如何以编程方式触发快照。

我想要做的是连接到远程服务器,开始分析,并将该服务器的性能快照从我用 Java 编写的测试代码保存到我的本地机器上。

我已经在 Linux 服务器上安装了 JProfiler 9.2,并且可以通过 JProfiler GUI 进行连接和拍摄快照。服务器还需要 SSH 连接以确保安全。我想从我的代码中执行此操作,类似于 Controller.saveSnapshot(file) 对本地 JVM 的工作方式。

这可能吗?

我知道我可以设置触发器并让远程分析器在服务器上保存快照,但这不是我想要的。

此外,我研究了使用命令行 Controller ,但即使远程 VM 选项中的参数正确,也无法让它连接到服务器。

我还尝试使用 ConnectionFactor.createRemoteConnection(),但没有看到允许输入密码的参数,因此失败了。

最佳答案

您可以通过编程方式访问 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");

}
}

关于java - 从本地触发远程服务器上的快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38340353/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com