use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class QueueContainer method drain.
/**
* Removes items from the queue and the queue store (if configured), up to {@code maxSize} or the size of the queue,
* whichever is smaller. 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 maxSize the maximum number of items to be removed
* @return the map of IDs and removed (drained) items
*/
public Map<Long, Data> drain(int maxSize) {
int maxSizeParam = maxSize;
if (maxSizeParam < 0 || maxSizeParam > getItemQueue().size()) {
maxSizeParam = getItemQueue().size();
}
final LinkedHashMap<Long, Data> map = new LinkedHashMap<Long, Data>(maxSizeParam);
mapDrainIterator(maxSizeParam, map);
if (store.isEnabled() && maxSizeParam != 0) {
try {
store.deleteAll(map.keySet());
} catch (Exception e) {
throw new HazelcastException(e);
}
}
long current = Clock.currentTimeMillis();
for (int i = 0; i < maxSizeParam; i++) {
final QueueItem item = getItemQueue().poll();
//For Stats
age(item, current);
}
if (maxSizeParam != 0) {
scheduleEvictionIfEmpty();
}
return map;
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class QueueContainer method remove.
/**
* iterates all items, checks equality with data
* This method does not trigger store load.
*/
public long remove(Data data) {
Iterator<QueueItem> iter = getItemQueue().iterator();
while (iter.hasNext()) {
QueueItem item = iter.next();
if (data.equals(item.getData())) {
if (store.isEnabled()) {
try {
store.delete(item.getItemId());
} catch (Exception e) {
throw new HazelcastException(e);
}
}
iter.remove();
//For Stats
age(item, Clock.currentTimeMillis());
scheduleEvictionIfEmpty();
return item.getItemId();
}
}
return -1;
}
use of com.hazelcast.core.HazelcastException 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;
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class XmlConfigBuilder method attachChildConfig.
private static void attachChildConfig(Object parent, Object child) throws Exception {
String targetName = child.getClass().getSimpleName();
Method attacher = getMethod(parent, "set" + targetName, false);
if (attacher == null) {
attacher = getMethod(parent, "add" + targetName, false);
}
if (attacher == null) {
throw new HazelcastException(String.format("%s doesn't accept %s as child", parent.getClass().getSimpleName(), targetName));
}
attacher.invoke(parent, child);
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class XmlConfigBuilder method invokeSetter.
private static void invokeSetter(Object target, Node node, String argument) {
Method method = getMethod(target, "set" + toPropertyName(cleanNodeName(node)), true);
if (method == null) {
throw new InvalidConfigurationException("Invalid element/attribute name in XML configuration: " + node);
}
Class<?> arg = method.getParameterTypes()[0];
Object coercedArg = arg == String.class ? argument : arg == int.class ? Integer.valueOf(argument) : arg == long.class ? Long.valueOf(argument) : arg == boolean.class ? getBooleanValue(argument) : null;
if (coercedArg == null) {
throw new HazelcastException(String.format("Method %s has unsupported argument type %s", method.getName(), arg.getSimpleName()));
}
try {
method.invoke(target, coercedArg);
} catch (Exception e) {
throw new HazelcastException(e);
}
}
Aggregations