public class InMemoryQueue<T>
extends java.lang.Object
GenericInMemoryQueue can have different type of consumers working on same data which means as queue logic,
some data will move to one consumer and other data will move to different consumer.
This use case is very rare, thus this new class tries to hnadle majority of normal use case.
As we have only one consumer of each type, we can think of adding or decreasing number of consumers easily.| Constructor and Description |
|---|
InMemoryQueue(java.lang.String queueName)
Creates a new in memory queue.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(T i)
Add item to the queue.
|
void |
addAllFromCollection(java.util.Collection<T> input)
This method adds data present in input collection
|
protected void |
clear(java.util.function.Consumer<java.util.Queue<T>>... consumer)
This method clears the queue.
|
void |
decrementBatchConsumer()
Decrement batch consumer count.
|
void |
decrementDirectConsumer()
Decrement direct consumer count.
|
java.lang.Integer |
getBatchConsumerCount()
Get current batch consumer count
|
protected java.lang.String |
getBatchConsumerName(java.lang.Integer i)
Name of batch consumer.
|
java.lang.Integer |
getDirectConsumerCount()
Get direct consumer count.
|
protected java.lang.String |
getDirectConsumerName(java.lang.Integer i)
Name of direct consumer.
|
java.lang.Long |
getNumberOfItemsInQueue()
Get current number of items in queue.
|
java.lang.Long |
getSleepTimePostConsumingAllMessage() |
java.lang.Long |
getSleepTimePostEachConsumption() |
void |
incrementBatchConsumer()
Increment batch consumer count.
|
void |
incrementDirectConsumer()
Increment direct consumer count.
|
boolean |
isBatchConsumerSet()
This method lets us know that for this queue, any batch consumer set.
|
boolean |
isDirectConsumerSet()
This method lets us know that for this queue, any direct consumer set.
|
protected void |
processForCounter(int start,
int end,
java.util.function.Consumer<java.lang.Integer> consumer)
Process for add/remove a consumer from given start to given end index.
|
void |
removeBatchConsumer()
Remove Batch Consumer.
|
void |
removeDirectConsumer()
Remove direct consumer.
|
protected void |
resetConsumer(java.util.function.Consumer<java.lang.Integer> unregisterConsumer,
java.util.function.Consumer<java.lang.Integer> registerConsumer,
int consumerCount,
java.util.concurrent.atomic.AtomicInteger index)
Whenever a consumer needs to be changed, this method is called to remove current consumers of queue and then add fresh consumers.
|
InMemoryQueue<T> |
setBatchConsumer(java.util.function.Consumer<java.util.List<T>> batchConsumer)
Set batch consumer logic
|
InMemoryQueue<T> |
setBatchConsumerCount(int batchConsumerCount)
Reset batch consumer count.
|
InMemoryQueue<T> |
setBatchSize(int batchSize)
Set batch size of the queue.
|
protected void |
setConsumerCount(int currentCount,
int newCount,
java.util.function.Consumer<java.lang.Integer> registerCounsumer,
java.util.function.Consumer<java.lang.Integer> unregisterConsumer)
This method is responsible for increasing/decreasing consumer count.
|
InMemoryQueue<T> |
setDirectConsumer(java.util.function.Consumer<T> consumer)
Set direct consumer which will work on the data present in queue.
|
InMemoryQueue<T> |
setDirectConsumerCount(int directConsumerCount)
Set direct consumer count.
|
InMemoryQueue<T> |
setSleepTimePostConsumingAllMessage(java.lang.Long sleepTimePostConsumingAllMessage)
This method sets sleepTimePostConsumingAllMessage with the value passed.
|
InMemoryQueue<T> |
setSleepTimePostEachConsumption(java.lang.Long sleepTimePostEachConsumption)
This method sets sleepTimePostEachConsumption with the value passed.
|
void |
shutdown()
Shutdown the queue.
|
public InMemoryQueue(java.lang.String queueName)
queueName - Name of queue.public java.lang.Long getSleepTimePostEachConsumption()
public InMemoryQueue<T> setSleepTimePostEachConsumption(java.lang.Long sleepTimePostEachConsumption)
sleepTimePostEachConsumption - the sleepTimePostEachConsumption to setprotected java.lang.String getBatchConsumerName(java.lang.Integer i)
i - Batch number idprotected java.lang.String getDirectConsumerName(java.lang.Integer i)
i - Direct consumer idpublic InMemoryQueue<T> setBatchSize(int batchSize)
batchSize - Batch sizeprotected void processForCounter(int start,
int end,
java.util.function.Consumer<java.lang.Integer> consumer)
start - Start index from which we have to perform operationend - End index till which we have to perform operationconsumer - Consumer which will perform actual operationpublic InMemoryQueue<T> setDirectConsumer(java.util.function.Consumer<T> consumer)
consumer - Consumer logicprotected void resetConsumer(java.util.function.Consumer<java.lang.Integer> unregisterConsumer,
java.util.function.Consumer<java.lang.Integer> registerConsumer,
int consumerCount,
java.util.concurrent.atomic.AtomicInteger index)
unregisterConsumer - Unregister consumer logicregisterConsumer - Register consumer logicconsumerCount - Count of consumers to maintainindex - Current indexpublic InMemoryQueue<T> setBatchConsumer(java.util.function.Consumer<java.util.List<T>> batchConsumer)
batchConsumer - Batch consumerprotected void setConsumerCount(int currentCount,
int newCount,
java.util.function.Consumer<java.lang.Integer> registerCounsumer,
java.util.function.Consumer<java.lang.Integer> unregisterConsumer)
currentCount - Current countnewCount - New count which needs to be setregisterCounsumer - Register logicunregisterConsumer - Unregister logicpublic InMemoryQueue<T> setDirectConsumerCount(int directConsumerCount)
directConsumerCount - Direct consumer countpublic InMemoryQueue<T> setBatchConsumerCount(int batchConsumerCount)
batchConsumerCount - batch consumers countpublic void shutdown()
public void addAllFromCollection(java.util.Collection<T> input)
input - Collection containing datapublic void add(T i)
i - Item to be added to queuepublic java.lang.Integer getBatchConsumerCount()
public java.lang.Integer getDirectConsumerCount()
public java.lang.Long getNumberOfItemsInQueue()
public void incrementDirectConsumer()
public void decrementDirectConsumer()
public void incrementBatchConsumer()
public void decrementBatchConsumer()
public boolean isDirectConsumerSet()
public boolean isBatchConsumerSet()
protected void clear(java.util.function.Consumer<java.util.Queue<T>>... consumer)
consumer - Consumers consuming data present in queuepublic void removeDirectConsumer()
public void removeBatchConsumer()
public java.lang.Long getSleepTimePostConsumingAllMessage()
public InMemoryQueue<T> setSleepTimePostConsumingAllMessage(java.lang.Long sleepTimePostConsumingAllMessage)
sleepTimePostConsumingAllMessage - the sleepTimePostConsumingAllMessage to set