Class JdbcQueue<ID,​DATA>

    • Field Detail

      • DEFAULT_TRANX_ISOLATION_LEVEL

        public static final int DEFAULT_TRANX_ISOLATION_LEVEL
        See Also:
        Constant Field Values
    • Constructor Detail

      • JdbcQueue

        public JdbcQueue()
    • Method Detail

      • setTableName

        public JdbcQueue<ID,​DATA> setTableName​(String tableName)
        Name of database table to store queue messages.
        Parameters:
        tableName -
        Returns:
      • getTableName

        public String getTableName()
        Name of database table to store queue messages.
        Returns:
      • setTableNameEphemeral

        public JdbcQueue<ID,​DATA> setTableNameEphemeral​(String tableNameEphemeral)
        Name of database table to store ephemeral messages.
        Parameters:
        tableNameEphemeral -
        Returns:
      • getTableNameEphemeral

        public String getTableNameEphemeral()
        Name of database table to store ephemeral messages.
        Returns:
      • setTransactionIsolationLevel

        public JdbcQueue<ID,​DATA> setTransactionIsolationLevel​(int transactionIsolationLevel)
        Transaction isolation level used in DB-operations.
        Parameters:
        transactionIsolationLevel -
        Returns:
      • getTransactionIsolationLevel

        public int getTransactionIsolationLevel()
        Transaction isolation level used in DB-operations.
        Returns:
      • getJdbcHelper

        public com.github.ddth.dao.jdbc.IJdbcHelper getJdbcHelper()
        Getter for jdbcHelper.
        Returns:
        Since:
        0.5.0
      • setJdbcHelper

        protected JdbcQueue<ID,​DATA> setJdbcHelper​(com.github.ddth.dao.jdbc.IJdbcHelper jdbcHelper,
                                                         boolean setMyOwnJdbcHelper)
        Setter for jdbcHelper.
        Parameters:
        jdbcHelper -
        setMyOwnJdbcHelper -
        Returns:
        Since:
        0.7.1
      • setJdbcHelper

        public JdbcQueue<ID,​DATA> setJdbcHelper​(com.github.ddth.dao.jdbc.IJdbcHelper jdbcHelper)
        Setter for jdbcHelper.
        Parameters:
        jdbcHelper -
        Returns:
        Since:
        0.5.1.1
      • setMaxRetries

        public JdbcQueue<ID,​DATA> setMaxRetries​(int maxRetries)
        Max number of retires for DB-operations.
        Parameters:
        maxRetries -
        Returns:
      • getMaxRetries

        public int getMaxRetries()
        Max number of retires for DB-operations.
        Returns:
      • buildJdbcHelper

        protected com.github.ddth.dao.jdbc.IJdbcHelper buildJdbcHelper()
        Build an IJdbcHelper to be used by this JDBC queue.
        Returns:
        Since:
        0.6.2.6
      • peekFromQueueStorage

        protected abstract IQueueMessage<ID,​DATA> peekFromQueueStorage​(Connection conn)
        Primitive operation: Read (but not removed) message from head of queue storage.
        Parameters:
        conn -
        Returns:
      • readFromEphemeralStorage

        protected abstract IQueueMessage<ID,​DATA> readFromEphemeralStorage​(Connection conn,
                                                                                 ID id)
        Primitive operation: Read (but not removed) a message from the ephemeral storage.
        Parameters:
        conn -
        id -
        Returns:
        Since:
        1.0.0
      • getOrphanMessagesFromEphemeralStorage

        protected abstract Collection<? extends IQueueMessage<ID,​DATA>> getOrphanMessagesFromEphemeralStorage​(Connection conn,
                                                                                                                    long thresholdTimestampMs)
        Primitive operation: Get all orphan messages (messages that were left in ephemeral storage for a long time).
        Parameters:
        conn -
        thresholdTimestampMs - get all orphan messages that were queued before this timestamp
        Returns:
        Since:
        0.2.0
      • putToQueueStorage

        protected abstract boolean putToQueueStorage​(Connection conn,
                                                     IQueueMessage<ID,​DATA> msg)
        Primitive operation: Put a message to tail of the queue storage.
        Parameters:
        conn -
        msg -
        Returns:
      • putToEphemeralStorage

        protected abstract boolean putToEphemeralStorage​(Connection conn,
                                                         IQueueMessage<ID,​DATA> msg)
        Primitive operation: Put a message to the ephemeral storage.
        Parameters:
        conn -
        msg -
      • removeFromQueueStorage

        protected abstract boolean removeFromQueueStorage​(Connection conn,
                                                          IQueueMessage<ID,​DATA> msg)
        Primitive operation: Remove a message from the queue storage.
        Parameters:
        conn -
        msg -
        Returns:
      • removeFromEphemeralStorage

        protected abstract boolean removeFromEphemeralStorage​(Connection conn,
                                                              IQueueMessage<ID,​DATA> msg)
        Primitive operation: Remove a message from the queue storage.
        Parameters:
        conn -
        msg -
        Returns:
      • executeWithRetries

        protected int executeWithRetries​(int numRetries,
                                         int maxRetries,
                                         boolean transactionMode,
                                         Connection conn,
                                         String sql,
                                         Map<String,​Object> params)
        Execute a query, retry if deadlock.

        Note: http://dev.mysql.com/doc/refman/5.0/en/innodb-deadlocks.html

        InnoDB uses automatic row-level locking. You can get deadlocks even in the case of transactions that just insert or delete a single row. That is because these operations are not really "atomic"; they automatically set locks on the (possibly several) index records of the row inserted or deleted.

        Parameters:
        numRetries -
        maxRetries -
        transactionMode -
        conn -
        sql -
        params -
        Returns:
        number of affected rows
        Since:
        1.0.0
      • executeWithRetries

        protected int executeWithRetries​(int numRetries,
                                         int maxRetries,
                                         boolean transactionMode,
                                         Connection conn,
                                         String sql,
                                         Object... params)
        Execute a query, retry if deadlock.

        Note: http://dev.mysql.com/doc/refman/5.0/en/innodb-deadlocks.html

        InnoDB uses automatic row-level locking. You can get deadlocks even in the case of transactions that just insert or delete a single row. That is because these operations are not really "atomic"; they automatically set locks on the (possibly several) index records of the row inserted or deleted.

        Parameters:
        numRetries -
        maxRetries -
        transactionMode -
        conn -
        sql -
        params -
        Returns:
        number of affected rows
        Since:
        1.0.0
      • executeWithRetries

        protected <T> T executeWithRetries​(int numRetries,
                                           int maxRetries,
                                           Supplier<T> sqlRunner)
        Execute queries, retry if deadlock.

        Note: http://dev.mysql.com/doc/refman/5.0/en/innodb-deadlocks.html

        InnoDB uses automatic row-level locking. You can get deadlocks even in the case of transactions that just insert or delete a single row. That is because these operations are not really "atomic"; they automatically set locks on the (possibly several) index records of the row inserted or deleted.

        Type Parameters:
        T -
        Parameters:
        numRetries -
        maxRetries -
        sqlRunner -
        Returns:
        Since:
        1.0.0
      • _queueWithRetries

        protected boolean _queueWithRetries​(Connection conn,
                                            IQueueMessage<ID,​DATA> immutableMsg,
                                            int numRetries,
                                            int maxRetries)
        Queue a message, retry if deadlock.

        Note: http://dev.mysql.com/doc/refman/5.0/en/innodb-deadlocks.html

        InnoDB uses automatic row-level locking. You can get deadlocks even in the case of transactions that just insert or delete a single row. That is because these operations are not really "atomic"; they automatically set locks on the (possibly several) index records of the row inserted or deleted.

        Parameters:
        conn -
        immutableMsg -
        numRetries -
        maxRetries -
        Returns:
      • _requeueSilentWithRetries

        protected boolean _requeueSilentWithRetries​(Connection conn,
                                                    IQueueMessage<ID,​DATA> immutableMsg,
                                                    int numRetries,
                                                    int maxRetries)
        Re-queue (silentlt) a message, retry if deadlock.

        Note: http://dev.mysql.com/doc/refman/5.0/en/innodb-deadlocks.html

        InnoDB uses automatic row-level locking. You can get deadlocks even in the case of transactions that just insert or delete a single row. That is because these operations are not really "atomic"; they automatically set locks on the (possibly several) index records of the row inserted or deleted.

        Parameters:
        conn -
        immutableMsg -
        numRetries -
        maxRetries -
        Returns:
      • _requeueWithRetries

        protected boolean _requeueWithRetries​(Connection conn,
                                              IQueueMessage<ID,​DATA> immutableMsg,
                                              int numRetries,
                                              int maxRetries)
        Re-queue a message, retry if deadlock.

        Note: http://dev.mysql.com/doc/refman/5.0/en/innodb-deadlocks.html

        InnoDB uses automatic row-level locking. You can get deadlocks even in the case of transactions that just insert or delete a single row. That is because these operations are not really "atomic"; they automatically set locks on the (possibly several) index records of the row inserted or deleted.

        Parameters:
        conn -
        immutableMsg -
        numRetries -
        maxRetries -
        Returns:
      • _finishWithRetries

        protected void _finishWithRetries​(Connection conn,
                                          IQueueMessage<ID,​DATA> msg,
                                          int numRetries,
                                          int maxRetries)
        Perform "finish" action, retry if deadlock.

        Note: http://dev.mysql.com/doc/refman/5.0/en/innodb-deadlocks.html

        InnoDB uses automatic row-level locking. You can get deadlocks even in the case of transactions that just insert or delete a single row. That is because these operations are not really "atomic"; they automatically set locks on the (possibly several) index records of the row inserted or deleted.

        Note: the supplied queue message is mutable.

        Parameters:
        conn -
        msg -
        numRetries -
        maxRetries -
      • 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.

      • _takeWithRetries

        protected IQueueMessage<ID,​DATA> _takeWithRetries​(Connection conn,
                                                                int numRetries,
                                                                int maxRetries)
        Take a message from queue, retry if deadlock.

        Note: http://dev.mysql.com/doc/refman/5.0/en/innodb-deadlocks.html

        InnoDB uses automatic row-level locking. You can get deadlocks even in the case of transactions that just insert or delete a single row. That is because these operations are not really "atomic"; they automatically set locks on the (possibly several) index records of the row inserted or deleted.

        Parameters:
        conn -
        numRetries -
        maxRetries -
        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
      • _getOrphanMessagesWithRetries

        protected Collection<? extends IQueueMessage<ID,​DATA>> _getOrphanMessagesWithRetries​(long thresholdTimestampMs,
                                                                                                   Connection conn,
                                                                                                   int numRetries,
                                                                                                   int maxRetries)
        Get all orphan messages (messages that were left in ephemeral storage for a long time), retry if deadlock.

        Note: http://dev.mysql.com/doc/refman/5.0/en/innodb-deadlocks.html

        InnoDB uses automatic row-level locking. You can get deadlocks even in the case of transactions that just insert or delete a single row. That is because these operations are not really "atomic"; they automatically set locks on the (possibly several) index records of the row inserted or deleted.

        Parameters:
        thresholdTimestampMs -
        conn -
        numRetries -
        maxRetries -
        Returns:
        Since:
        0.2.0
      • 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 if message's timestampMillis + thresholdTimestampMs < now . Which means getOrphanMessages(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
      • doSize

        protected int doSize​(Connection conn,
                             String sql,
                             String field,
                             Object... params)
        Parameters:
        conn -
        sql -
        field -
        params -
        Returns:
        Since:
        1.0.0
      • queueSize

        protected int queueSize​(Connection conn)
        Get number of items currently in queue storage.
        Parameters:
        conn -
        Returns:
        Since:
        0.5.0
      • ephemeralSize

        protected int ephemeralSize​(Connection conn)
        Get number of items currently in ephemeral storage.
        Parameters:
        conn -
        Returns:
        Since:
        0.5.0
      • 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