Search in sources :

Example 1 with TxQueueItem

use of com.hazelcast.collection.impl.txnqueue.TxQueueItem in project hazelcast by hazelcast.

the class QueueContainer method txnPeek.

/**
     * Retrieves, but does not remove, the head of the queue. If the queue is empty checks if there is a reserved item with
     * the associated {@code offerId} and returns it.
     * If the item was retrieved from the queue but does not contain any data and the queue store is enabled, this method will
     * try load the data from the data store.
     *
     * @param offerId       the ID of the reserved item to be returned if the queue is empty
     * @param transactionId currently ignored
     * @return the head of the queue or a reserved item associated with the {@code offerId} if the queue is empty
     * @throws HazelcastException if there is an exception while loading the data from the queue store
     */
public QueueItem txnPeek(long offerId, String transactionId) {
    QueueItem item = getItemQueue().peek();
    if (item == null) {
        if (offerId == -1) {
            return null;
        }
        TxQueueItem txItem = txMap.get(offerId);
        if (txItem == null) {
            return null;
        }
        item = new QueueItem(this, txItem.getItemId(), txItem.getData());
        return item;
    }
    if (store.isEnabled() && item.getData() == null) {
        try {
            load(item);
        } catch (Exception e) {
            throw new HazelcastException(e);
        }
    }
    return item;
}
Also used : HazelcastException(com.hazelcast.core.HazelcastException) TxQueueItem(com.hazelcast.collection.impl.txnqueue.TxQueueItem) TxQueueItem(com.hazelcast.collection.impl.txnqueue.TxQueueItem) TransactionException(com.hazelcast.transaction.TransactionException) HazelcastException(com.hazelcast.core.HazelcastException) IOException(java.io.IOException)

Example 2 with TxQueueItem

use of com.hazelcast.collection.impl.txnqueue.TxQueueItem in project hazelcast by hazelcast.

the class QueueContainer method txnOfferReserve.

//TX Offer
/**
     * Reserves an ID for a future queue item and associates it with the given {@code transactionId}.
     * The item is not yet visible in the queue, it is just reserved for future insertion.
     *
     * @param transactionId the ID of the transaction offering this item
     * @return the ID of the reserved item
     */
public long txnOfferReserve(String transactionId) {
    TxQueueItem item = new TxQueueItem(this, nextId(), null).setTransactionId(transactionId).setPollOperation(false);
    txMap.put(item.getItemId(), item);
    return item.getItemId();
}
Also used : TxQueueItem(com.hazelcast.collection.impl.txnqueue.TxQueueItem)

Example 3 with TxQueueItem

use of com.hazelcast.collection.impl.txnqueue.TxQueueItem in project hazelcast by hazelcast.

the class QueueContainer method txnPollBackupReserve.

public void txnPollBackupReserve(long itemId, String transactionId) {
    QueueItem item = getBackupMap().remove(itemId);
    if (item == null) {
        logger.warning("Backup reserve failed, itemId: " + itemId + " is not found");
        return;
    }
    txMap.put(itemId, new TxQueueItem(item).setPollOperation(true).setTransactionId(transactionId));
}
Also used : TxQueueItem(com.hazelcast.collection.impl.txnqueue.TxQueueItem) TxQueueItem(com.hazelcast.collection.impl.txnqueue.TxQueueItem)

Example 4 with TxQueueItem

use of com.hazelcast.collection.impl.txnqueue.TxQueueItem in project hazelcast by hazelcast.

the class QueueContainer method txnPollReserve.

//TX Poll
/**
     * Retrieves and removes the head of the queue and loads the data from the queue store if the data is not stored in-memory
     * and the queue store is configured and enabled. If the queue is empty returns an item which was previously reserved with
     * the {@code reservedOfferId} by invoking {@code {@link #txnOfferReserve(String)}}.
     *
     * @param reservedOfferId the ID of the reserved item to be returned if the queue is empty
     * @param transactionId   the transaction ID for which this poll is invoked
     * @return the head of the queue or a reserved item with the {@code reservedOfferId} if there is any
     */
public QueueItem txnPollReserve(long reservedOfferId, String transactionId) {
    QueueItem item = getItemQueue().peek();
    if (item == null) {
        TxQueueItem txItem = txMap.remove(reservedOfferId);
        if (txItem == null) {
            return null;
        }
        item = new QueueItem(this, txItem.getItemId(), txItem.getData());
        return item;
    }
    if (store.isEnabled() && item.getData() == null) {
        try {
            load(item);
        } catch (Exception e) {
            throw new HazelcastException(e);
        }
    }
    getItemQueue().poll();
    txMap.put(item.getItemId(), new TxQueueItem(item).setPollOperation(true).setTransactionId(transactionId));
    return item;
}
Also used : HazelcastException(com.hazelcast.core.HazelcastException) TxQueueItem(com.hazelcast.collection.impl.txnqueue.TxQueueItem) TxQueueItem(com.hazelcast.collection.impl.txnqueue.TxQueueItem) TransactionException(com.hazelcast.transaction.TransactionException) HazelcastException(com.hazelcast.core.HazelcastException) IOException(java.io.IOException)

Example 5 with TxQueueItem

use of com.hazelcast.collection.impl.txnqueue.TxQueueItem in project hazelcast by hazelcast.

the class QueueContainer method addTxItemOrdered.

private void addTxItemOrdered(TxQueueItem txQueueItem) {
    if (isPriorityQueue) {
        getItemQueue().add(txQueueItem);
    } else {
        ListIterator<QueueItem> iterator = ((List<QueueItem>) getItemQueue()).listIterator();
        while (iterator.hasNext()) {
            QueueItem queueItem = iterator.next();
            if (txQueueItem.itemId < queueItem.itemId) {
                iterator.previous();
                break;
            }
        }
        iterator.add(txQueueItem);
    }
}
Also used : ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) TxQueueItem(com.hazelcast.collection.impl.txnqueue.TxQueueItem)

Aggregations

TxQueueItem (com.hazelcast.collection.impl.txnqueue.TxQueueItem)14 HazelcastException (com.hazelcast.core.HazelcastException)4 TransactionException (com.hazelcast.transaction.TransactionException)4 IOException (java.io.IOException)4 AddAllBackupOperation (com.hazelcast.collection.impl.queue.operations.AddAllBackupOperation)1 AddAllOperation (com.hazelcast.collection.impl.queue.operations.AddAllOperation)1 CheckAndEvictOperation (com.hazelcast.collection.impl.queue.operations.CheckAndEvictOperation)1 ClearBackupOperation (com.hazelcast.collection.impl.queue.operations.ClearBackupOperation)1 ClearOperation (com.hazelcast.collection.impl.queue.operations.ClearOperation)1 CompareAndRemoveBackupOperation (com.hazelcast.collection.impl.queue.operations.CompareAndRemoveBackupOperation)1 CompareAndRemoveOperation (com.hazelcast.collection.impl.queue.operations.CompareAndRemoveOperation)1 ContainsOperation (com.hazelcast.collection.impl.queue.operations.ContainsOperation)1 DrainBackupOperation (com.hazelcast.collection.impl.queue.operations.DrainBackupOperation)1 DrainOperation (com.hazelcast.collection.impl.queue.operations.DrainOperation)1 IsEmptyOperation (com.hazelcast.collection.impl.queue.operations.IsEmptyOperation)1 IteratorOperation (com.hazelcast.collection.impl.queue.operations.IteratorOperation)1 OfferBackupOperation (com.hazelcast.collection.impl.queue.operations.OfferBackupOperation)1 OfferOperation (com.hazelcast.collection.impl.queue.operations.OfferOperation)1 PeekOperation (com.hazelcast.collection.impl.queue.operations.PeekOperation)1 PollBackupOperation (com.hazelcast.collection.impl.queue.operations.PollBackupOperation)1