Package com.github.ddth.queue.impl
Class RabbitMqQueue<ID,DATA>
- java.lang.Object
-
- com.github.ddth.queue.impl.AbstractQueue<ID,DATA>
-
- com.github.ddth.queue.impl.RabbitMqQueue<ID,DATA>
-
- All Implemented Interfaces:
IQueue<ID,DATA>,AutoCloseable
- Direct Known Subclasses:
BaseUniversalRabbitMqQueue
public abstract class RabbitMqQueue<ID,DATA> extends AbstractQueue<ID,DATA>
(Experimental) RabbitMQ implementation ofIQueue.- Queue-size support: no
- Ephemeral storage support: no
- Since:
- 0.6.1
- 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_QUEUE_NAMEstatic StringDEFAULT_URI-
Fields inherited from interface com.github.ddth.queue.IQueue
SIZE_NOT_SUPPORTED
-
-
Constructor Summary
Constructors Constructor Description RabbitMqQueue()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected com.rabbitmq.client.ConnectionFactorybuildConnectionFactory()protected voidcloseQuietly(com.rabbitmq.client.Channel channel)protected voidcloseQuietly(com.rabbitmq.client.Connection connection)protected com.rabbitmq.client.ChannelcreateChannel()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 com.rabbitmq.client.ConnectiongetConnection()protected com.rabbitmq.client.ConnectionFactorygetConnectionFactory()Getter forconnectionFactory.protected com.rabbitmq.client.ChannelgetConsumerChannel()Get theChanneldedicated for consuming messages.Collection<IQueueMessage<ID,DATA>>getOrphanMessages(long thresholdTimestampMs)Get all orphan messages (messages that were left in ephemeral storage for a long time).protected com.rabbitmq.client.ChannelgetProducerChannel()Get theChanneldedicated for sending messages.StringgetQueueName()Name of RabbitMQ queue to send/receive messages.StringgetUri()RabbitMQ's connection URI (formatamqp://username:password@host:port/virtualHost).RabbitMqQueue<ID,DATA>init()Init method.intqueueSize()Get number of items currently in queue storage.RabbitMqQueue<ID,DATA>setConnectionFactory(com.rabbitmq.client.ConnectionFactory connectionFactory)Setter forconnectionFactory.protected RabbitMqQueue<ID,DATA>setConnectionFactory(com.rabbitmq.client.ConnectionFactory connectionFactory, boolean setMyOwnConnectionFactory)Setter forconnectionFactory.RabbitMqQueue<ID,DATA>setQueueName(String queueName)Name of RabbitMQ queue to send/receive messages.RabbitMqQueue<ID,DATA>setUri(String uri)RabbitMQ's connection URI (formatamqp://username:password@host:port/virtualHost).IQueueMessage<ID,DATA>take()Take a message out of queue.-
Methods inherited from class com.github.ddth.queue.impl.AbstractQueue
close, createMessage, createMessage, createMessage, deserialize, deserialize, getMessageFactory, getSerDeser, queue, requeue, requeueSilent, serialize, setMessageFactory, setSerDeser
-
-
-
-
Field Detail
-
DEFAULT_URI
public static final String DEFAULT_URI
- See Also:
- Constant Field Values
-
DEFAULT_QUEUE_NAME
public static final String DEFAULT_QUEUE_NAME
- See Also:
- Constant Field Values
-
-
Method Detail
-
getUri
public String getUri()
RabbitMQ's connection URI (formatamqp://username:password@host:port/virtualHost).- Returns:
-
setUri
public RabbitMqQueue<ID,DATA> setUri(String uri)
RabbitMQ's connection URI (formatamqp://username:password@host:port/virtualHost).- Parameters:
uri-- Returns:
-
getQueueName
public String getQueueName()
Name of RabbitMQ queue to send/receive messages.- Overrides:
getQueueNamein classAbstractQueue<ID,DATA>- Returns:
-
setQueueName
public RabbitMqQueue<ID,DATA> setQueueName(String queueName)
Name of RabbitMQ queue to send/receive messages.- Overrides:
setQueueNamein classAbstractQueue<ID,DATA>- Parameters:
queueName-- Returns:
-
getConnectionFactory
protected com.rabbitmq.client.ConnectionFactory getConnectionFactory()
Getter forconnectionFactory.- Returns:
-
setConnectionFactory
protected RabbitMqQueue<ID,DATA> setConnectionFactory(com.rabbitmq.client.ConnectionFactory connectionFactory, boolean setMyOwnConnectionFactory)
Setter forconnectionFactory.- Parameters:
connectionFactory-setMyOwnConnectionFactory-- Returns:
- Since:
- 0.7.1
-
setConnectionFactory
public RabbitMqQueue<ID,DATA> setConnectionFactory(com.rabbitmq.client.ConnectionFactory connectionFactory)
Setter forconnectionFactory.- Parameters:
connectionFactory-- Returns:
-
getConnection
protected com.rabbitmq.client.Connection getConnection() throws IOException, TimeoutException- Throws:
IOExceptionTimeoutException
-
createChannel
protected com.rabbitmq.client.Channel createChannel() throws IOException, TimeoutException- Throws:
IOExceptionTimeoutException
-
getProducerChannel
protected com.rabbitmq.client.Channel getProducerChannel() throws IOException, TimeoutExceptionGet theChanneldedicated for sending messages.- Returns:
- Throws:
IOExceptionTimeoutException
-
getConsumerChannel
protected com.rabbitmq.client.Channel getConsumerChannel() throws IOException, TimeoutExceptionGet theChanneldedicated for consuming messages.- Returns:
- Throws:
IOExceptionTimeoutException
-
buildConnectionFactory
protected com.rabbitmq.client.ConnectionFactory buildConnectionFactory() throws KeyManagementException, NoSuchAlgorithmException, URISyntaxException- Returns:
- Throws:
URISyntaxExceptionNoSuchAlgorithmExceptionKeyManagementException- Since:
- 0.6.2.6
-
init
public RabbitMqQueue<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>
-
closeQuietly
protected void closeQuietly(com.rabbitmq.client.Connection connection)
-
closeQuietly
protected void closeQuietly(com.rabbitmq.client.Channel channel)
-
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.
-
doPutToQueue
protected boolean doPutToQueue(IQueueMessage<ID,DATA> msg, AbstractQueue.PutToQueueCase queueCase)
Put a message to the queue storage.Sub-class implements this method.
queueCaseis ignore as we always add new message to RabbitMQ.- Specified by:
doPutToQueuein classAbstractQueue<ID,DATA>- Returns:
-
take
public IQueueMessage<ID,DATA> take() throws QueueException.EphemeralIsFull
Take a message out of queue.Implementation flow:
- Read message from head of queue storage.
- Write message to ephemeral storage.
- Remove message from queue storage.
Note: ephemeral storage implementation is optional, depends on implementation.
- Returns:
- Throws:
QueueException.EphemeralIsFull- if the ephemeral storage is full
-
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
-
-