2011-10-27 66 views
3

Java Attach API可以連接到本地VM並將代理裝載到它。如何連接到另一臺計算機上的VM以加載代理?連接到遠程(非本地)VM

我知道JMX。但我沒有發現如何將我的自定義代理加載到遠程VM。

也許存在另一種方法來解決我的問題(加載和執行自定義代碼(代理)到遠程虛擬機)?

upd。我想在遠程JVM上執行自定義代碼。初始JVM參數的獨立性是正的。

謝謝。

+0

您需要用這種方法解決什麼是原始問題? –

+0

遠程運行乾淨JVM的附件,監控和代碼更新 –

+0

我強烈建議不要使用代理來更新軟件。試想一下你可以得到的支持問題。 –

回答

0

我發現實驗方案:jsadebugd
然後您可以通過連接到sun.jvm.hotspot.jdi.SAPIDAttachingConnector連接器。

+0

你是什麼意思?多少步驟? –

+0

你在程序中解決了這個問題嗎? @Andrew Knodratovich –

+0

我和你有同樣的問題。如何使用程序連接遠程jvm並注入代碼而無需重新啓動遠程jvm來添加一些調試參數? –

0

我不明白這個問題,你只是想連接到遠程虛擬機?當連接時,你可以執行任何你想要的,事件終止虛擬機。 你必須要對你的虛擬機在調試模式:

-Xdebug -Xrunjdwp:交通= dt_socket,地址= 8000,服務器= Y,暫停= N

然後在Eclipse例如,創建一個新的遠程應用程序個人資料。檢查此: http://www.ibm.com/developerworks/opensource/library/os-eclipse-javadebug/index.html

+0

我知道調試接口,但我需要這個用於生產,而不是調試。 –

2

在生產中運行應用程序服務器(Tomcat),即使使用remote debugging attached也沒有問題。

UPDATE

如果要執行你的應用程序中的自定義代碼,則一個解決辦法是寫一個類和編譯它,在一些地方保存在服務器上,然後你的應用程序中執行一些方法,這樣:

/** 
* 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; 
} 
+0

我需要注入自定義代碼。使用JMX,我可以獲得有關遠程JVM的信息,但我無法注入並執行代碼。 Attach API提供了在JVM中加載可執行代理的方法,但只提供本地JVM。我正在尋找與遠程JVM一樣的方法。 –

+0

通過遠程調試,您可以評估表達式(整行代碼)。您不能保存該代碼,但可以更改字段和變量的值。我不完全明白你想要什麼,你可以編輯你最初的問題,並詳細解釋。 – davorp

+0

如您所願更新。 –

相關問題