如何制作RPC-JSON java服务器

时间:2016-04-10 03:06:30

标签: java json rpc json-rpc

我在Android中有一个客户端RPC-JSON,我正在尝试使用Java库(http://software.dzhuvinov.com/json-rpc-2.0-server.html)创建一个RPC-JSON服务器。这是官方的例子:

// The JSON-RPC 2.0 Base classes that define the 
// JSON-RPC 2.0 protocol messages
import com.thetransactioncompany.jsonrpc2.*;

// The JSON-RPC 2.0 server framework package
import com.thetransactioncompany.jsonrpc2.server.*;

import java.text.*;
import java.util.*;


/**
 * Demonstration of the JSON-RPC 2.0 Server framework usage. The request
 * handlers are implemented as static nested classes for convenience, but in 
 * real life applications may be defined as regular classes within their old 
 * source files.
 *
 * @author Vladimir Dzhuvinov
 * @version 2011-03-05
 */ 
public class Example {


    // Implements a handler for an "echo" JSON-RPC method
    public static class EchoHandler implements RequestHandler {


        // Reports the method names of the handled requests
        public String[] handledRequests() {

            return new String[]{"echo"};
        }


         // Processes the requests
         public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {

             if (req.getMethod().equals("echo")) {

                 // Echo first parameter

                 List params = (List)req.getParams();

             Object input = params.get(0);

             return new JSONRPC2Response(input, req.getID());
            }
            else {

                // Method name not supported

                return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
        }
        }
    }


    // Implements a handler for "getDate" and "getTime" JSON-RPC methods
    // that return the current date and time
    public static class DateTimeHandler implements RequestHandler {


        // Reports the method names of the handled requests
    public String[] handledRequests() {

        return new String[]{"getDate", "getTime"};
    }


    // Processes the requests
    public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {

        if (req.getMethod().equals("getDate")) {

            DateFormat df = DateFormat.getDateInstance();

        String date = df.format(new Date());

        return new JSONRPC2Response(date, req.getID());

            }
            else if (req.getMethod().equals("getTime")) {

            DateFormat df = DateFormat.getTimeInstance();

        String time = df.format(new Date());

        return new JSONRPC2Response(time, req.getID());
            }
        else {

            // Method name not supported

        return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
            }
        }
    }


    public static void main(String[] args) {

        // Create a new JSON-RPC 2.0 request dispatcher
    Dispatcher dispatcher =  new Dispatcher();


    // Register the "echo", "getDate" and "getTime" handlers with it
    dispatcher.register(new EchoHandler());
    dispatcher.register(new DateTimeHandler());


    // Simulate an "echo" JSON-RPC 2.0 request
    List echoParam = new LinkedList();
    echoParam.add("Hello world!");

    JSONRPC2Request req = new JSONRPC2Request("echo", echoParam, "req-id-01");
    System.out.println("Request: \n" + req);

    JSONRPC2Response resp = dispatcher.process(req, null);
    System.out.println("Response: \n" + resp);

    // Simulate a "getDate" JSON-RPC 2.0 request
    req = new JSONRPC2Request("getDate", "req-id-02");
    System.out.println("Request: \n" + req);

    resp = dispatcher.process(req, null);
    System.out.println("Response: \n" + resp);

    // Simulate a "getTime" JSON-RPC 2.0 request
    req = new JSONRPC2Request("getTime", "req-id-03");
    System.out.println("Request: \n" + req);

    resp = dispatcher.process(req, null);
    System.out.println("Response: \n" + resp);
    }
}

我在手册和谷歌中搜索,但我不明白我是怎么做的,服务器正在等待请求并发送回复。我怎么能这样做?

0 个答案:

没有答案