Package org.hobbit.core.rabbit
Class RabbitRpcClient
java.lang.Object
org.hobbit.core.rabbit.RabbitRpcClient
- All Implemented Interfaces:
Closeable,AutoCloseable
This class implements a thread safe client that can process several RPC calls
in parallel.
- Author:
- Michael Röder (roeder@informatik.uni-leipzig.de)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classInternal implementation of a Consumer that receives messages on the reply queue and callsprocessResponseForRequest(String, byte[])of itsRabbitRpcClient.RabbitRpcClientConsumer.client.protected static classSimple extension of theAbstractFutureclass that waits for the response which is set by theRabbitRpcClient.RabbitRpcRequest.setResponse(byte[] response). -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Map<String,RabbitRpcClient.RabbitRpcRequest> Mapping of correlation Ids to theirRabbitRpcClient.RabbitRpcRequestinstances.private static final longThe default maximum amount of time in millisecond the client is waiting for a response = 600000Lms.private static final org.slf4j.Loggerprivate longThe maximum amount of time in millisecond the client is waiting for a response.private SemaphoreMutex for managing access to thecurrentRequestsobject.private RabbitQueueQueue used for the request.private RabbitQueueQueue used for the responses. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()static RabbitRpcClientCreates a StorageServiceClient using the given RabbitMQConnection.longprotected voidInitializes 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.protected voidprocessResponseForRequest(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.byte[]request(byte[] data) Sends the request, i.e., the given data, and blocks until the response is received.voidsetMaxWaitingTime(long maxWaitingTime) Sets the maximum amount of time the client is waiting for a response.
-
Field Details
-
LOGGER
private static final org.slf4j.Logger LOGGER -
DEFAULT_MAX_WAITING_TIME
private static final long DEFAULT_MAX_WAITING_TIMEThe default maximum amount of time in millisecond the client is waiting for a response = 600000Lms.- See Also:
-
requestQueue
Queue used for the request. -
responseQueue
Queue used for the responses. -
requestMapMutex
Mutex for managing access to thecurrentRequestsobject. -
currentRequests
Mapping of correlation Ids to theirRabbitRpcClient.RabbitRpcRequestinstances. -
maxWaitingTime
private long maxWaitingTimeThe maximum amount of time in millisecond the client is waiting for a response. The default value is defined byDEFAULT_MAX_WAITING_TIME.
-
-
Constructor Details
-
RabbitRpcClient
protected RabbitRpcClient()Constructor.
-
-
Method Details
-
create
public static RabbitRpcClient create(com.rabbitmq.client.Connection connection, String requestQueueName) throws IOException Creates a StorageServiceClient using the given RabbitMQConnection.- Parameters:
connection- RabbitMQ connection used for the communicationrequestQueueName- 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 queuesrequestQueueName- 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
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 responsebody- 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
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-