一、Apache XML-RPC介绍
Apache XML-RPC is a Java implementation of XML-RPC, a popular protocol that uses XML over HTTP to implement remote procedure calls.(HTTP + XML)
XML-RPC的全称是XML Remote Procedure Call,即XML远程方法调用。它是一套允许运行在不同操作系统、不同环境的程序实现基于Internet过程调用的规范和一系列的实现。这种远程过程调用使用http作为传输协议,XML作为传送信息的编码格式。Xml-Rpc的定义尽可能的保持了简单,但同时能够传送、处理、返回复杂的数据结构。XML-RPC是工作在Internet上的远程过程调用协议。一个XML-RPC消息就是一个请求体为xml的http-post请求,被调用的方法在服务器端执行并将执行结果以xml格式编码后返回。
Version 3 of Apache XML-RPC is still compliant to the XML-RPC specification. However, the user may enable several vendor extensions are available, that greatly extend the power of XML-RPC:
All primitive Java types are supported, including long, byte, short, and double.
Calendar objects are supported. In particular, timezone settings, and milliseconds may be sent.
DOM nodes, or JAXB objects, can be transmitted. So are objects implementing the java.io.Serializable interface.
Both server and client can operate in a streaming mode, which preserves resources much better than the default mode, which is based on large internal byte arrays.
二、实现示例
服务端实现
步骤一、编写实现类
package server;
public class Calculator {
public int add(int i1, int i2) {
System.out.println("add i1=" + i1 + ",i2=" + i2);
return i1 + i2;
}
public int subtract(int i1, int i2) {
System.out.println("subtract i1=" + i1 + ",i2=" + i2);
return i1 - i2;
}
}
步骤二、创建配置映射文件
在包org.apache.xmlrpc.webserver下面新建一个 XmlRpcServlet.properties文件,注意:包名和文件名都不能变。在里面添加上对刚才新建的Calculator类的声明,以便外部调用。
Calculator=org.apache.xmlrpc.demo.Calculator
=左侧是Calculator是外部调用时使用的名称,例如gaojingsong_Calculator
=右侧是类的全名(包括包名),例如server.Calculator
gaojingsong_Calculator=server.Calculator
步骤三、修改web.xml 声明服务名称类
<servlet>
<servlet-name>XmlRpcServlet</servlet-name>
<servlet-class>org.apache.xmlrpc.webserver.XmlRpcServlet</servlet-class>
<init-param>
<param-name>enabledForExtensions</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>XmlRpcServlet</servlet-name>
<url-pattern>/xmlrpc_gaojingsong</url-pattern>
</servlet-mapping>
步骤四、开发服务端
package 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 {
System.out.println("------Server----begin-----------");
WebServer webServer = new WebServer(port);
XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
PropertyHandlerMapping phm = new PropertyHandlerMapping();
phm.addHandler("gaojingsong_Calculator", Calculator.class);
xmlRpcServer.setHandlerMapping(phm);
XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
serverConfig.setEnabledForExtensions(true);
serverConfig.setContentLengthOptional(false);
webServer.start();
System.out.println("------Server----end-----------");
}
}
客户端
package 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://192.168.1.100:9999/xmlrpc_gaojingsong"));
config.setEnabledForExtensions(true);
config.setConnectionTimeout(60 * 1000);
config.setReplyTimeout(60 * 1000);
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
Object[] params = new Object[]{new Integer(5), new Integer(3)};
Integer result = (Integer) client.execute("gaojingsong_Calculator.add", params);
System.out.println("result : "+result);
result = (Integer) client.execute("gaojingsong_Calculator.subtract", params);
System.out.println("result : "+result);
}
}
备注:xmlrpc_gaojingsong 对应的是web.xml 文件中的上下文路径
gaojingsong_Calculator对应的是XmlRpcServlet.properties文件中等号左边的key
三、验证
服务端
客户端