XML-RPC(遠程調用)

[size=large]The XmlRpcClient[/size]
xml-rpc java的下載路徑
[url]http://apache.freelamp.com/ws/xmlrpc/[/url]


客戶端的配置要設置下面幾個對象。

[img]http://dl.iteye.com/upload/attachment/184249/9983fbf7-a9af-3759-a882-3e5913351154.jpg[/img]

例子如下:
利用默認的TransportFactory的客戶端代碼片段:


public static void main(String[] args) throws Exception {
// create configuration
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://127.0.0.1:8080/XmlRpc"));
config.setEnabledForExtensions(true);
config.setConnectionTimeout(60 * 1000);
config.setReplyTimeout(60 * 1000);
XmlRpcClient client = new XmlRpcClient();
// use Commons HttpClient as transport
client.setTransportFactory( new XmlRpcSunHttpTransportFactory(client));//default TransportFactory
// client.setTransportFactory( new XmlRpcLocalTransportFactory(client));
// client.setTransportFactory( new XmlRpcLiteHttpTransportFactory(client));
// client.setTransportFactory( new XmlRpcCommonsTransportFactory(client));

// set configuration
client.setConfig(config);
// make the a regular call
Object[] params = new Object[]{ new Integer(2), new Integer(3) };
Integer result = (Integer) client.execute("Calculator.add", params);
System.out.println("2 + 3 = " + result);

// make a call using dynamic proxy
// ClientFactory factory = new ClientFactory(client);
// Calculator calculator = (Calculator) factory.newInstance(Calculator.class);
// int sum = calculator.add(2, 4);
// System.out.println("2 + 4 = " + sum);
}

我們調用遠程方法Calculator.add,輸入參數2,3.得到預期結果。
注意 :運行XmlRpcCommonsTransportFactory的時候出現異常 ,用到基於Jakarta HTTP Client的Factory時,因爲這個開源包沒有包含 Jakarta HTTP Client的jar包,因此需要到官網上下載這個jar包。
[size=large]The Transport Factory[/size]

[img]http://dl.iteye.com/upload/attachment/184251/e19fee4d-99cd-3ef8-95db-ac84d04c860f.jpg[/img]


[size=large]Server-side XML-RPC[/size]

[size=large]The Server configuration[/size]


[img]http://dl.iteye.com/upload/attachment/184262/4ffa2cd1-37c4-31cd-b59a-94b973cbf1c0.jpg[/img]


private static final int port = 8080;

public static void main(String[] args) throws Exception {
WebServer webServer = new WebServer(port);
XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
PropertyHandlerMapping phm = new PropertyHandlerMapping();
/* Load handler definitions from a property file.
* The property file might look like:
* Calculator=org.apache.xmlrpc.demo.Calculator
* org.apache.xmlrpc.demo.proxy.Adder=org.apache.xmlrpc.demo.proxy.AdderImpl
*/
// phm.load(Thread.currentThread().getContextClassLoader(),"\\org\\apache\\xmlrpc\\webserver\\MyHandler.properties");

phm.addHandler("Calculator", org.apache.xmlrpc.demo.Calculator.class);
// phm.addHandler(org.apache.xmlrpc.demo.proxy.Adder.class.getName(),
// org.apache.xmlrpc.demo.proxy.AdderImpl.class);
xmlRpcServer.setHandlerMapping(phm);
XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
serverConfig.setEnabledForExtensions(true);
serverConfig.setContentLengthOptional(false);
webServer.start();
System.out.println("Registered Calculator to \"Calculator\"");
System.out.println("Now Accepting Requests ...");
}




從上面的server端代碼可以看出:
可以有2中方式實現server端代碼

phm.addHandler("Calculator", org.apache.xmlrpc.demo.Calculator.class);






phm.load(Thread.currentThread().getContextClassLoader(),"\\org\\apache\\xmlrpc\\webserver\\MyHandler.properties");



下面這中情況需要建一個MyHandler.properties文件。
內容:

Calculator=org.apache.xmlrpc.demo.Calculator


對於server端,還可以這樣寫


private static final int port = 8080;

public static void main(String[] args) throws Exception {
XmlRpcServlet servlet = new XmlRpcServlet();
ServletWebServer webServer = new ServletWebServer(servlet, port);
webServer.start();
System.out.println("Now XmlRpcServlet Accepting Requests ...");
}


用這種方式的話,必須有個XmlRpcServlet.properties這個文件
properties文件必須在org.apache.xmlrpc.webserver這個目錄下

Calculator=org.apache.xmlrpc.demo.Calculator
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章