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 String
DEFAULT_QUEUE_NAME
static String
DEFAULT_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.ConnectionFactory
buildConnectionFactory()
protected void
closeQuietly(com.rabbitmq.client.Channel channel)
protected void
closeQuietly(com.rabbitmq.client.Connection connection)
protected com.rabbitmq.client.Channel
createChannel()
void
destroy()
Destroy method.protected boolean
doPutToQueue(IQueueMessage<ID,DATA> msg, AbstractQueue.PutToQueueCase queueCase)
Put a message to the queue storage.int
ephemeralSize()
Get number of items currently in ephemeral storage.void
finish(IQueueMessage<ID,DATA> msg)
Called when finish processing the message to cleanup ephemeral storage.protected com.rabbitmq.client.Connection
getConnection()
protected com.rabbitmq.client.ConnectionFactory
getConnectionFactory()
Getter forconnectionFactory
.protected com.rabbitmq.client.Channel
getConsumerChannel()
Get theChannel
dedicated 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.Channel
getProducerChannel()
Get theChannel
dedicated for sending messages.String
getQueueName()
Name of RabbitMQ queue to send/receive messages.String
getUri()
RabbitMQ's connection URI (formatamqp://username:password@host:port/virtualHost
).RabbitMqQueue<ID,DATA>
init()
Init method.int
queueSize()
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:
getQueueName
in classAbstractQueue<ID,DATA>
- Returns:
-
setQueueName
public RabbitMqQueue<ID,DATA> setQueueName(String queueName)
Name of RabbitMQ queue to send/receive messages.- Overrides:
setQueueName
in 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:
IOException
TimeoutException
-
createChannel
protected com.rabbitmq.client.Channel createChannel() throws IOException, TimeoutException
- Throws:
IOException
TimeoutException
-
getProducerChannel
protected com.rabbitmq.client.Channel getProducerChannel() throws IOException, TimeoutException
Get theChannel
dedicated for sending messages.- Returns:
- Throws:
IOException
TimeoutException
-
getConsumerChannel
protected com.rabbitmq.client.Channel getConsumerChannel() throws IOException, TimeoutException
Get theChannel
dedicated for consuming messages.- Returns:
- Throws:
IOException
TimeoutException
-
buildConnectionFactory
protected com.rabbitmq.client.ConnectionFactory buildConnectionFactory() throws KeyManagementException, NoSuchAlgorithmException, URISyntaxException
- Returns:
- Throws:
URISyntaxException
NoSuchAlgorithmException
KeyManagementException
- Since:
- 0.6.2.6
-
init
public RabbitMqQueue<ID,DATA> init() throws Exception
Init method.- Overrides:
init
in classAbstractQueue<ID,DATA>
- Returns:
- Throws:
Exception
-
destroy
public void destroy()
Destroy method.- Overrides:
destroy
in 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.
queueCase
is ignore as we always add new message to RabbitMQ.- Specified by:
doPutToQueue
in 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:
null
or 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
-
-