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
  • Field Details

    • log

      private static final org.apache.logging.log4j.Logger log
    • connection

      protected Connection connection
    • tableName

      protected final String tableName
    • jdbcUrl

      private final String jdbcUrl
    • username

      private final String username
    • password

      private final String password
  • Constructor Details

    • SQLQueueDatabase

      protected SQLQueueDatabase(SQLQueueDatabase.DBConfig config)
      Constructs a new SQLQueueDatabase instance.
      Parameters:
      config - Database configuration including connection details
      Throws:
      IllegalArgumentException - if table name contains invalid characters
  • Method Details

    • validateTableName

      private String validateTableName(String tableName)
      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

      protected abstract String getDatabaseType()
      Get the database type name for logging.
      Returns:
      Database type (e.g., "MariaDB", "PostgreSQL")
    • getCreateTableSQL

      protected abstract String 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:
      initialize in interface QueueDatabase<T extends Serializable>
    • createTableIfNotExists

      private void createTableIfNotExists() throws SQLException
      Create the queue table if it doesn't exist.
      Throws:
      SQLException - if table creation fails
    • enqueue

      public void enqueue(T item)
      Description copied from interface: QueueDatabase
      Add an item to the tail of the queue.
      Specified by:
      enqueue in interface QueueDatabase<T extends Serializable>
      Parameters:
      item - The item to enqueue
    • dequeue

      public T dequeue()
      Description copied from interface: QueueDatabase
      Remove and return the head of the queue, or null if empty.
      Specified by:
      dequeue in interface QueueDatabase<T extends Serializable>
      Returns:
      The head item or null if empty
    • peek

      public T peek()
      Description copied from interface: QueueDatabase
      Peek at the head without removing.
      Specified by:
      peek in interface QueueDatabase<T extends Serializable>
      Returns:
      The head item or null if empty
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: QueueDatabase
      Check if the queue is empty.
      Specified by:
      isEmpty in interface QueueDatabase<T extends Serializable>
      Returns:
      true if the queue is empty
    • size

      public long size()
      Description copied from interface: QueueDatabase
      Get the size of the queue.
      Specified by:
      size in interface QueueDatabase<T extends Serializable>
      Returns:
      The number of items in the queue
    • snapshot

      public List<T> snapshot()
      Description copied from interface: QueueDatabase
      Take a snapshot copy of current values for read-only inspection.
      Specified by:
      snapshot in interface QueueDatabase<T extends Serializable>
      Returns:
      List of all items in the queue
    • removeByIndex

      public boolean removeByIndex(int index)
      Description copied from interface: QueueDatabase
      Remove an item from the queue by index (0-based).
      Specified by:
      removeByIndex in interface QueueDatabase<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

      public int removeByIndices(List<Integer> indices)
      Description copied from interface: QueueDatabase
      Remove items from the queue by indices (0-based).
      Specified by:
      removeByIndices in interface QueueDatabase<T extends Serializable>
      Parameters:
      indices - The indices of items to remove
      Returns:
      Number of items successfully removed
    • removeByUID

      public boolean removeByUID(String uid)
      Description copied from interface: QueueDatabase
      Remove an item from the queue by UID (for RelaySession).
      Specified by:
      removeByUID in interface QueueDatabase<T extends Serializable>
      Parameters:
      uid - The UID of the item to remove
      Returns:
      true if item was removed, false if not found
    • removeByUIDs

      public int removeByUIDs(List<String> uids)
      Description copied from interface: QueueDatabase
      Remove items from the queue by UIDs (for RelaySession).
      Specified by:
      removeByUIDs in interface QueueDatabase<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: QueueDatabase
      Clear all items from the queue.
      Specified by:
      clear in interface QueueDatabase<T extends Serializable>
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • serialize

      private byte[] serialize(T item)
      Serialize an object to byte array using Java serialization.
      Parameters:
      item - Item to serialize
      Returns:
      Serialized byte array
    • deserialize

      private T deserialize(byte[] data)
      Deserialize a byte array to object using Java deserialization.
      Parameters:
      data - Byte array to deserialize
      Returns:
      Deserialized object