Java RPC通信机制之XML-RPC
二、举例
下面举一个实际运用XML-RPC进行RPC调用的例子,XML-RPC规范有多种针对不同语言的实现,这里我们使用的是Apache的XML-RPC3.0RC1。
在开始之前,需到/commons/index.html下载如下程序包:
commons-codec-1.3(通用编码/解码算法实现,可参考www.devx/Java/Article/29795/1954?pf=true或/commons/codec/userguide.html来获得该软件包的详细信息)
commons-httpclient-3.0.1(HTTP协议的客户端编程工具包,详细介绍见www-128.ibm/developerworks/cn/opensource/os-httpclient/)
将上述通用工具包解压后,拷贝其中的jar文件到XML-RPC解压目录的dist目录中。
并添加如下环境变量:
XMLRPC_HOME XML-RPC的解压目录
XMLRPC_LIB %XMLRPC_HOME%/dist
XMLRPCCLASSPATH %XMLRPC_LIB%/xmlrpc-common-3.0rc1.jar;%XMLRPC_LIB%/xmlrpc-server-3.0rc1.jar;%XMLRPC_LIB%/xmlrpc-client-3.0rc1.jar;%XMLRPC_LIB%/commons-httpclient-3.0.1.jar;%XMLRPC_LIB%/commons-codec-1.3.jar
整个应用很简单,通过XML-RPC调用Server端提供的HelloHandler.sayHello方法回显一个字符串信息。下面是HelloHandler接口及其实现类相关代码:
// HelloHandler.java
lrpc;
public interface HelloHandler {
public String sayHello(String str);
}
// HelloHandlerImpl.java
lrpc;
public class HelloHandlerImpl implements HelloHandler {
public String sayHello(String str){
return "Hello, " + str + "!";
}
webserver接口开发}
以下是对应的Server端源代码:
// Server1.java
lrpc;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lrpc.XmlRpcException;
import lrpc.server.PropertyHandlerMapping;
import lrpc.server.XmlRpcServerConfigImpl;
import lrpc.webserver.XmlRpcServletServer;
public class Server1 extends HttpServlet {
private XmlRpcServletServer server;
public void init(ServletConfig pConfig) throws ServletException {
super.init(pConfig);
try {
// create a new XmlRpcServletServer object
server = new XmlRpcServletServer();
// set up handler mapping of XmlRpcServletServer object
PropertyHandlerMapping phm = new PropertyHandlerMapping();
phm.addHandler("HelloHandler", HelloHandlerImpl.class);
server.setHandlerMapping(phm);
// more config of XmlRpcServletServer object
XmlRpcServerConfigImpl serverConfig = (Config();
serverConfig.setEnabledForExtensions(true);
serverConfig.setContentLengthOptional(false);
} catch (XmlRpcException e) {
try {
log("Failed to create XmlRpcServer: " + e.getMessage(), e);
} catch (Throwable ignore) {
}
throw new ServletException(e);
}
}
public void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse)
throws IOException, ServletException {
ute(pRequest, pResponse);
}
}
以下是对应的Client端源代码:
// Client1.java
lrpc;
import java.io.IOException;
import java.MalformedURLException;
import java.util.Vector;
import java.URL;
import lrpc.XmlRpcException;
import lrpc.client.XmlRpcClient;
import lrpc.client.XmlRpcClientConfigImpl;
public class Client1 {
public static void main(String[] args) {
try {
// config client
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("localhost:8080/jsp/XmlRpcServer")); // should be modified according to your configuration of jsp container
// create a new XmlRpcClient object and bind above config object with it
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
// create parameter list
Vector<String> params = new Vector<String>();
params.addElement("Tom");
// execute XML-RPC call
String result = (String) ute("HelloHandler.sayHello", params);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论