Package com.mimecast.robin.queue
Class SQLQueueDatabase<T extends Serializable>
java.lang.Object
com.mimecast.robin.queue.SQLQueueDatabase<T>
- Type Parameters:
T- Type of items stored in the queue, must be Serializable
- All Implemented Interfaces:
QueueDatabase<T>,Closeable,AutoCloseable
- Direct Known Subclasses:
QueueMariaDB,QueuePgSQL
public abstract class SQLQueueDatabase<T extends Serializable>
extends Object
implements QueueDatabase<T>
Abstract base class for SQL-based queue database implementations.
Provides common functionality for MariaDB and PostgreSQL backends including:
- Connection management
- FIFO queue operations (enqueue, dequeue, peek)
- Item removal by index or UID
- Serialization/deserialization of queue items
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classDatabase configuration holder. -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructs a new SQLQueueDatabase instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clear all items from the queue.voidclose()private voidCreate the queue table if it doesn't exist.dequeue()Remove and return the head of the queue, or null if empty.private Tdeserialize(byte[] data) Deserialize a byte array to object using Java deserialization.voidAdd an item to the tail of the queue.protected abstract StringGet the SQL for creating the queue table.protected abstract StringGet the database type name for logging.voidInitialize the database connection and create table if needed.booleanisEmpty()Check if the queue is empty.peek()Peek at the head without removing.booleanremoveByIndex(int index) Remove an item from the queue by index (0-based).intremoveByIndices(List<Integer> indices) Remove items from the queue by indices (0-based).booleanremoveByUID(String uid) Remove an item from the queue by UID (for RelaySession).intremoveByUIDs(List<String> uids) Remove items from the queue by UIDs (for RelaySession).private byte[]Serialize an object to byte array using Java serialization.longsize()Get the size of the queue.snapshot()Take a snapshot copy of current values for read-only inspection.private StringvalidateTableName(String tableName) Validate table name to prevent SQL injection.
-
Field Details
-
log
private static final org.apache.logging.log4j.Logger log -
connection
-
tableName
-
jdbcUrl
-
username
-
password
-
-
Constructor Details
-
SQLQueueDatabase
Constructs a new SQLQueueDatabase instance.- Parameters:
config- Database configuration including connection details- Throws:
IllegalArgumentException- if table name contains invalid characters
-
-
Method Details
-
validateTableName
Validate table name to prevent SQL injection. Only allows alphanumeric characters and underscores.- Parameters:
tableName- Table name to validate- Returns:
- Validated table name
- Throws:
IllegalArgumentException- if table name contains invalid characters
-
getDatabaseType
Get the database type name for logging.- Returns:
- Database type (e.g., "MariaDB", "PostgreSQL")
-
getCreateTableSQL
Get the SQL for creating the queue table.Should include table structure with id, data, and created_at columns.
- Returns:
- CREATE TABLE SQL statement
-
initialize
public void initialize()Initialize the database connection and create table if needed.- Specified by:
initializein interfaceQueueDatabase<T extends Serializable>
-
createTableIfNotExists
Create the queue table if it doesn't exist.- Throws:
SQLException- if table creation fails
-
enqueue
Description copied from interface:QueueDatabaseAdd an item to the tail of the queue.- Specified by:
enqueuein interfaceQueueDatabase<T extends Serializable>- Parameters:
item- The item to enqueue
-
dequeue
Description copied from interface:QueueDatabaseRemove and return the head of the queue, or null if empty.- Specified by:
dequeuein interfaceQueueDatabase<T extends Serializable>- Returns:
- The head item or null if empty
-
peek
Description copied from interface:QueueDatabasePeek at the head without removing.- Specified by:
peekin interfaceQueueDatabase<T extends Serializable>- Returns:
- The head item or null if empty
-
isEmpty
public boolean isEmpty()Description copied from interface:QueueDatabaseCheck if the queue is empty.- Specified by:
isEmptyin interfaceQueueDatabase<T extends Serializable>- Returns:
- true if the queue is empty
-
size
public long size()Description copied from interface:QueueDatabaseGet the size of the queue.- Specified by:
sizein interfaceQueueDatabase<T extends Serializable>- Returns:
- The number of items in the queue
-
snapshot
Description copied from interface:QueueDatabaseTake a snapshot copy of current values for read-only inspection.- Specified by:
snapshotin interfaceQueueDatabase<T extends Serializable>- Returns:
- List of all items in the queue
-
removeByIndex
public boolean removeByIndex(int index) Description copied from interface:QueueDatabaseRemove an item from the queue by index (0-based).- Specified by:
removeByIndexin interfaceQueueDatabase<T extends Serializable>- Parameters:
index- The index of the item to remove- Returns:
- true if item was removed, false if index was out of bounds
-
removeByIndices
Description copied from interface:QueueDatabaseRemove items from the queue by indices (0-based).- Specified by:
removeByIndicesin interfaceQueueDatabase<T extends Serializable>- Parameters:
indices- The indices of items to remove- Returns:
- Number of items successfully removed
-
removeByUID
Description copied from interface:QueueDatabaseRemove an item from the queue by UID (for RelaySession).- Specified by:
removeByUIDin interfaceQueueDatabase<T extends Serializable>- Parameters:
uid- The UID of the item to remove- Returns:
- true if item was removed, false if not found
-
removeByUIDs
Description copied from interface:QueueDatabaseRemove items from the queue by UIDs (for RelaySession).- Specified by:
removeByUIDsin interfaceQueueDatabase<T extends Serializable>- Parameters:
uids- The UIDs of items to remove- Returns:
- Number of items successfully removed
-
clear
public void clear()Description copied from interface:QueueDatabaseClear all items from the queue.- Specified by:
clearin interfaceQueueDatabase<T extends Serializable>
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
serialize
Serialize an object to byte array using Java serialization.- Parameters:
item- Item to serialize- Returns:
- Serialized byte array
-
deserialize
Deserialize a byte array to object using Java deserialization.- Parameters:
data- Byte array to deserialize- Returns:
- Deserialized object
-