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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值