XML-RPC 是一套規範與實作,讓運行在不同作業系統、不同環境中的程式能夠進行基於網際網路的遠程程序呼叫。它以 HTTP 作為傳輸協議,以 XML 作為訊息編碼格式。
XML-RPC 的設計刻意保持簡單,卻仍足以傳送、處理並回傳複雜的資料結構。一個 XML-RPC 訊息在本質上就是一個請求主體為 XML 的 HTTP POST 請求:被呼叫的方法在伺服器端執行,執行結果再以 XML 編碼後回傳。
POST /RPC2 HTTP/1.0
User-Agent: Frontier5.1.2 (WinNT)
Host: betty.userland.com
Content-Type: text/xml
Content-length: 181
<?xml version="1.0"?>
<methodCall>
<methodName>examples.getStateName</methodName>
<params>
<param>
<value><i4>41</i4></value>
</param>
</params>
</methodCall>
HTTP/1.1 200 OK
Connection: close
Content-Length: 158
Content-Type: text/xml
Date: Fri, 17 Jul 1998 19:55:08 GMT
Server: UserLand Frontier5.1.2-WinNT
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value><string>South Dakota</string></value>
</param>
</params>
</methodResponse>
整個範例由三個部分組成:管理器(HelloHandler)、伺服器(HelloServer)與用戶端(HelloClient)。
第一步是建立用於遠程呼叫的類別與方法,人們通常稱之為「管理器」。XML-RPC 管理器是一組方法的集合,它接收 XML-RPC 請求、解碼請求內容,再把呼叫轉發給對應的類別與方法。
package XMLRpc;
/**
* @author trier
*
* <b><code>HelloHandler</code></b> is a simple handler that can
* be registered with an XML-RPC server
*/
public class HelloHandler {
public String sayHello(String name) {
return "Hello " + name;
}
}
伺服器程式負責把管理器註冊到伺服器上,並設定其他必要參數。
package XMLRpc;
import org.apache.xmlrpc.WebServer;
import org.apache.xmlrpc.XMLRpc;
import java.io.IOException;
/**
* <b><code>HelloServer</code></b> is a simple XML-RPC server
* that will take the <code>HelloHandler</code> class available
* for XML-RPC calls.
*/
public class HelloServer {
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: java HelloServer [port]");
System.exit(-1);
}
try {
XMLRpc.setDriver("org.apache.xerces.parsers.SAXParser");
// 啟動伺服器
System.out.println("Starting XML-RPC Server......");
WebServer server = new WebServer(Integer.parseInt(args[0]));
// 註冊我們的管理器類別
server.addHandler("hello", new HelloHandler());
System.out.println("Now accepting requests......");
} catch (ClassNotFoundException e) {
System.out.println("Could not locate SAX Driver");
} catch (IOException e) {
System.out.println("Could not start server: " + e.getMessage());
}
}
}
package XMLRpc;
import java.io.IOException;
import java.util.Vector;
import java.net.MalformedURLException;
import org.apache.xmlrpc.XMLRpc;
import org.apache.xmlrpc.XMLRpcClient;
import org.apache.xmlrpc.XMLRpcException;
/**
* <b><code>HelloClient</code></b> is a simple XML-RPC client
* that makes an XML-RPC request to <code>HelloServer</code>
*/
public class HelloClient {
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: java HelloClient [your name]");
System.exit(-1);
}
try {
// 使用 Apache Xerces 的 SAX Driver
XMLRpc.setDriver("org.apache.xerces.parsers.SAXParser");
// 指定伺服器位址
XMLRpcClient client = new XMLRpcClient("https://localhost:8585");
// 建立請求參數
Vector params = new Vector();
params.addElement(args[0]);
// 送出請求並印出結果
String result = (String) client.execute("hello.sayHello", params);
System.out.println("Response from server: " + result);
} catch (ClassNotFoundException e) {
System.out.println("Could not locate SAX Driver");
} catch (MalformedURLException e) {
System.out.println("Incorrect URL format for XML-RPC server: "
+ e.getMessage());
} catch (XMLRpcException e) {
System.out.println("XMLRpcException: " + e.getMessage());
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
}
}
}
兩者最關鍵的差異在於「方法如何被定位」。
在 RMI 中,遠端介面為每個遠端方法都定義了方法簽章。如果某個方法在伺服器端存在,卻沒有對應的簽章被加到遠端介面上,那麼用戶端就無法呼叫這個新方法。
在 RPC 中,當請求抵達伺服器時,請求本身即包含一組參數與一個文字值,通常形如 classname.methodname。這等於告訴伺服器:要找的方法在名為 classname 的類別中,名稱為 methodname。伺服器接著搜尋相符的類別與方法,並以請求中攜帶的型別資訊作為輸入參數進行匹配;一旦匹配成功,方法就被執行,結果編碼後回傳用戶端。
這個差異也解釋了 XML-RPC 的彈性來源:因為方法名稱是以字串形式在執行期傳遞,伺服器端不必事先靜態宣告所有可呼叫的方法簽章,這讓跨語言、跨平台的整合變得容易,代價則是少了編譯期的型別檢查。