Java Code Examples for java.util.concurrent.BlockingQueue#drainTo()

The following examples show how to use java.util.concurrent.BlockingQueue#drainTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DrainToFails.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void testDelayQueue(final BlockingQueue q) throws Throwable {
    System.err.println(q.getClass().getSimpleName());
    for (int i = 0; i < CAPACITY; i++)
        q.add(new PDelay(i));
    ArrayBlockingQueue q2 = new ArrayBlockingQueue(SMALL);
    try {
        q.drainTo(q2, SMALL + 3);
        fail("should throw");
    } catch (IllegalStateException success) {
        equal(SMALL, q2.size());
        equal(new PDelay(0), q2.poll());
        equal(new PDelay(1), q2.poll());
        check(q2.isEmpty());
        for (int i = SMALL; i < CAPACITY; i++)
            equal(new PDelay(i), q.poll());
        equal(0, q.size());
    }
}
 
Example 2
Source File: SubscriptionWorker.java    From sourcerer with MIT License 6 votes vote down vote up
private List<Update<T>> getUpdateBatch(
        final BlockingQueue<Update<T>> updatesQueue) throws InterruptedException {
    Update<T> update = updatesQueue.poll(1000, TimeUnit.MILLISECONDS);
    if (update != null) {
        // We have at least one pending update, check if there's more!
        List<Update<T>> updatesBatch = new ArrayList<>();
        updatesBatch.add(update);
        if (updatesQueue.peek() != null) {
            logger.debug("Subscription received update, queue not empty, draining ...");
            updatesQueue.drainTo(updatesBatch);
        } else {
            logger.debug("Subscription received single update");
        }
        return updatesBatch;
    } else {
        // Nothing pending, nothing to see here
        logger.trace("No update (yet)");
        return null;
    }
}
 
Example 3
Source File: CoalescingStrategies.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Override
protected <C extends Coalescable> void coalesceInternal(BlockingQueue<C> input, List<C> out,  int maxItems) throws InterruptedException
{
    if (input.drainTo(out, maxItems) == 0)
    {
        out.add(input.take());
    }

    long average = notifyOfSample(out.get(0).timestampNanos());

    debugGap(average);

    maybeSleep(out.size(), average, maxCoalesceWindow, parker);

    input.drainTo(out, maxItems - out.size());
    for (int ii = 1; ii < out.size(); ii++)
        notifyOfSample(out.get(ii).timestampNanos());
}
 
Example 4
Source File: ItemManager.java    From Kepler with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Handle bulk item deletion.
 *
 * @param itemDeletionQueue the queue that's used for deleting items
 */
public void performItemDeletion(BlockingQueue<Integer> itemDeletionQueue) {
    try {
        if (itemDeletionQueue.isEmpty()) {
            return;
        }

        List<Integer> itemList = new ArrayList<>();
        itemDeletionQueue.drainTo(itemList);

        if (itemList.size() > 0) {
            ItemDao.deleteItems(itemList);
        }
    } catch (Exception ex) {
        Log.getErrorLogger().error("Error when attempting to save items: ", ex);
    }
}
 
Example 5
Source File: BoundedSourceSystem.java    From beam with Apache License 2.0 6 votes vote down vote up
private List<IncomingMessageEnvelope> getNextMessages(
    SystemStreamPartition ssp, long timeoutMillis) throws InterruptedException {
  if (lastException != null) {
    throw new RuntimeException(lastException);
  }

  final List<IncomingMessageEnvelope> envelopes = new ArrayList<>();
  final BlockingQueue<IncomingMessageEnvelope> queue = queues.get(ssp);
  final IncomingMessageEnvelope envelope = queue.poll(timeoutMillis, TimeUnit.MILLISECONDS);

  if (envelope != null) {
    envelopes.add(envelope);
    queue.drainTo(envelopes);
  }

  available.release(envelopes.size());

  if (lastException != null) {
    throw new RuntimeException(lastException);
  }

  return envelopes;
}
 
Example 6
Source File: FairCallQueue.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * drainTo defers to each sub-queue. Note that draining from a FairCallQueue
 * to another FairCallQueue will likely fail, since the incoming calls
 * may be scheduled differently in the new FairCallQueue. Nonetheless this
 * method is provided for completeness.
 */
@Override
public int drainTo(Collection<? super E> c, int maxElements) {
  int sum = 0;
  for (BlockingQueue<E> q : this.queues) {
    sum += q.drainTo(c, maxElements);
  }
  return sum;
}
 
Example 7
Source File: WriteAheadFlowFileRepository.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void onSync(final int partitionIndex) {
    final BlockingQueue<ResourceClaim> claimQueue = claimsAwaitingDestruction.get(Integer.valueOf(partitionIndex));
    if (claimQueue == null) {
        return;
    }

    final Set<ResourceClaim> claimsToDestroy = new HashSet<>();
    claimQueue.drainTo(claimsToDestroy);

    for (final ResourceClaim claim : claimsToDestroy) {
        markDestructable(claim);
    }
}
 
Example 8
Source File: DefaultOfflineMessageStore.java    From jmqtt with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<Message> getAllOfflineMessage(String clientId) {
    BlockingQueue<Message> queue = offlineTable.get(clientId);
    Collection<Message> allMessages = new ArrayList<>();
    int rs = queue.drainTo(allMessages);
    return allMessages;
}
 
Example 9
Source File: CouchbaseStreamingConnection.java    From components with Apache License 2.0 5 votes vote down vote up
public void stopStreaming() {
    if (resultsQueue != null) {
        client.stopStreaming(partitionsToStream()).await();
        BlockingQueue<ByteBuf> queue = resultsQueue;
        resultsQueue = null;
        List<ByteBuf> drained = new ArrayList<ByteBuf>();
        queue.drainTo(drained);
        for (ByteBuf byteBuf : drained) {
            byteBuf.release();
        }
        client.disconnect();
    }
}
 
Example 10
Source File: Queues.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Drains the queue as {@link BlockingQueue#drainTo(Collection, int)}, but if the requested
 * {@code numElements} elements are not available, it will wait for them up to the specified
 * timeout.
 *
 * @param q the blocking queue to be drained
 * @param buffer where to add the transferred elements
 * @param numElements the number of elements to be waited for
 * @param timeout how long to wait before giving up, in units of {@code unit}
 * @param unit a {@code TimeUnit} determining how to interpret the timeout parameter
 * @return the number of elements transferred
 * @throws InterruptedException if interrupted while waiting
 */

@Beta
@CanIgnoreReturnValue
public static <E> int drain(
  BlockingQueue<E> q,
  Collection<? super E> buffer,
  int numElements,
  long timeout,
  TimeUnit unit)
    throws InterruptedException {
  Preconditions.checkNotNull(buffer);
  /*
   * This code performs one System.nanoTime() more than necessary, and in return, the time to
   * execute Queue#drainTo is not added *on top* of waiting for the timeout (which could make
   * the timeout arbitrarily inaccurate, given a queue that is slow to drain).
   */
  long deadline = System.nanoTime() + unit.toNanos(timeout);
  int added = 0;
  while (added < numElements) {
    // we could rely solely on #poll, but #drainTo might be more efficient when there are multiple
    // elements already available (e.g. LinkedBlockingQueue#drainTo locks only once)
    added += q.drainTo(buffer, numElements - added);
    if (added < numElements) { // not enough elements immediately available; will have to poll
      E e = q.poll(deadline - System.nanoTime(), TimeUnit.NANOSECONDS);
      if (e == null) {
        break; // we already waited enough, and there are no more elements in sight
      }
      buffer.add(e);
      added++;
    }
  }
  return added;
}
 
Example 11
Source File: Queues.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Drains the queue as {@link BlockingQueue#drainTo(Collection, int)}, but if the requested
 * {@code numElements} elements are not available, it will wait for them up to the specified
 * timeout.
 *
 * @param q the blocking queue to be drained
 * @param buffer where to add the transferred elements
 * @param numElements the number of elements to be waited for
 * @param timeout how long to wait before giving up, in units of {@code unit}
 * @param unit a {@code TimeUnit} determining how to interpret the timeout parameter
 * @return the number of elements transferred
 * @throws InterruptedException if interrupted while waiting
 */
@Beta
@CanIgnoreReturnValue
public static <E> int drain(
    BlockingQueue<E> q,
    Collection<? super E> buffer,
    int numElements,
    long timeout,
    TimeUnit unit)
    throws InterruptedException {
  Preconditions.checkNotNull(buffer);
  /*
   * This code performs one System.nanoTime() more than necessary, and in return, the time to
   * execute Queue#drainTo is not added *on top* of waiting for the timeout (which could make
   * the timeout arbitrarily inaccurate, given a queue that is slow to drain).
   */
  long deadline = System.nanoTime() + unit.toNanos(timeout);
  int added = 0;
  while (added < numElements) {
    // we could rely solely on #poll, but #drainTo might be more efficient when there are multiple
    // elements already available (e.g. LinkedBlockingQueue#drainTo locks only once)
    added += q.drainTo(buffer, numElements - added);
    if (added < numElements) { // not enough elements immediately available; will have to poll
      E e = q.poll(deadline - System.nanoTime(), TimeUnit.NANOSECONDS);
      if (e == null) {
        break; // we already waited enough, and there are no more elements in sight
      }
      buffer.add(e);
      added++;
    }
  }
  return added;
}
 
Example 12
Source File: Queues.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Drains the queue as {@link BlockingQueue#drainTo(Collection, int)}, but if the requested
 * {@code numElements} elements are not available, it will wait for them up to the specified
 * timeout.
 *
 * @param q the blocking queue to be drained
 * @param buffer where to add the transferred elements
 * @param numElements the number of elements to be waited for
 * @param timeout how long to wait before giving up, in units of {@code unit}
 * @param unit a {@code TimeUnit} determining how to interpret the timeout parameter
 * @return the number of elements transferred
 * @throws InterruptedException if interrupted while waiting
 */

@Beta
@CanIgnoreReturnValue
public static <E> int drain(
  BlockingQueue<E> q,
  Collection<? super E> buffer,
  int numElements,
  long timeout,
  TimeUnit unit)
    throws InterruptedException {
  Preconditions.checkNotNull(buffer);
  /*
   * This code performs one System.nanoTime() more than necessary, and in return, the time to
   * execute Queue#drainTo is not added *on top* of waiting for the timeout (which could make
   * the timeout arbitrarily inaccurate, given a queue that is slow to drain).
   */
  long deadline = System.nanoTime() + unit.toNanos(timeout);
  int added = 0;
  while (added < numElements) {
    // we could rely solely on #poll, but #drainTo might be more efficient when there are multiple
    // elements already available (e.g. LinkedBlockingQueue#drainTo locks only once)
    added += q.drainTo(buffer, numElements - added);
    if (added < numElements) { // not enough elements immediately available; will have to poll
      E e = q.poll(deadline - System.nanoTime(), TimeUnit.NANOSECONDS);
      if (e == null) {
        break; // we already waited enough, and there are no more elements in sight
      }
      buffer.add(e);
      added++;
    }
  }
  return added;
}
 
Example 13
Source File: BlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * drainTo(this, n) throws IllegalArgumentException
 */
public void testDrainToSelfN() {
    final BlockingQueue q = emptyCollection();
    try {
        q.drainTo(q, 0);
        shouldThrow();
    } catch (IllegalArgumentException success) {}
}
 
Example 14
Source File: DefaultOfflineMessageStore.java    From iot-mqtt with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<Message> getAllOfflineMessage(String clientId) {
    BlockingQueue<Message> queue = offlineTable.get(clientId);
    Collection<Message> allMessages = new ArrayList<>();
    queue.drainTo(allMessages);
    return allMessages;
}
 
Example 15
Source File: FairCallQueue.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public int drainTo(Collection<? super E> c) {
  int sum = 0;
  for (BlockingQueue<E> q : this.queues) {
    sum += q.drainTo(c);
  }
  return sum;
}
 
Example 16
Source File: ExceedWait.java    From ThreadDebugger with Apache License 2.0 5 votes vote down vote up
public List<Runnable> drainExceedQueue() {
    BlockingQueue<Runnable> q = mExceedQueue;
    ArrayList<Runnable> taskList = new ArrayList<>();
    q.drainTo(taskList);
    if (!q.isEmpty()) {
        for (Runnable r : q.toArray(new Runnable[0])) {
            if (q.remove(r))
                taskList.add(r);
        }
    }
    return taskList;
}
 
Example 17
Source File: DelayedConfigResponses.java    From vespa with Apache License 2.0 5 votes vote down vote up
/**
 * Drains delayed responses queue and returns responses in an array
 *
 * @return and array of DelayedConfigResponse objects
 */
List<DelayedConfigResponse> drainQueue(ApplicationId app) {
    ArrayList<DelayedConfigResponse> ret = new ArrayList<>();
    
    if (delayedResponses.containsKey(app)) {
        BlockingQueue<DelayedConfigResponse> queue = delayedResponses.get(app);
        queue.drainTo(ret);
    }
    metrics.remove(app);
    return ret;
}
 
Example 18
Source File: TwitterStream.java    From Java-for-Data-Science with MIT License 4 votes vote down vote up
public Stream<TweetHandler> stream() {
        String myKey = "sl2WbCf4UnIr08xvHVitHJ99r";
        String mySecret = "PE6yauvXjKLuvoQNXZAJo5C8N5U5piSFb3udwkoI76paK6KyqI";
        String myToken = "1098376471-p6iWfxCLtyMvMutTb010w1D1xZ3UyJhcC2kkBjN";
        String myAccess = "2o1uGcp4b2bFynOfu2cA1uz63n5aruV0RwNsUjRpjDBZS";

        out.println("Creating Twitter Stream");
        BlockingQueue<String> statusQueue = new LinkedBlockingQueue<>(1000);
        StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
        endpoint.trackTerms(Lists.newArrayList("twitterapi", this.topic));
        endpoint.stallWarnings(false);
        Authentication twitterAuth = new OAuth1(myKey, mySecret, myToken, myAccess);

        BasicClient twitterClient = new ClientBuilder()
                .name("Twitter client")
                .hosts(Constants.STREAM_HOST)
                .endpoint(endpoint)
                .authentication(twitterAuth)
                .processor(new StringDelimitedProcessor(statusQueue))
                .build();

        twitterClient.connect();

        List<TweetHandler> list = new ArrayList();
        List<String> twitterList = new ArrayList();

        statusQueue.drainTo(twitterList);
        for(int i=0; i<numberOfTweets; i++) {
            String message;
            try {
                message = statusQueue.take();
                list.add(new TweetHandler(message));
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }

//        for (int msgRead = 0; msgRead < this.numberOfTweets; msgRead++) {
//            try {
//                if (twitterClient.isDone()) {
//                  //  out.println(twitterClient.getExitEvent().getMessage());
//                    break;
//                }
//
//                String msg = statusQueue.poll(10, TimeUnit.SECONDS);
//                if (msg == null) {
//                    out.println("Waited 10 seconds - no message received");
//                } else {
//                    list.add(new TweetHandler(msg));
//                    out.println("Added message: " + msg.length());
//                }
//            } catch (InterruptedException ex) {
//                ex.printStackTrace();
//            }
//        }
        twitterClient.stop();
        out.printf("%d messages processed!\n", twitterClient.getStatsTracker().getNumMessages());

        return list.stream();
    }
 
Example 19
Source File: Queues.java    From codebuff with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Drains the queue as {@linkplain #drain(BlockingQueue, Collection, int, long, TimeUnit)},
 * but with a different behavior in case it is interrupted while waiting. In that case, the
 * operation will continue as usual, and in the end the thread's interruption status will be set
 * (no {@code InterruptedException} is thrown).
 *
 * @param q the blocking queue to be drained
 * @param buffer where to add the transferred elements
 * @param numElements the number of elements to be waited for
 * @param timeout how long to wait before giving up, in units of {@code unit}
 * @param unit a {@code TimeUnit} determining how to interpret the timeout parameter
 * @return the number of elements transferred
 */

@Beta
@CanIgnoreReturnValue
public static <E> int drainUninterruptibly(
  BlockingQueue<E> q,
  Collection<? super E> buffer,
  int numElements,
  long timeout,
  TimeUnit unit) {
  Preconditions.checkNotNull(buffer);
  long deadline = System.nanoTime() + unit.toNanos(timeout);
  int added = 0;
  boolean interrupted = false;
  try {
    while (added < numElements) {
      // we could rely solely on #poll, but #drainTo might be more efficient when there are
      // multiple elements already available (e.g. LinkedBlockingQueue#drainTo locks only once)
      added += q.drainTo(buffer, numElements - added);
      if (added < numElements) { // not enough elements immediately available; will have to poll
        E e; // written exactly once, by a successful (uninterrupted) invocation of #poll
        while (true) {
          try {
            e = q.poll(deadline - System.nanoTime(), TimeUnit.NANOSECONDS);
            break;
          } catch (InterruptedException ex) {
            interrupted = true; // note interruption and retry
          }
        }
        if (e == null) {
          break; // we already waited enough, and there are no more elements in sight
        }
        buffer.add(e);
        added++;
      }
    }
  } finally {
    if (interrupted) {
      Thread.currentThread().interrupt();
    }
  }
  return added;
}
 
Example 20
Source File: ChunkedRunningQuery.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void releaseAcceptedMessages() {

    for (Map.Entry<BSBundle, BlockingQueue<IChunkMessage<IBindingSet>>> e : operatorQueues
            .entrySet()) {

        final BlockingQueue<IChunkMessage<IBindingSet>> queue = e.getValue();

        if (queue.isEmpty())
            continue;
        
        final LinkedList<IChunkMessage<IBindingSet>> c = new LinkedList<IChunkMessage<IBindingSet>>();

        queue.drainTo(c);

        for (IChunkMessage<IBindingSet> msg : c) {

            msg.release();
            
        }
        
    }
 
}