【问题标题】:Attach to remote (not local) VM附加到远程(非本地)VM
【发布时间】:2011-10-27 21:09:53
【问题描述】:

Java Attach API 可以附加到本地 VM 并将代理加载到它。如何附加到另一台计算机上的 VM 以加载代理?

我知道 JMX。但我不知道如何将我的自定义代理加载到远程 VM。

也许还有其他方法可以解决我的问题(将自定义代码(代理)加载并执行到远程 VM)?

更新。我想在远程 JVM 上执行自定义代码。初始JVM参数的独立性是加号的。

谢谢。

【问题讨论】:

  • 这种方式需要解决的原始问题是什么?
  • 远程运行干净 JVM 的附加、监控和代码更新
  • 我强烈反对使用代理来更新软件。想象一下您在这方面可能遇到的支持问题。

标签: java jvm jmx


【解决方案1】:

即使使用remote debugging attached,在生产环境中运行应用服务器 (Tomcat) 也没有问题。

更新

如果您想在您的应用程序中执行自定义代码,那么一种解决方案是编写一个类并对其进行编译,将其存储在服务器上的某个位置,然后在您的应用程序中执行一些类似这样的方法:

/**
 * This method:
 * <li>loads a class from the server file system
 * <li>does a lookup for the method to execute
 * <li>creates a new instance of the specified class
 * <li>executes the given method with the given arguments
 *     (which can be null if the method doesn't have arguments)
 * <li>returns the result of the invoked method
 * 
 * @param classUrlOnTheServer
 * @param className
 * @param methodNameToExecute
 * @param argumentsForTheMethod arguments that should be passed to
 *                              the method of the loaded class - can
 *                              be null.
 * @return returns the result of the invoked method
 * @throws ClassNotFoundException
 * @throws MalformedURLException
 * @throws SecurityException
 * @throws NoSuchMethodException
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 * @throws InvocationTargetException
 */
public static Object loadAndExecuteCustomMethodFromALoadedClass(String classUrlOnTheServer,
                                                        String className,
                                                        String methodNameToExecute,
                                                        Object ... argumentsForTheMethod)
                                                                                        throws ClassNotFoundException,
                                                                                        MalformedURLException,
                                                                                        SecurityException,
                                                                                        NoSuchMethodException,
                                                                                        InstantiationException,
                                                                                        IllegalAccessException,
                                                                                        IllegalArgumentException,
                                                                                        InvocationTargetException {
   File file = new File(classUrlOnTheServer);
   URL url = file.toURI().toURL();  
   URL[] urls = new URL[] { url };
   ClassLoader cl = new URLClassLoader(urls);
   Class clazz = cl.loadClass(className);

   Method method = clazz.getDeclaredMethod(methodNameToExecute);
   Object instance = clazz.newInstance();
   Object result = method.invoke(instance, argumentsForTheMethod);
   return result;
}

【讨论】:

  • 我需要注入自定义代码。使用 JMX,我可以获得有关远程 JVM 的信息,但我无法注入和执行代码。附加 API 提供了在 JVM 中加载可执行代理的方法,但仅限于本地 JVM。我正在寻找对远程 JVM 执行相同操作的方法。
  • 通过远程调试,您可以评估表达式(整行代码)。您无法保存该代码,但可以更改字段和变量的值。我不完全明白你想要什么,你能否编辑你最初的问题并详细解释。
【解决方案2】:

我不明白这个问题,你只是想连接到远程虚拟机吗?连接后,您可以执行任何您想要的,事件终止虚拟机。 你必须在调试模式下启动你的虚拟机:

-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

然后以 Eclipse 为例,创建一个新的远程应用程序配置文件。检查这个: http://www.ibm.com/developerworks/opensource/library/os-eclipse-javadebug/index.html

【讨论】:

  • 我知道调试接口,但我需要它用于生产,而不是用于调试。
【解决方案3】:

我找到了实验性解决方案:jsadebugd
然后您可以通过 sun.jvm.hotspot.jdi.SAPIDAttachingConnector 连接器附加到它。

【讨论】:

  • 你在程序中解决了这个问题吗? @Andrew Knodratovich
  • 我和你有同样的问题。如何附加远程jvm并使用程序注入代码而不重新启动远程jvm以添加一些调试参数?
猜你喜欢
  • 2014-03-01
  • 2011-09-04
  • 1970-01-01
  • 2018-04-25
  • 1970-01-01
  • 2022-10-24
  • 2021-09-10
  • 2018-09-23
  • 2020-01-26
相关资源
最近更新 更多