apache的xml-rpc例子

网上找的一个例子,用java实现的,即apache的xml-rpc:

 

计算类(server的一个服务,当然可以自己写):

package com.xmlrpc.server; public class Calculator { public int add(int i1, int i2) { return i1 + i2; } public int subtract(int i1, int i2) { return i1 - i2; } }

 

server端:

package com.xmlrpc.server; import org.apache.xmlrpc.server.PropertyHandlerMapping; import org.apache.xmlrpc.server.XmlRpcServer; import org.apache.xmlrpc.server.XmlRpcServerConfigImpl; import org.apache.xmlrpc.webserver.WebServer; public class Server { private static final int port = 9999; public static void main(String[] args) throws Exception { WebServer webServer = new WebServer(port); XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer(); PropertyHandlerMapping phm = new PropertyHandlerMapping(); phm.addHandler("Calculator", Calculator.class); xmlRpcServer.setHandlerMapping(phm); XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig(); serverConfig.setEnabledForExtensions(true); serverConfig.setContentLengthOptional(false); webServer.start(); } }

 

client端:

package com.xmlrpc.client; import java.net.URL; import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; public class Client { public static void main(String[] args) throws Exception { // create configuration XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); config.setServerURL(new URL("http://localhost:9999/xmlrpc")); config.setEnabledForExtensions(true); config.setConnectionTimeout(60 * 1000); config.setReplyTimeout(60 * 1000); XmlRpcClient client = new XmlRpcClient(); client.setConfig(config); Object[] params = new Object[]{new Integer(2), new Integer(3)}; Integer result = (Integer) client.execute("Calculator.add", params); System.out.println("result : "+result); } }

 

最后,先启动server,再运行client即可。代码具体含义,可以http://ws.apache.org/xmlrpc/下源代码,看看。

你可能感兴趣的:(apache,exception,server,object,Integer,Class)