连接远程虚拟机(非本地)

3
Java Attach API可以附加到本地VM并向其加载代理程序。我如何附加到另一台计算机上的VM以加载代理程序?
我知道JMX。但我没有找到如何将自定义代理程序加载到远程VM。
也许存在其他解决我的问题的方法(加载和执行自定义代码(代理程序)到远程VM)?
更新。我想在远程JVM上执行自定义代码。独立于初始JVM参数是一个优点。
谢谢。

你需要用这种方式解决的最初问题是什么? - Thorbjørn Ravn Andersen
远程运行干净的JVM时,附件、监控和代码更新。 - Andrei Kandratovich
我强烈反对使用代理来更新软件。想象一下,这可能会带来多少支持问题。 - Thorbjørn Ravn Andersen
3个回答

2
在生产环境中运行应用程序服务器(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的信息,但是我不能注入和执行代码。Attach API提供了在JVM中加载可执行代理程序的方法,但仅限于本地JVM。我正在寻找一种在远程JVM上实现相同功能的方法。 - Andrei Kandratovich
通过远程调试,您可以评估表达式(整行代码)。您无法保存该代码,但可以更改字段和变量的值。我不完全理解您想要什么,请编辑您的初始问题并详细说明。 - davorp

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

您在程序中解决了这个问题吗?@Andrew Knodratovich - Nick Dong
我和你有同样的问题。如何在不重启远程jvm的情况下,通过程序连接远程jvm并注入代码以添加一些调试参数? - Nick Dong

0

我不明白问题,你只是想连接到远程虚拟机吗?连接后,您可以执行任何操作,甚至终止虚拟机。您必须以调试模式启动虚拟机:

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

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


我知道调试接口,但我需要它用于生产而不是调试。 - Andrei Kandratovich

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接