首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从本地触发远程服务器上的快照

问从本地触发远程服务器上的快照
EN

Stack Overflow用户
提问于 2016-07-13 07:25:11
回答 1查看 217关注 0票数 2

我正试图从我的测试代码中远程分析运行在运行1.8 JVM和Apache Tomcat 7.xx的64位linux服务器上的alfresco,但是我不知道如何以编程方式触发快照。

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

我已经在JProfiler服务器上安装了Linux9.2,可以通过JProfiler图形用户界面连接和拍摄快照。出于安全性考虑,服务器还需要SSH连接。我想在我的代码中做到这一点,类似于Controller.saveSnapshot(文件)对本地JVM的工作方式。

这个是可能的吗?

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

此外,我尝试使用命令行控制器,但即使在远程VM选项中使用正确的参数,也无法使其连接到服务器。

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

EN

回答 1

Stack Overflow用户

发布于 2016-07-13 17:11:24

您可以通过编程方式访问JProfiler MBean。下面是一个如何做到这一点的例子。我会在远程机器上运行这样一个程序,并通过SSH启动它,因为JMX连接很难通过SSH进行隧道传输。

代码语言:javascript
复制
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");

    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38340353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档