use of com.hazelcast.core.HazelcastException 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;
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class QueueContainer method mapIterateAndRemove.
/**
* Deletes items from the queue which have IDs contained in the key set of the given map. Also schedules the queue for
* destruction if it is empty or destroys it immediately if it is empty and {@link QueueConfig#getEmptyQueueTtl()} is 0.
*
* @param map the map of items which to be removed.
*/
public void mapIterateAndRemove(Map<Long, Data> map) {
if (map.size() <= 0) {
return;
}
if (store.isEnabled()) {
try {
store.deleteAll(map.keySet());
} catch (Exception e) {
throw new HazelcastException(e);
}
}
final Iterator<QueueItem> iter = getItemQueue().iterator();
while (iter.hasNext()) {
final QueueItem item = iter.next();
if (map.containsKey(item.getItemId())) {
iter.remove();
//For Stats
age(item, Clock.currentTimeMillis());
}
}
scheduleEvictionIfEmpty();
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class QueueContainer method offer.
//TX Methods Ends
public long offer(Data data) {
QueueItem item = new QueueItem(this, nextId(), null);
if (store.isEnabled()) {
try {
store.store(item.getItemId(), data);
} catch (Exception e) {
throw new HazelcastException(e);
}
}
if (!store.isEnabled() || store.getMemoryLimit() > getItemQueue().size()) {
item.setData(data);
}
getItemQueue().offer(item);
cancelEvictionIfExists();
return item.getItemId();
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class QueueContainer method clear.
public Map<Long, Data> clear() {
long current = Clock.currentTimeMillis();
LinkedHashMap<Long, Data> map = new LinkedHashMap<Long, Data>(getItemQueue().size());
for (QueueItem item : getItemQueue()) {
map.put(item.getItemId(), item.getData());
// For stats
age(item, current);
}
if (store.isEnabled() && !map.isEmpty()) {
try {
store.deleteAll(map.keySet());
} catch (Exception e) {
throw new HazelcastException(e);
}
}
getItemQueue().clear();
dataMap.clear();
scheduleEvictionIfEmpty();
return map;
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class QueueContainer method poll.
/**
* Retrieves and removes the head of the queue (in other words, the first item), or returns {@code null} if it is empty.
* Also calls the queue store for item deletion by item ID.
*
* @return the first item in the queue
*/
public QueueItem poll() {
final QueueItem item = peek();
if (item == null) {
return null;
}
if (store.isEnabled()) {
try {
store.delete(item.getItemId());
} catch (Exception e) {
throw new HazelcastException(e);
}
}
getItemQueue().poll();
age(item, Clock.currentTimeMillis());
scheduleEvictionIfEmpty();
return item;
}
Aggregations