如何通过java代码设置JMX远程端口系统环境参数进行远程监控? [英] How to set JMX remote port system environment parameters through java code for remote monitoring?

查看:184
本文介绍了如何通过java代码设置JMX远程端口系统环境参数进行远程监控?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序需要动态(即在运行时)打开一个可用的套接字并在其上启动一个JMX代理。此JMX参数是在Java代码中设置的,而不是通过命令行设置的。这很好用。此后需要通过Java Visual VM
远程监控(即发出JMX命令等)

I have a program which requires dynamically (i.e. at run time) opening an available socket and start a JMX agent on it. This JMX parameters are being set inside the Java code and not through command line. This works fine. Thereafter it is needed to monitor( i.e issue JMX commands etc) through Java Visual VM remotely

程序中的RMI服务器代理是在线路上箱管理描述于:
http:/ /download.oracle.com/javase/6/docs/technotes/guides/management/agent.html

The RMI server agent in the program is on the lines of out of box management described at: http://download.oracle.com/javase/6/docs/technotes/guides/management/agent.html

我的问题可归纳为:
如何通过Java代码将这些命令行属性设置为系统级
,以便可以使用远程分析?

The question I have can be summarized as: How can such command line properties be set to the system level through the Java code, so that remote profiling can be used??

-Dcom.sun.management.jmxremote.port=1234

如果jmxremote.port和其他参数都是通过命令行设置的,
远程监控工作正常。我试图通过Java
而不是通过命令行找到一种方法。

If the "jmxremote.port" and other parameters are set through command line, remote monitoring works fine. I am trying to find a way to do this through Java and not through the command line.

程序无法通过命令行指定端口新的可用端口必须在运行时计算出来。

The program can not specify the port through the command line as the new available port has to be figured out at run time.

该过程需要远程监控,并且在本地工作正常。
如果未在命令行中指定以下参数,则Java Visual VM不会连接到该进程。

The process needs remote monitoring and it works fine locally. If the following parameters are not specified at command line, Java Visual VM does not connect to the process.

-Dcom.sun.management.jmxremote.port=1234
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=10.0.0.128

我试过了。

System.setProperty("com.sun.management.jmxremote.port",Integer.toString(port));

这是在启动JMXConnectorServer之前在程序中完成的第一件事。不幸的是,它不被承认。只有运行时属性(即通过命令行指定由Java Visual VM识别JMX连接)。

This is one of the first things done in the program before starting the JMXConnectorServer. Unfortunately it is not recognized. Only the run time properties (i.e. specified through command line are recognized for JMX connection by Java Visual VM).

还遇到了从java集合类中提取属性的方式但无法达到如何追踪属性com.sun.management.jmxremote.port =

Also came across the way properties can be extracted from java collection classes but could not reach how to trace the property "com.sun.management.jmxremote.port="

public static void setEnv(Map<String, String> newenv) throws Exception {
  Class[] classes = Collections.class.getDeclaredClasses();
  Map<String, String> env = System.getenv();

  for(Class cl : classes) {

    if("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {

      Field field = cl.getDeclaredField("m");
      field.setAccessible(true);

      Object obj = field.get(env);
      Map<String, String> map = (Map<String, String>) obj;

      //map.clear();
      map.putAll(newenv);
    }
  }
}

任何帮助将不胜感激!

Any help would be appreciated!

推荐答案

kbec的答案显示方式,但对我不起作用 - 但是通过查看这篇文章我能够修改它并获得一个有效的解决方案。

kbec's answer showed the way but did not work for me - however by looking at this post I was able to modify it and get a working solution.

public static String loadJMXAgent(int port) throws IOException,
        AttachNotSupportedException, AgentLoadException,
        AgentInitializationException {
    String name = ManagementFactory.getRuntimeMXBean().getName();
    VirtualMachine vm = VirtualMachine.attach(name.substring(0,
            name.indexOf('@')));

    String lca = vm.getAgentProperties().getProperty(
            "com.sun.management.jmxremote.localConnectorAddress");
    if (lca == null) {
        Path p = Paths.get(System.getProperty("java.home")).normalize();
        if (!"jre".equals(p.getName(p.getNameCount() - 1).toString()
                .toLowerCase())) {
            p = p.resolve("jre");
        }
        File f = p.resolve("lib").resolve("management-agent.jar").toFile();
        if (!f.exists()) {
            throw new IOException("Management agent not found");
        }
        String options = String.format("com.sun.management.jmxremote.port=%d, " +
                "com.sun.management.jmxremote.authenticate=false, " +
                "com.sun.management.jmxremote.ssl=false", port);
        vm.loadAgent(f.getCanonicalPath(), options);
        lca = vm.getAgentProperties().getProperty(
                "com.sun.management.jmxremote.localConnectorAddress");
    }
    vm.detach();
    return lca;
}

这适用于Eclipse但是在命令行中使用它是一个不同的问题 - 这里有一些讨论为什么在Linux上使用Java Attach API会失败? (即使maven构建完成)
但我发现在我的类路径中添加$ JAVA_HOME / lib / tools.jar解决了这个问题。

This works in Eclipse however getting it to work at the command line is a different matter - there is some discussion about this here Why does using the Java Attach API fail on linux? (even though maven build completes) but I found adding $JAVA_HOME/lib/tools.jar to my classpath solved the problem.

这篇关于如何通过java代码设置JMX远程端口系统环境参数进行远程监控?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆