Class RabbitRpcClient

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class RabbitRpcClient
    extends Object
    implements Closeable
    This class implements a thread safe client that can process several RPC calls in parallel.
    Author:
    Michael Röder (roeder@informatik.uni-leipzig.de)
    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
      • DEFAULT_MAX_WAITING_TIME

        private static final long DEFAULT_MAX_WAITING_TIME
        The default maximum amount of time in millisecond the client is waiting for a response = 600000Lms.
        See Also:
        Constant Field Values
      • requestQueue

        private RabbitQueue requestQueue
        Queue used for the request.
      • responseQueue

        private RabbitQueue responseQueue
        Queue used for the responses.
      • maxWaitingTime

        private long maxWaitingTime
        The maximum amount of time in millisecond the client is waiting for a response. The default value is defined by DEFAULT_MAX_WAITING_TIME.
    • Constructor Detail

      • RabbitRpcClient

        protected RabbitRpcClient()
        Constructor.
    • Method Detail

      • create

        public static RabbitRpcClient create​(com.rabbitmq.client.Connection connection,
                                             String requestQueueName)
                                      throws IOException
        Creates a StorageServiceClient using the given RabbitMQ Connection.
        Parameters:
        connection - RabbitMQ connection used for the communication
        requestQueueName - name of the queue to which the requests should be sent
        Returns:
        a StorageServiceClient instance
        Throws:
        IOException - if a problem occurs during the creation of the queues or the consumer.
      • init

        protected void init​(com.rabbitmq.client.Connection connection,
                            String requestQueueName)
                     throws IOException
        Initializes the client by declaring a request queue using the given connection and queue name as well as a second queue and a consumer for retrieving responses.
        Parameters:
        connection - the RabbitMQ connection that is used for creating queues
        requestQueueName - the name of the queue
        Throws:
        IOException - if a communication problem during the creation of the channel, the queue or the internal consumer occurs
      • request

        public byte[] request​(byte[] data)
        Sends the request, i.e., the given data, and blocks until the response is received.
        Parameters:
        data - the data of the request
        Returns:
        the response or null if an error occurs.
      • processResponseForRequest

        protected void processResponseForRequest​(String corrId,
                                                 byte[] body)
        Processes the response with the given correlation Id and byte array by searching for a matching request and setting the response if it could be found. If there is no request with the same correlation Id, nothing is done.
        Parameters:
        corrId - correlation Id of the response
        body - data of the response
      • getMaxWaitingTime

        public long getMaxWaitingTime()
      • setMaxWaitingTime

        public void setMaxWaitingTime​(long maxWaitingTime)
        Sets the maximum amount of time the client is waiting for a response.
        Parameters:
        maxWaitingTime - the maximum waiting time in milliseconds