/** {@inheritDoc} */ @Override public boolean bounded() { return delegate.bounded(); }
/** {@inheritDoc} */ @Override public int remainingCapacity() { if (!bounded()) return Integer.MAX_VALUE; int remaining = cap - size(); return remaining > 0 ? remaining : 0; }
/** {@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; } }
/** * @param hdr Queue header. */ public void onHeaderChanged(GridCacheQueueHeader hdr) { if (!hdr.empty()) { readSem.drainPermits(); readSem.release(hdr.size()); } if (bounded()) { writeSem.drainPermits(); if (!hdr.full()) writeSem.release(hdr.capacity() - hdr.size()); } }
/** {@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; }
/** * @param queueName Queue name. * @param hdr Queue hdr. * @param cctx Cache context. */ protected GridCacheQueueAdapter(String queueName, GridCacheQueueHeader hdr, GridCacheContext<?, ?> cctx) { this.cctx = cctx; this.queueName = queueName; id = hdr.id(); cap = hdr.capacity(); collocated = hdr.collocated(); queueKey = new GridCacheQueueHeaderKey(queueName); cache = cctx.kernalContext().cache().internalCache(cctx.name()); this.compute = cctx.kernalContext().grid().compute(); log = cctx.logger(getClass()); readSem = new Semaphore(hdr.size(), true); writeSem = bounded() ? new Semaphore(hdr.capacity() - hdr.size(), true) : null; }
/** {@inheritDoc} */ @Override public boolean bounded() { return delegate.bounded(); }
/** {@inheritDoc} */ @Override public int remainingCapacity() { if (!bounded()) return Integer.MAX_VALUE; int remaining = cap - size(); return remaining > 0 ? remaining : 0; }
/** {@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; } }
/** * @param hdr Queue header. */ public void onHeaderChanged(GridCacheQueueHeader hdr) { if (!hdr.empty()) { readSem.drainPermits(); readSem.release(hdr.size()); } if (bounded()) { writeSem.drainPermits(); if (!hdr.full()) writeSem.release(hdr.capacity() - hdr.size()); } }
/** {@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; }
/** * @param queueName Queue name. * @param hdr Queue hdr. * @param cctx Cache context. */ @SuppressWarnings("unchecked") protected GridCacheQueueAdapter(String queueName, GridCacheQueueHeader hdr, GridCacheContext<?, ?> cctx) { this.cctx = cctx; this.queueName = queueName; id = hdr.id(); cap = hdr.capacity(); collocated = hdr.collocated(); queueKey = new GridCacheQueueHeaderKey(queueName); cache = cctx.kernalContext().cache().internalCache(cctx.name()); this.compute = cctx.kernalContext().grid().compute(); log = cctx.logger(getClass()); readSem = new Semaphore(hdr.size(), true); writeSem = bounded() ? new Semaphore(hdr.capacity() - hdr.size(), true) : null; }