Apache RPC调用实例
程序员文章站
2022-06-13 18:38:59
...
一.工程结构
二.工程代码
Server.java
package com.bijian.study; 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 = 8005; public static void main(String[] args) throws Exception { WebServer webServer = new WebServer(port); XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer(); PropertyHandlerMapping phm = new PropertyHandlerMapping(); phm.addHandler("HelloHandler", HelloHandler.class); xmlRpcServer.setHandlerMapping(phm); XmlRpcServerConfigImpl config = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig(); config.setEnabledForExtensions(true); config.setContentLengthOptional(false); webServer.start(); } }
HelloHandler.java
package com.bijian.study; public class HelloHandler { public String sayHello(String name, String word) { return name + "说:" + word; } }
Client.java
package com.bijian.study; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; public class Client { public static void main(String[] args) throws Exception { XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); config.setServerURL(new URL("http://127.0.0.1:8005/xmlrpc")); XmlRpcClient client = new XmlRpcClient(); client.setConfig(config); List<String> list = new ArrayList<String>(); list.add("bijian"); list.add("rpc demo"); String result = (String) client.execute("HelloHandler.sayHello", list); System.out.println(result); } }
三.运行效果
先运服Server.java,再运行Client.java,Client输出"bijian说:rpc demo"。
上一篇: 获取json数据所有的节点路径