Class RpcServer

  • Direct Known Subclasses:
    MapRpcServer, StringRpcServer

    public class RpcServer
    extends Object
    Class which manages a request queue for a simple RPC-style service. The class is agnostic about the format of RPC arguments / return values.
    • Constructor Detail

      • RpcServer

        public RpcServer​(Channel channel)
                  throws IOException
        Creates an RpcServer listening on a temporary exclusive autodelete queue.
        Throws:
        IOException
      • RpcServer

        public RpcServer​(Channel channel,
                         String queueName)
                  throws IOException
        If the passed-in queue name is null, creates a server-named temporary exclusive autodelete queue to use; otherwise expects the queue to have already been declared.
        Throws:
        IOException
    • Method Detail

      • close

        public void close()
                   throws IOException
        Public API - cancels the consumer, thus deleting the queue, if it was a temporary queue, and marks the RpcServer as closed.
        Throws:
        IOException - if an error is encountered
      • setupConsumer

        protected QueueingConsumer setupConsumer()
                                          throws IOException
        Registers a consumer on the reply queue.
        Returns:
        the newly created and registered consumer
        Throws:
        IOException - if an error is encountered
      • mainloop

        public ShutdownSignalException mainloop()
                                         throws IOException
        Public API - main server loop. Call this to begin processing requests. Request processing will continue until the Channel (or its underlying Connection) is shut down, or until terminateMainloop() is called, or until the thread running the loop is interrupted. Note that if the mainloop is blocked waiting for a request, the termination flag is not checked until a request is received, so a good time to call terminateMainloop() is during a request handler.
        Returns:
        the exception that signalled the Channel shutdown, or null for orderly shutdown
        Throws:
        IOException
      • terminateMainloop

        public void terminateMainloop()
        Call this method to terminate the mainloop. Note that if the mainloop is blocked waiting for a request, the termination flag is not checked until a request is received, so a good time to call terminateMainloop() is during a request handler.
      • handleCall

        public byte[] handleCall​(AMQP.BasicProperties requestProperties,
                                 byte[] requestBody,
                                 AMQP.BasicProperties replyProperties)
        Mid-level response method. Calls handleCall(byte[],AMQP.BasicProperties).
      • handleCall

        public byte[] handleCall​(byte[] requestBody,
                                 AMQP.BasicProperties replyProperties)
        High-level response method. Returns an empty response by default - override this (or other handleCall and handleCast methods) in subclasses.
      • preprocessReplyProperties

        protected AMQP.BasicProperties preprocessReplyProperties​(QueueingConsumer.Delivery request,
                                                                 AMQP.BasicProperties.Builder builder)
        Gives a chance to set/modify reply properties before handling call. Note the correlationId property is already set.
        Parameters:
        request - the inbound message
        builder - the reply properties builder
        Returns:
        the properties to pass in to the handling call
      • postprocessReplyProperties

        protected AMQP.BasicProperties postprocessReplyProperties​(QueueingConsumer.Delivery request,
                                                                  AMQP.BasicProperties.Builder builder)
        Gives a chance to set/modify reply properties after the handling call
        Parameters:
        request - the inbound message
        builder - the reply properties builder
        Returns:
        the properties to pass in to the response message
      • handleCast

        public void handleCast​(QueueingConsumer.Delivery request)
        Lowest-level handler method. Calls handleCast(AMQP.BasicProperties,byte[]).
      • handleCast

        public void handleCast​(AMQP.BasicProperties requestProperties,
                               byte[] requestBody)
        Mid-level handler method. Calls handleCast(byte[]).
      • handleCast

        public void handleCast​(byte[] requestBody)
        High-level handler method. Does nothing by default - override this (or other handleCast and handleCast methods) in subclasses.
      • getChannel

        public Channel getChannel()
        Retrieve the channel.
        Returns:
        the channel to which this server is connected
      • getQueueName

        public String getQueueName()
        Retrieve the queue name.
        Returns:
        the queue which this server is consuming from