Java Attach API 可以附加到本地 VM 和负载代理。如何连接到另一台计算机上的 VM 以加载代理?
我知道 JMX。但是我没有找到如何将我的自定义代理加载到远程 VM。
也许有其他方法可以解决我的问题(将自定义代码(代理)加载并执行到远程 VM)?
更新。我想在远程 JVM 上执行自定义代码。初始 JVM 参数的独立性是加号。
谢谢。
最佳答案
即使使用 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;
}
关于java - 附加到远程(非本地)VM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7922454/