一个rpc java的小例子

服务端:

package w.x.xmlrpc;


import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.WebServer;

import w.x.hellohandler.HelloHandlerImpl;


public class Server3 {
    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("w.x.hellohandler.HelloHandler", HelloHandlerImpl.class);
        xmlRpcServer.setHandlerMapping(phm);
        XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) xmlRpcServer
                .getConfig();
        serverConfig.setEnabledForExtensions(true);
        serverConfig.setContentLengthOptional(false);
        webServer.start();
    }
}

客户端1:

package w.x.xmlrpc;

import java.net.MalformedURLException;
import java.util.Vector;
import java.net.URL;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

public class Client1 {
    public static void main(String[] args) {
        try {
            XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
            config.setServerURL(new URL("http://192.168.12.48:8005/xmlrpc"));
            XmlRpcClient client = new XmlRpcClient();
            client.setConfig(config);
            Vector<String> params = new Vector<String>();
            params.addElement("牛粪");
            String result = (String) client.execute("w.x.hellohandler.HelloHandler.sayHello",params);
            String result2 = (String) client.execute("w.x.hellohandler.HelloHandler.sayHi",params);
            System.out.println(result);
            System.out.println(result2);
        } catch (MalformedURLException e) {
            System.out.println(e.toString());
        } catch (XmlRpcException e) {
            System.out.println(e.toString());
        }
    }
}

客户端2:

package w.x.xmlrpc;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.xmlrpc.XmlRpcRequest;
import org.apache.xmlrpc.client.AsyncCallback;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.util.ClientFactory;

import w.x.hellohandler.HelloHandler;

class EchoCallback implements AsyncCallback {
    public void handleResult(XmlRpcRequest pRequest, Object pResult) {
        System.out.println("服务器说: " + (String) pResult);
    }

    public void handleError(XmlRpcRequest pRequest, Throwable pError) {
        System.out.println("发生错误: " + pError.getMessage());
    }
}

public class Client3 {
    public static void main(String[] args) throws Exception {
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL("http://192.168.12.48:8005/xmlrpc"));
        config.setEnabledForExtensions(true);
        config.setConnectionTimeout(60 * 1000);
        config.setReplyTimeout(60 * 1000);
        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(config);
        ClientFactory factory = new ClientFactory(client);
        HelloHandler handler = (HelloHandler) factory.newInstance(w.x.hellohandler.HelloHandler.class);
        String str = handler.sayHello("美女");
        System.out.println(str);
        List<String> params = new ArrayList<String>();
        params.add("帅哥");
        String result=(String)client.execute("w.x.hellohandler.HelloHandler.sayHello", params);
        System.out.println(result);
        client.executeAsync("w.x.hellohandler.HelloHandler.sayHello", params,new EchoCallback());
        client.executeAsync("w.x.hellohandler.HelloHandler.sayHi", params,new EchoCallback());
    }
}

你可能感兴趣的:(一个rpc,java的小例子)