پروتکل xml-rpc در حالت کلی یک وب سرویس است. در اینجا صرف نظر از توضیحات تئوریک این پروتکل صرفا به شرح روش پیاده سازی خواهم پرداخت. شرح کلی اینکه: باید کلاسی در یک وب سرور با یک  پورت مشخص، رجیستر شود و در کلاس کلاینت با اتصال به آن وب سرور متدهای مورد نظر فراخوانی شود.

قبل از همه باید اشاره کنم که نیاز من این بود که این سرویس با استارت شدن وبلاجیک بلافاصله استارت شود و نیازی به اجرای دستور یا ارسال درخواستی نباشد. به همین دلیل از سرولت استفاده کردم و در فایل web.xml تگ زیر را قرار دادم:

<load-on-startup>1</load-on-startup>

import java.io.IOException;
import java.io.PrintWriter;

import java.net.BindException;
import java.net.Socket;

import javax.servlet.*;
import javax.servlet.http.*;

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

public class MyServlet extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=UTF-8";

    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        System.out.println("Started successfully automatic.");
        int port = 4555;
        WebServer webserver = new WebServer(port);
        XmlRpcServer xmlRpcServer = webserver.getXmlRpcServer();
        // Map Your Classes to the server for example:
        PropertyHandlerMapping phm = new PropertyHandlerMapping();
        try {
            phm.addHandler("area", AreaHandler.class);
            xmlRpcServer.setHandlerMapping(phm);
            // Start Server
            XmlRpcServerConfigImpl serverConfig =
                (XmlRpcServerConfigImpl)xmlRpcServer.getConfig();
            serverConfig.setEnabledForExtensions(true);
            serverConfig.setContentLengthOptional(false);
            webserver.start();
        } catch (XmlRpcException e) {
            System.out.println("XmlRpcException in MyServlet: " +
                               e.getMessage());
        } catch (BindException e) {
            webserver.run();
        } catch (IOException e) {
            System.out.println("IOException in MyServlet: " + e.getMessage());
        }
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException,
                                                           IOException {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>RecFunServlet</title></head>");
        out.println("<body>");
        out.println("<p>The servlet has received a GET. This is the reply.</p>");
        out.println("</body></html>");
        out.close();
    }
}

import java.util.HashMap;

public class AreaHandler {
    public AreaHandler() {
        super();
    }

    public HashMap<String, String> rectArea(HashMap<String, String> params) {
        Double length = new Double(params.get("length"));
        Double width = new Double(params.get("width"));
        HashMap<String, String> result = new HashMap<String, String>();
        result.put("rectArea", new Double(length * width).toString());
        return result;
    }

    public HashMap<String, String> circleArea(HashMap<String, String> params) {        
        Double radius = new Double(params.get("radius"));
        Double value = (radius * radius * Math.PI);
        HashMap<String, String> result = new HashMap<String, String>();
        result.put("circleArea", value.toString());
        return result;
    }
}
public class AreaClient {
    public static void main(String[] args) {
        
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        try {
            config.setServerURL(new URL("http://localhost:4555/"));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(config);
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("length", "20");
        params.put("width", "30");
        Object result;
        try {
            result = client.execute("area.rectArea", new Object[] { params });
            System.out.println("The area of the rect would be: " +
                               result.toString());
        } catch (XmlRpcException e) {
            e.printStackTrace();
        }
    }
}

ضمنا در این برنامه نیازمند استفاده از کتابخانه xml-rpc نیز هستید:

https://archive.apache.org/dist/ws/xmlrpc/

با تشکر

موفق باشید

میلاد