Package com.github.ddth.queue.impl
Class BaseRedisQueue<ID,DATA>
- java.lang.Object
-
- com.github.ddth.queue.impl.AbstractQueue<ID,DATA>
-
- com.github.ddth.queue.impl.AbstractEphemeralSupportQueue<ID,DATA>
-
- com.github.ddth.queue.impl.BaseRedisQueue<ID,DATA>
-
- All Implemented Interfaces:
IQueue<ID,DATA>,AutoCloseable
- Direct Known Subclasses:
RedisQueue
public abstract class BaseRedisQueue<ID,DATA> extends AbstractEphemeralSupportQueue<ID,DATA>
Base Redis implementation ofIQueue.Implementation:
- A hash to store message, format {queue_id => message}. See
setRedisHashName(String). - A list to act as a queue of message's queue_id. See
setRedisListName(String). - A sorted set to act as ephemeral storage of message's queue_id, score is
message's timestamp. See
setRedisSortedSetName(String).
Features:
- Queue-size support: yes
- Ephemeral storage support: yes
- Ephemeral-size support: yes
- Since:
- 0.6.2.6
- Author:
- Thanh Ba Nguyen
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.github.ddth.queue.impl.AbstractQueue
AbstractQueue.PutToQueueCase
-
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_HASH_NAMEstatic StringDEFAULT_LIST_NAMEstatic StringDEFAULT_PASSWORDstatic StringDEFAULT_SORTED_SET_NAMEprotected booleanmyOwnRedisFlag to mark if the Redis resource (e.g.-
Fields inherited from interface com.github.ddth.queue.IQueue
SIZE_NOT_SUPPORTED
-
-
Constructor Summary
Constructors Constructor Description BaseRedisQueue()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract com.github.ddth.commons.redis.JedisConnectorbuildJedisConnector()Build aJedisConnectorinstance for my own use.protected abstract voidcloseJedisCommands(redis.clients.jedis.BinaryJedisCommands jedisCommands)Close the unusedBinaryJedisCommands.voiddestroy()Destroy method.protected booleandoPutToQueue(IQueueMessage<ID,DATA> msg, AbstractQueue.PutToQueueCase queueCase)Put a message to the queue storage.intephemeralSize()Get number of items currently in ephemeral storage.voidfinish(IQueueMessage<ID,DATA> msg)Called when finish processing the message to cleanup ephemeral storage.protected abstract redis.clients.jedis.BinaryJedisCommandsgetBinaryJedisCommands()GetBinaryJedisCommandsinstance.com.github.ddth.commons.redis.JedisConnectorgetJedisConnector()Get the currentJedisConnectorused by this queue.Collection<IQueueMessage<ID,DATA>>getOrphanMessages(long thresholdTimestampMs)Get all orphan messages (messages that were left in ephemeral storage for a long time).StringgetRedisHashName()Name of the Redis hash to store queue messages.byte[]getRedisHashNameAsBytes()Name of the Redis hash to store queue messages.StringgetRedisListName()Name of the Redis list to store queue message ids.byte[]getRedisListNameAsBytes()Name of the Redis list to store queue message ids.StringgetRedisPassword()Redis' password.StringgetRedisSortedSetName()Name of the Redis sorted-set to store ephemeral message ids.byte[]getRedisSortedSetNameAsBytes()Name of the Redis sorted-set to store ephemeral message ids.StringgetScriptTake()LUA script to take a message out of queue.protected byte[]getScriptTakeAsBytes()LUA script to take a message out of queue.BaseRedisQueue<ID,DATA>init()Init method.intqueueSize()Get number of items currently in queue storage.protected abstract booleanremove(IQueueMessage<ID,DATA> msg)Remove a message completely.BaseRedisQueue<ID,DATA>setJedisConnector(com.github.ddth.commons.redis.JedisConnector jedisConnector)Set the externalJedisConnectorto be used by this queue.protected BaseRedisQueue<ID,DATA>setJedisConnector(com.github.ddth.commons.redis.JedisConnector jedisConnector, boolean setMyOwnRedis)Setter forjedisConnector.BaseRedisQueue<ID,DATA>setRedisHashName(String redisHashName)Name of the Redis hash to store queue messages.BaseRedisQueue<ID,DATA>setRedisListName(String redisListName)Name of the Redis list to store queue message ids.BaseRedisQueue<ID,DATA>setRedisPassword(String redisPassword)Redis' password.BaseRedisQueue<ID,DATA>setRedisSortedSetName(String redisSortedSetName)Name of the Redis sorted-set to store ephemeral message ids.BaseRedisQueue<ID,DATA>setScriptTake(String scriptTake)LUA script to take a message out of queue.protected abstract booleanstoreNew(IQueueMessage<ID,DATA> msg)Store a new message.protected abstract booleanstoreOld(IQueueMessage<ID,DATA> msg)Re-store an old message (called byAbstractQueue.requeue(IQueueMessage)orAbstractQueue.requeueSilent(IQueueMessage).-
Methods inherited from class com.github.ddth.queue.impl.AbstractEphemeralSupportQueue
getEphemeralDisabled, getEphemeralMaxSize, isEphemeralDisabled, setEphemeralDisabled, setEphemeralMaxSize
-
Methods inherited from class com.github.ddth.queue.impl.AbstractQueue
close, createMessage, createMessage, createMessage, deserialize, deserialize, getMessageFactory, getQueueName, getSerDeser, queue, requeue, requeueSilent, serialize, setMessageFactory, setQueueName, setSerDeser
-
-
-
-
Field Detail
-
DEFAULT_PASSWORD
public static final String DEFAULT_PASSWORD
-
DEFAULT_HASH_NAME
public static final String DEFAULT_HASH_NAME
- See Also:
- Constant Field Values
-
DEFAULT_LIST_NAME
public static final String DEFAULT_LIST_NAME
- See Also:
- Constant Field Values
-
DEFAULT_SORTED_SET_NAME
public static final String DEFAULT_SORTED_SET_NAME
- See Also:
- Constant Field Values
-
myOwnRedis
protected boolean myOwnRedis
Flag to mark if the Redis resource (e.g. Redis client pool) is created and handled by the lock instance.
-
-
Method Detail
-
getJedisConnector
public com.github.ddth.commons.redis.JedisConnector getJedisConnector()
Get the currentJedisConnectorused by this queue.- Returns:
-
setJedisConnector
protected BaseRedisQueue<ID,DATA> setJedisConnector(com.github.ddth.commons.redis.JedisConnector jedisConnector, boolean setMyOwnRedis)
Setter forjedisConnector.- Parameters:
jedisConnector-setMyOwnRedis-- Returns:
- Since:
- 0.7.1
-
setJedisConnector
public BaseRedisQueue<ID,DATA> setJedisConnector(com.github.ddth.commons.redis.JedisConnector jedisConnector)
Set the externalJedisConnectorto be used by this queue.- Parameters:
jedisConnector-- Returns:
-
getRedisPassword
public String getRedisPassword()
Redis' password.- Returns:
-
setRedisPassword
public BaseRedisQueue<ID,DATA> setRedisPassword(String redisPassword)
Redis' password.- Parameters:
redisPassword-- Returns:
-
getRedisHashName
public String getRedisHashName()
Name of the Redis hash to store queue messages.- Returns:
-
getRedisHashNameAsBytes
public byte[] getRedisHashNameAsBytes()
Name of the Redis hash to store queue messages.- Returns:
-
setRedisHashName
public BaseRedisQueue<ID,DATA> setRedisHashName(String redisHashName)
Name of the Redis hash to store queue messages.- Parameters:
redisHashName-- Returns:
-
getRedisListName
public String getRedisListName()
Name of the Redis list to store queue message ids.- Returns:
-
getRedisListNameAsBytes
public byte[] getRedisListNameAsBytes()
Name of the Redis list to store queue message ids.- Returns:
-
setRedisListName
public BaseRedisQueue<ID,DATA> setRedisListName(String redisListName)
Name of the Redis list to store queue message ids.- Parameters:
redisListName-- Returns:
-
getRedisSortedSetName
public String getRedisSortedSetName()
Name of the Redis sorted-set to store ephemeral message ids.- Returns:
-
getRedisSortedSetNameAsBytes
public byte[] getRedisSortedSetNameAsBytes()
Name of the Redis sorted-set to store ephemeral message ids.- Returns:
-
setRedisSortedSetName
public BaseRedisQueue<ID,DATA> setRedisSortedSetName(String redisSortedSetName)
Name of the Redis sorted-set to store ephemeral message ids.- Parameters:
redisSortedSetName-- Returns:
-
getScriptTake
public String getScriptTake()
LUA script to take a message out of queue.- Returns:
-
getScriptTakeAsBytes
protected byte[] getScriptTakeAsBytes()
LUA script to take a message out of queue.- Returns:
- Since:
- 1.0.0
-
setScriptTake
public BaseRedisQueue<ID,DATA> setScriptTake(String scriptTake)
LUA script to take a message out of queue.Normally queue implementations will build this script; no need to "set" it from outside.
- Parameters:
scriptTake-- Returns:
-
buildJedisConnector
protected abstract com.github.ddth.commons.redis.JedisConnector buildJedisConnector()
Build aJedisConnectorinstance for my own use.- Returns:
- Since:
- 0.6.2.6
-
init
public BaseRedisQueue<ID,DATA> init() throws Exception
Init method.- Overrides:
initin classAbstractQueue<ID,DATA>- Returns:
- Throws:
Exception
-
destroy
public void destroy()
Destroy method.- Overrides:
destroyin classAbstractQueue<ID,DATA>
-
getBinaryJedisCommands
protected abstract redis.clients.jedis.BinaryJedisCommands getBinaryJedisCommands()
GetBinaryJedisCommandsinstance.- Returns:
- Since:
- 0.6.2.6
-
closeJedisCommands
protected abstract void closeJedisCommands(redis.clients.jedis.BinaryJedisCommands jedisCommands)
Close the unusedBinaryJedisCommands.- Parameters:
jedisCommands-- Since:
- 0.6.2.6
-
remove
protected abstract boolean remove(IQueueMessage<ID,DATA> msg) throws QueueException
Remove a message completely.Implementation note:
- Message should no longer exist after method call regardless of return value (
trueorfalse) - In case of error, throws
QueueException
- Parameters:
msg-- Returns:
trueif the message has been removed,falsemeans the message does not exist for completely removal- Throws:
QueueException- if error (e.g. IOException while communicating with Redis server)
- Message should no longer exist after method call regardless of return value (
-
storeNew
protected abstract boolean storeNew(IQueueMessage<ID,DATA> msg)
Store a new message.Implementation note:
- Message's ID must be successfully pushed into the "queue-list" so that the method call is considered successful.
If message's id has not pushed to the "queue-list", this method must return
false. - Caller can retry if method returns
false. Implementation's responsibility to ensure message is not duplicated in storage. - In case of error, throws
QueueException
- Parameters:
msg-- Returns:
- Message's ID must be successfully pushed into the "queue-list" so that the method call is considered successful.
If message's id has not pushed to the "queue-list", this method must return
-
storeOld
protected abstract boolean storeOld(IQueueMessage<ID,DATA> msg)
Re-store an old message (called byAbstractQueue.requeue(IQueueMessage)orAbstractQueue.requeueSilent(IQueueMessage).Implementation note:
- Message's ID must be successfully pushed into the "queue-list" so that the method call is considered successful.
If message's id has not pushed to the "queue-list", this method must return
false. - Caller can retry if method returns
false. Implementation's responsibility to ensure message is not duplicated in storage. - In case of error, throws
QueueException
- Parameters:
msg-- Returns:
- Message's ID must be successfully pushed into the "queue-list" so that the method call is considered successful.
If message's id has not pushed to the "queue-list", this method must return
-
doPutToQueue
protected boolean doPutToQueue(IQueueMessage<ID,DATA> msg, AbstractQueue.PutToQueueCase queueCase)
Put a message to the queue storage.Sub-class implements this method.
- Specified by:
doPutToQueuein classAbstractQueue<ID,DATA>- Returns:
-
finish
public void finish(IQueueMessage<ID,DATA> msg)
Called when finish processing the message to cleanup ephemeral storage.Implementation flow:
- Remove message from ephemeral storage.
Note: ephemeral storage implementation is optional, depends on implementation.
-
getOrphanMessages
public Collection<IQueueMessage<ID,DATA>> getOrphanMessages(long thresholdTimestampMs)
Get all orphan messages (messages that were left in ephemeral storage for a long time).- Parameters:
thresholdTimestampMs- message is orphan ifmessage's timestampMillis + thresholdTimestampMs < now. Which meansgetOrphanMessages(10000)will return orphan messages that have stayed in ephemeral storage for more than 10000 milliseconds.- Returns:
nullor empty collection if there is no orphan message
-
queueSize
public int queueSize()
Get number of items currently in queue storage.- Returns:
- negative number if queue size can not be queried
-
ephemeralSize
public int ephemeralSize()
Get number of items currently in ephemeral storage.Note: ephemeral storage implementation is optional, depends on implementation.
- Returns:
- negative number if ephemeral size can not be queried
-
-