/** {@inheritDoc} */ @Override public boolean add(final T item) { gate.enter(); try { return delegate.add(item); } finally { gate.leave(); } }
/** {@inheritDoc} */ @Override public boolean addAll(final Collection<? extends T> items) { gate.enter(); try { return delegate.addAll(items); } finally { gate.leave(); } }
/** {@inheritDoc} */ @Override public <R> R affinityCall(final IgniteCallable<R> job) { return delegate.affinityCall(job); }
/** {@inheritDoc} */ @Nullable @Override public T take() throws IgniteException { while (true) { try { readSem.acquire(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IgniteInterruptedException("Queue take interrupted.", e); } checkStopping(); T e = poll(); if (e != null) return e; } }
/** {@inheritDoc} */ @Override public void put(T item) throws IgniteException { A.notNull(item, "item"); if (!bounded()) { boolean offer = offer(item); assert offer; return; } while (true) { try { writeSem.acquire(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IgniteInterruptedException("Queue put interrupted.", e); } checkStopping(); if (offer(item)) return; } }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void clear(int batchSize) throws IgniteException { A.ensure(batchSize >= 0, "Batch size cannot be negative: " + batchSize); try { Object obj = cache.invoke(queueKey, new ClearProcessor(id)).get(); if (obj == null) return; IgniteBiTuple<Long, Long> t = obj instanceof BinaryObject ? ((BinaryObject)obj).deserialize() : (IgniteBiTuple<Long, Long>)obj; checkRemoved(t.get1()); removeKeys(cache, id, queueName, collocated, t.get1(), t.get2(), batchSize); } catch (IgniteCheckedException e) { throw U.convertException(e); } }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Nullable @Override public T peek() throws IgniteException { try { while (true) { GridCacheQueueHeader hdr = (GridCacheQueueHeader)cache.get(queueKey); checkRemoved(hdr); if (hdr.empty()) return null; T val = (T)cache.get(itemKey(hdr.head())); if (val == null) // Header might have been polled. Retry. continue; return val; } } catch (IgniteCheckedException e) { throw U.convertException(e); } }
/** {@inheritDoc} */ @Override public void clear() { clear(DFLT_CLEAR_BATCH_SIZE); }
/** {@inheritDoc} */ @Override public boolean bounded() { return delegate.bounded(); }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public int size() { try { GridCacheQueueHeader hdr = (GridCacheQueueHeader)cache.get(queueKey); checkRemoved(hdr); return hdr.size(); } catch (IgniteCheckedException e) { throw U.convertException(e); } }
/** {@inheritDoc} */ @Override public void close() { gate.enter(); try { delegate.close(); } finally { gate.leave(); } }
/** {@inheritDoc} */ @Override public void affinityRun(final IgniteRunnable job) { delegate.affinityRun(job); }
/** {@inheritDoc} */ @Override public int capacity() { return delegate.capacity(); }
/** {@inheritDoc} */ @Override public boolean offer(T item, long timeout, TimeUnit unit) throws IgniteException { A.notNull(item, "item"); A.ensure(timeout >= 0, "Timeout cannot be negative: " + timeout); if (!bounded()) { boolean offer = offer(item); assert offer; return true; } long end = U.currentTimeMillis() + MILLISECONDS.convert(timeout, unit); while (U.currentTimeMillis() < end) { boolean retVal = false; try { if (writeSem.tryAcquire(end - U.currentTimeMillis(), MILLISECONDS)) { checkStopping(); retVal = offer(item); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IgniteInterruptedException("Queue put interrupted.", e); } if (retVal) return true; } return false; }
/** {@inheritDoc} */ @Nullable @Override public T poll(long timeout, TimeUnit unit) throws IgniteException { A.ensure(timeout >= 0, "Timeout cannot be negative: " + timeout); long end = U.currentTimeMillis() + MILLISECONDS.convert(timeout, unit); while (U.currentTimeMillis() < end) { T retVal = null; try { if (readSem.tryAcquire(end - U.currentTimeMillis(), MILLISECONDS)) { checkStopping(); retVal = poll(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IgniteInterruptedException("Queue poll interrupted.", e); } if (retVal != null) return retVal; } return null; }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public void clear(int batchSize) throws IgniteException { A.ensure(batchSize >= 0, "Batch size cannot be negative: " + batchSize); try { IgniteBiTuple<Long, Long> t = (IgniteBiTuple<Long, Long>)cache.invoke(queueKey, new ClearProcessor(id)).get(); if (t == null) return; checkRemoved(t.get1()); removeKeys(cache, id, queueName, collocated, t.get1(), t.get2(), batchSize); } catch (IgniteCheckedException e) { throw U.convertException(e); } }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Nullable @Override public T peek() throws IgniteException { try { while (true) { GridCacheQueueHeader hdr = (GridCacheQueueHeader)cache.get(queueKey); checkRemoved(hdr); if (hdr.empty()) return null; T val = (T)cache.get(itemKey(hdr.head())); if (val == null) // Header might have been polled. Retry. continue; return val; } } catch (IgniteCheckedException e) { throw U.convertException(e); } }
/** {@inheritDoc} */ @Override public void clear() { gate.enter(); try { delegate.clear(); } finally { gate.leave(); } }