Class Dispatcher

java.lang.Object
com.thetransactioncompany.jsonrpc2.server.Dispatcher
All Implemented Interfaces:
NotificationHandler, RequestHandler

public class Dispatcher
extends Object
implements RequestHandler, NotificationHandler
Dispatcher for JSON-RPC 2.0 requests and notifications. This class is tread-safe.

Use the register() methods to add a request or notification handler for an RPC method.

Use the process() methods to have an incoming request or notification processed by the matching handler.

The reportProcTime() method enables reporting of request processing time (in microseconds) by appending a non-standard "xProcTime" attribute to the resulting JSON-RPC 2.0 response message.

Example:

 { 
   "result"    : "xyz",
   "id"        : 1,
   "jsonrpc"   : "2.0",
   "xProcTime" : "189 us"
 }
 

Note: The dispatch(...) methods were deprecated in version 1.7. Use process(...) instead.

Author:
Vladimir Dzhuvinov
  • Constructor Details

    • Dispatcher

      public Dispatcher()
      Creates a new dispatcher with no registered handlers.
  • Method Details

    • register

      public void register​(RequestHandler handler)
      Registers a new JSON-RPC 2.0 request handler.
      Parameters:
      handler - The request handler to register. Must not be null.
      Throws:
      IllegalArgumentException - On attempting to register a handler that duplicates an existing request name.
    • register

      public void register​(NotificationHandler handler)
      Registers a new JSON-RPC 2.0 notification handler.
      Parameters:
      handler - The notification handler to register. Must not be null.
      Throws:
      IllegalArgumentException - On attempting to register a handler that duplicates an existing notification name.
    • handledRequests

      public String[] handledRequests()
      Description copied from interface: RequestHandler
      Gets the names of the handled JSON-RPC 2.0 request methods.
      Specified by:
      handledRequests in interface RequestHandler
      Returns:
      The names of the handled JSON-RPC 2.0 request methods.
    • handledNotifications

      public String[] handledNotifications()
      Description copied from interface: NotificationHandler
      Gets the names of the handled JSON-RPC 2.0 notification methods.
      Specified by:
      handledNotifications in interface NotificationHandler
      Returns:
      The names of the handled JSON-RPC 2.0 notification methods.
    • getRequestHandler

      public RequestHandler getRequestHandler​(String requestName)
      Gets the handler for the specified JSON-RPC 2.0 request name.
      Parameters:
      requestName - The request name to lookup.
      Returns:
      The corresponding request handler or null if none was found.
    • getNotificationHandler

      public NotificationHandler getNotificationHandler​(String notificationName)
      Gets the handler for the specified JSON-RPC 2.0 notification name.
      Parameters:
      notificationName - The notification name to lookup.
      Returns:
      The corresponding notification handler or null if none was found.
    • dispatch

      public JSONRPC2Response dispatch​(JSONRPC2Request request, MessageContext requestCtx)
      Deprecated.
    • process

      public JSONRPC2Response process​(JSONRPC2Request request, MessageContext requestCtx)
      Description copied from interface: RequestHandler
      Processes a JSON-RPC 2.0 request.
      Specified by:
      process in interface RequestHandler
      Parameters:
      request - A valid JSON-RPC 2.0 request instance. Must not be null.
      requestCtx - Context information about the request message, may be null if undefined.
      Returns:
      The resulting JSON-RPC 2.0 response. It indicates success or an error, such as METHOD_NOT_FOUND.
    • dispatch

      public void dispatch​(JSONRPC2Notification notification, MessageContext notificationCtx)
      Deprecated.
    • process

      public void process​(JSONRPC2Notification notification, MessageContext notificationCtx)
      Description copied from interface: NotificationHandler
      Processes a JSON-RPC 2.0 notification.

      Note that JSON-RPC 2.0 notifications don't produce a response!

      Specified by:
      process in interface NotificationHandler
      Parameters:
      notification - A valid JSON-RPC 2.0 notification instance. Must not be null.
      notificationCtx - Context information about the notification message, may be null if undefined.
    • reportProcTime

      public void reportProcTime​(boolean enable)
      Controls reporting of request processing time by appending a non-standard "xProcTime" attribute to the JSON-RPC 2.0 response. Reporting is disabled by default.
      Parameters:
      enable - true to enable proccessing time reporting, false to disable it.
    • reportsProcTime

      public boolean reportsProcTime()
      Returns true if reporting of request processing time is enabled. See the reportProcTime description for more information.
      Returns:
      true if reporting of request processing time is enabled, else false.