Class DataReceiverImpl
- All Implemented Interfaces:
Closeable,AutoCloseable,DataReceiver
DataReceiver interface.
Use the internal DataReceiverImpl.Builder class for creating instances of the
DataReceiverImpl class. Note that the created
DataReceiverImpl will either use a given RabbitQueue or
create a new one. In both cases the receiver will become the owner of the
queue, i.e., if the DataReceiverImpl instance is closed the queue
will be closed as well.
Internally, the receiver uses an own thread to consume incoming messages.
These messages are forwarded to the given DataHandler instance.
Note that this forwarding is based on an ExecutorService the
called method DataHandler.handleData(byte[]) should be thread safe
since it might be called in parallel.
The DataReceiverImpl owns recources that need to be freed if its work
is done. This can be achieved by closing the receiver. In most cases, this
should be done using the closeWhenFinished() method which waits
until all incoming messages are processed and all streams are closed. Note
that using the close() method leads to a direct shutdown of the
queue which could lead to data loss and threads getting stuck.
- Author:
- Michael Röder (roeder@informatik.uni-leipzig.de)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classprotected classprotected class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate DataHandlerprivate static final intprivate intprivate ExecutorServiceprivate static final org.slf4j.Loggerprotected RabbitQueueprivate TerminatableRunnableprivate Thread -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDataReceiverImpl(RabbitQueue queue, DataHandler handler, int maxParallelProcessedMsgs) -
Method Summary
Modifier and TypeMethodDescriptionstatic DataReceiverImpl.Builderbuilder()Returns a newly createdDataReceiverImpl.Builder.protected RunnablebuildMsgProcessingTask(com.rabbitmq.client.Delivery delivery) This factory method creates a runnable task that processes the given message.protected TerminatableRunnablebuildMsgReceivingTask(QueueingConsumer consumer) This factory method creates a runnable task that uses the given consumer to receive incoming messages.voidclose()A rude way to close the receiver.voidThis method waits for the data receiver to finish its work and closes the incoming queue as well as the internal thread pool after that.intReturns the number of errors that have been encountered while receiving data.protected ExecutorServicegetQueue()void
-
Field Details
-
LOGGER
private static final org.slf4j.Logger LOGGER -
DEFAULT_MAX_PARALLEL_PROCESSED_MESSAGES
private static final int DEFAULT_MAX_PARALLEL_PROCESSED_MESSAGES- See Also:
-
queue
-
errorCount
private int errorCount -
dataHandler
-
executor
-
receiverTask
-
receiverThread
-
-
Constructor Details
-
DataReceiverImpl
protected DataReceiverImpl(RabbitQueue queue, DataHandler handler, int maxParallelProcessedMsgs) throws IOException - Throws:
IOException
-
-
Method Details
-
getDataHandler
- Specified by:
getDataHandlerin interfaceDataReceiver
-
increaseErrorCount
public void increaseErrorCount()- Specified by:
increaseErrorCountin interfaceDataReceiver
-
getErrorCount
public int getErrorCount()Description copied from interface:DataReceiverReturns the number of errors that have been encountered while receiving data. If this number is not 0 the receiver can not guarantee that all data has been received correctly.- Specified by:
getErrorCountin interfaceDataReceiver- Returns:
- the number of errors encountered during the receiving of data
-
getQueue
- Specified by:
getQueuein interfaceDataReceiver
-
getExecutor
-
closeWhenFinished
public void closeWhenFinished()This method waits for the data receiver to finish its work and closes the incoming queue as well as the internal thread pool after that.- Specified by:
closeWhenFinishedin interfaceDataReceiver
-
close
public void close()A rude way to close the receiver. Note that this method directly closes the incoming queue and only notifies the internal consumer to stop its work but won't wait for the handler threads to finish their work.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
builder
Returns a newly createdDataReceiverImpl.Builder.- Returns:
- a new
DataReceiverImpl.Builderinstance
-
buildMsgReceivingTask
This factory method creates a runnable task that uses the given consumer to receive incoming messages.- Parameters:
consumer- the consumer that can be used to receive messages- Returns:
- a Runnable instance that will handle incoming messages as soon as it will be executed
-
buildMsgProcessingTask
This factory method creates a runnable task that processes the given message.- Parameters:
delivery- the message that should be processed- Returns:
- a Runnable instance that will process the message as soon as it will be executed
-