public class DataReceiverImpl extends Object implements 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.
| Modifier and Type | Class and Description |
|---|---|
static class |
DataReceiverImpl.Builder |
protected class |
DataReceiverImpl.MsgProcessingTask |
protected class |
DataReceiverImpl.MsgReceivingTask |
| Modifier and Type | Field and Description |
|---|---|
private DataHandler |
dataHandler |
private static int |
DEFAULT_MAX_PARALLEL_PROCESSED_MESSAGES |
private int |
errorCount |
private ExecutorService |
executor |
private static org.slf4j.Logger |
LOGGER |
protected RabbitQueue |
queue |
private TerminatableRunnable |
receiverTask |
private Thread |
receiverThread |
| Modifier | Constructor and Description |
|---|---|
protected |
DataReceiverImpl(RabbitQueue queue,
DataHandler handler,
int maxParallelProcessedMsgs) |
| Modifier and Type | Method and Description |
|---|---|
static DataReceiverImpl.Builder |
builder()
Returns a newly created
DataReceiverImpl.Builder. |
protected Runnable |
buildMsgProcessingTask(com.rabbitmq.client.QueueingConsumer.Delivery delivery)
This factory method creates a runnable task that processes the given message.
|
protected TerminatableRunnable |
buildMsgReceivingTask(com.rabbitmq.client.QueueingConsumer consumer)
This factory method creates a runnable task that uses the given consumer to
receive incoming messages.
|
void |
close()
A rude way to close the receiver.
|
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.
|
DataHandler |
getDataHandler() |
int |
getErrorCount()
Returns the number of errors that have been encountered while receiving
data.
|
protected ExecutorService |
getExecutor() |
RabbitQueue |
getQueue() |
void |
increaseErrorCount() |
private static final org.slf4j.Logger LOGGER
private static final int DEFAULT_MAX_PARALLEL_PROCESSED_MESSAGES
protected RabbitQueue queue
private int errorCount
private DataHandler dataHandler
private ExecutorService executor
private TerminatableRunnable receiverTask
private Thread receiverThread
protected DataReceiverImpl(RabbitQueue queue, DataHandler handler, int maxParallelProcessedMsgs) throws IOException
IOExceptionpublic DataHandler getDataHandler()
getDataHandler in interface DataReceiverpublic void increaseErrorCount()
increaseErrorCount in interface DataReceiverpublic int getErrorCount()
DataReceivergetErrorCount in interface DataReceiverpublic RabbitQueue getQueue()
getQueue in interface DataReceiverprotected ExecutorService getExecutor()
public void closeWhenFinished()
closeWhenFinished in interface DataReceiverpublic void close()
close in interface Closeableclose in interface AutoCloseablepublic static DataReceiverImpl.Builder builder()
DataReceiverImpl.Builder.DataReceiverImpl.Builder instanceprotected TerminatableRunnable buildMsgReceivingTask(com.rabbitmq.client.QueueingConsumer consumer)
consumer - the consumer that can be used to receive messagesprotected Runnable buildMsgProcessingTask(com.rabbitmq.client.QueueingConsumer.Delivery delivery)
delivery - the message that should be processedCopyright © 2017–2018. All rights reserved.