List of usage examples for java.util.concurrent BlockingQueue take
E take() throws InterruptedException;
From source file:com.splout.db.qnode.QNodeHandlerContext.java
/** * This method can be called by {@link QNodeHandler} to cancel the Thrift client cache when a DNode disconnects. * Usually this happens when Hazelcast notifies it. */// www .j a va 2 s.c o m public void discardThriftClientCacheFor(String dnode) throws InterruptedException { thriftClientCacheLock.lock(); try { // discarding all connections to a DNode who leaved log.info(Thread.currentThread().getName() + " : trashing queue for [" + dnode + "] as it leaved."); BlockingQueue<DNodeService.Client> dnodeQueue = thriftClientCache.get(dnode); // release connections until empty while (!dnodeQueue.isEmpty()) { dnodeQueue.take().getOutputProtocol().getTransport().close(); } thriftClientCache.remove(dnode); // to indicate that the DNode is not present } finally { thriftClientCacheLock.unlock(); } }
From source file:org.opensilk.music.playback.control.PlaybackController.java
public static PlaybackServiceConnection bindService(Context context) throws InterruptedException { if (context == null) { throw new NullPointerException("context == null"); }//from ww w . j a va 2 s . c o m ensureNotOnMainThread(context); final BlockingQueue<IPlaybackService> q = new LinkedBlockingQueue<IPlaybackService>(1); ServiceConnection keyChainServiceConnection = new ServiceConnection() { volatile boolean mConnectedAtLeastOnce = false; @Override public void onServiceConnected(ComponentName name, IBinder service) { if (!mConnectedAtLeastOnce) { mConnectedAtLeastOnce = true; try { q.put(IPlaybackService.Stub.asInterface(service)); } catch (InterruptedException e) { // will never happen, since the queue starts with one available slot } } } @Override public void onServiceDisconnected(ComponentName name) { } }; Intent intent = new Intent(context, PlaybackService.class); boolean isBound = context.bindService(intent, keyChainServiceConnection, Context.BIND_AUTO_CREATE); if (!isBound) { throw new AssertionError("could not bind to KeyChainService"); } return new PlaybackServiceConnection(context, keyChainServiceConnection, q.take()); }
From source file:com.google.gplus.provider.TestGPlusUserActivityCollector.java
/** * Creates a randomized activity and randomized date range. * The activity feed is separated into three chunks, * |. . . data too recent to be in date range . . .||. . . data in date range. . .||. . . data too old to be in date range| * [index 0, ............................................................................................., index length-1] * Inside of those chunks data has no order, but the list is ordered by those three chunks. * * The test will check to see if the num of data in the date range make onto the output queue. *//*w w w . j ava 2 s . c o m*/ @Test @Repeat(iterations = 3) public void testWithBeforeAndAfterDates() throws InterruptedException { //initialize counts assuming no date ranges will be used int numActivities = randomIntBetween(0, 1000); int numActivitiesInDateRange = numActivities; int numberOutOfRange = 0; int numBerforeRange = 0; int numAfterRange = 0; //determine if date ranges will be used DateTime beforeDate = null; DateTime afterDate = null; if (randomInt() % 2 == 0) { beforeDate = DateTime.now().minusDays(randomIntBetween(1, 5)); } if (randomInt() % 2 == 0) { if (beforeDate == null) { afterDate = DateTime.now().minusDays(randomIntBetween(1, 10)); } else { afterDate = beforeDate.minusDays(randomIntBetween(1, 10)); } } //update counts if date ranges are going to be used. if (beforeDate != null || afterDate != null) { //assign amount to be in range numActivitiesInDateRange = randomIntBetween(0, numActivities); numberOutOfRange = numActivities - numActivitiesInDateRange; } if (beforeDate == null && afterDate != null) { //assign all out of range to be before the start of the range numBerforeRange = numberOutOfRange; } else if (beforeDate != null && afterDate == null) { //assign all out of range to be after the start of the range numAfterRange = numberOutOfRange; } else if (beforeDate != null && afterDate != null) { //assign half before range and half after the range numAfterRange = (numberOutOfRange / 2) + (numberOutOfRange % 2); numBerforeRange = numberOutOfRange / 2; } Plus plus = createMockPlus(numBerforeRange, numAfterRange, numActivitiesInDateRange, afterDate, beforeDate); BackOffStrategy strategy = new ConstantTimeBackOffStrategy(1); BlockingQueue<StreamsDatum> datums = new LinkedBlockingQueue<>(); UserInfo userInfo = new UserInfo(); userInfo.setUserId("A"); userInfo.setAfterDate(afterDate); userInfo.setBeforeDate(beforeDate); GPlusUserActivityCollector collector = new GPlusUserActivityCollector(plus, datums, strategy, userInfo); collector.run(); assertEquals(numActivitiesInDateRange, datums.size()); while (!datums.isEmpty()) { StreamsDatum datum = datums.take(); assertNotNull(datum); assertNotNull(datum.getDocument()); assertTrue(datum.getDocument() instanceof String); assertTrue(((String) datum.getDocument()).contains(IN_RANGE_IDENTIFIER)); //only in range documents are on the out going queue. } }
From source file:gridool.processors.job.GridJobWorker.java
private void aggregateTaskResults(final Map<String, Pair<GridTask, List<Future<?>>>> taskMap, final BlockingQueue<GridTaskResult> resultQueue, final TextProgressBar progressBar) throws GridException { final int numTasks = taskMap.size(); for (int finishedTasks = 0; finishedTasks < numTasks; finishedTasks++) { GridTaskResult result = null;//www . ja va2 s. c om try { if (JOB_INSPECTION_INTERVAL_SEC == -1L) { result = resultQueue.take(); } else { while (true) { result = resultQueue.poll(JOB_INSPECTION_INTERVAL_SEC, TimeUnit.SECONDS); if (result != null) { break; } if (LOG.isInfoEnabled()) { showRemainingTasks(job, taskMap); } } } } catch (InterruptedException e) { LOG.warn("GridTask is interrupted", e); throw new GridException("GridTask is interrupted", e); } final String taskId = result.getTaskId(); final GridTaskResultPolicy policy = job.result(result); switch (policy) { case CONTINUE: { progressBar.inc(); taskMap.remove(taskId); break; } case CONTINUE_WITH_SPECULATIVE_TASKS: { progressBar.inc(); taskMap.remove(taskId); for (final GridTask task : result.getSpeculativeTasks()) { String speculativeTaskId = task.getTaskId(); Pair<GridTask, List<Future<?>>> entry = taskMap.get(speculativeTaskId); if (entry == null) {// Is the task already finished? LOG.info("No need to run a speculative task: " + speculativeTaskId); } else { Future<?> newFuture = runSpeculativeTask(task, resultQueue); // SKIP handling is required for speculative tasks if (newFuture != null) { List<Future<?>> futures = entry.getSecond(); futures.add(newFuture); } } } break; } case RETURN: taskMap.remove(taskId); return; case CANCEL_RETURN: { taskMap.remove(taskId); if (!taskMap.isEmpty()) { cancelRemainingTasks(taskMap); } return; } case FAILOVER: { Pair<GridTask, List<Future<?>>> entry = taskMap.get(taskId); GridTask task = entry.getFirst(); Future<?> newFuture = failover(task, resultQueue); List<Future<?>> futureList = entry.getSecond(); futureList.add(newFuture); finishedTasks--; break; } case SKIP: finishedTasks--; break; default: assert false : "Unexpected policy: " + policy; } } }
From source file:com.miraclelinux.historygluon.BridgeWorker.java
private boolean queryAll(byte[] pktBuf, int idx) throws IOException { try {/*w w w .j a v a 2s. co m*/ // write reply header to the socket replyQueryAllHeader(); // data loop BlockingQueue<HistoryStreamElement> queue = m_driver.openHistoryStream(); int errorCode = ErrorCode.SUCCESS; while (true) { HistoryStreamElement streamElem = queue.take(); if (streamElem.isEndOfStream()) { errorCode = streamElem.getErrorCode(); break; } sendOneHistoryData(streamElem.getData()); m_ostream.flush(); } replyQueryAllFooter(errorCode); } catch (Exception e) { e.printStackTrace(); m_log.error(e); replyQueryAllFooter(ErrorCode.UNKNOWN_ERROR); return true; } finally { m_driver.closeHistoryStream(); } return true; }
From source file:gridool.deployment.PeerClassLoader.java
@Override public Class<?> findClass(String name) throws ClassNotFoundException { BlockingQueue<GridTaskResult> resultQueue = new SynchronousQueue<GridTaskResult>(); String jobId = "p2p-classloading_" + resultQueue.hashCode(); responseQueue.addResponseQueue(jobId, resultQueue); if (LOG.isInfoEnabled()) { LOG.info("Loading a class '" + name + "' from " + remoteNode); }//ww w. j av a 2 s . c om GridNode localNode = communicator.getLocalNode(); final GridGetClassTask task = new GridGetClassTask(jobId, localNode, name); try {// send a class-loading request communicator.sendTaskRequest(task, remoteNode); } catch (GridException e) { throw new ClassNotFoundException("Failed sending a GridGetClassTask of the class: " + name, e); } // Receive a requested class final GridTaskResult result; try { result = resultQueue.take(); // TODO timeout } catch (InterruptedException e) { throw new ClassNotFoundException("An error caused while receiving a class: " + name, e); } final ClassData clazz = result.getResult(); assert (clazz != null); byte[] clazzData = clazz.getClassData(); long ts = clazz.getTimestamp(); final Class<?> definedClazz = parentLdr.defineClassIfNeeded(name, clazzData, ts); // define enclosing classes ClassData enclosingClass = clazz.getEnclosingClass(); while (enclosingClass != null) { defineClass(enclosingClass, parentLdr); enclosingClass = enclosingClass.getEnclosingClass(); } return definedClazz; }
From source file:org.kuali.rice.krad.data.provider.ProviderRegistryImplTest.java
/** * Verifies ProviderRegistryImpl is threadsafe *//*from w ww .jav a2 s. co m*/ @Test public void testConcurrency() throws InterruptedException { final Class<? extends Provider>[] TYPES = new Class[] { Provider.class, MetadataProvider.class, PersistenceProvider.class, CustomProvider.class }; int providers = 50; int threads = providers * 2; // just use live threads for all consumers/producers to ensure no consumer deadlock final BlockingQueue<Provider> queue = new LinkedBlockingQueue<Provider>(); ExecutorService threadpool = Executors.newFixedThreadPool(threads); Callable<Object>[] producers = new Callable[providers]; Callable<Object>[] consumers = new Callable[providers]; Callable<Object> producer = new Callable<Object>() { @Override public Object call() throws Exception { Provider p = mock(TYPES[RandomUtils.nextInt(5)]); registry.registerProvider(p); queue.add(p); return null; } }; Callable<Object> consumer = new Callable<Object>() { @Override public Object call() throws Exception { Provider p = queue.take(); registry.unregisterProvider(p); return null; } }; Arrays.fill(producers, producer); Arrays.fill(consumers, consumer); List<Callable<Object>> tasks = new ArrayList<Callable<Object>>(providers * 2); tasks.addAll(Arrays.asList(producers)); tasks.addAll(Arrays.asList(consumers)); Collections.shuffle(tasks); System.out.println("Registering and unregistering " + providers + " providers"); threadpool.invokeAll(tasks, 10, TimeUnit.SECONDS); // all producers and consumers should have run, we should be back at 0 providers registered assertEquals(0, registry.getProviders().size()); }
From source file:gobblin.couchbase.writer.CouchbaseWriter.java
@Override public Future<WriteResponse> write(final D record, final WriteCallback callback) { assertRecordWritable(record);//w w w . j a va 2 s. c o m if (record instanceof TupleDocument) { ((TupleDocument) record).content().value1().retain(); } Observable<D> observable = _bucket.async().upsert(record); if (callback == null) { return new WriteResponseFuture<>( observable.timeout(_operationTimeout, _operationTimeunit).toBlocking().toFuture(), _defaultWriteResponseMapper); } else { final AtomicBoolean callbackFired = new AtomicBoolean(false); final BlockingQueue<Pair<WriteResponse, Throwable>> writeResponseQueue = new ArrayBlockingQueue<>(1); final Future<WriteResponse> writeResponseFuture = new Future<WriteResponse>() { @Override public boolean cancel(boolean mayInterruptIfRunning) { return false; } @Override public boolean isCancelled() { return false; } @Override public boolean isDone() { return callbackFired.get(); } @Override public WriteResponse get() throws InterruptedException, ExecutionException { Pair<WriteResponse, Throwable> writeResponseThrowablePair = writeResponseQueue.take(); return getWriteResponseorThrow(writeResponseThrowablePair); } @Override public WriteResponse get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { Pair<WriteResponse, Throwable> writeResponseThrowablePair = writeResponseQueue.poll(timeout, unit); if (writeResponseThrowablePair == null) { throw new TimeoutException("Timeout exceeded while waiting for future to be done"); } else { return getWriteResponseorThrow(writeResponseThrowablePair); } } }; observable.timeout(_operationTimeout, _operationTimeunit).subscribe(new Subscriber<D>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { callbackFired.set(true); writeResponseQueue.add(new Pair<WriteResponse, Throwable>(null, e)); callback.onFailure(e); } @Override public void onNext(D doc) { try { callbackFired.set(true); WriteResponse writeResponse = new GenericWriteResponse<D>(doc); writeResponseQueue.add(new Pair<WriteResponse, Throwable>(writeResponse, null)); callback.onSuccess(writeResponse); } finally { if (doc instanceof TupleDocument) { ((TupleDocument) doc).content().value1().release(); } } } }); return writeResponseFuture; } }
From source file:com.netflix.curator.framework.imps.TestFramework.java
@Test public void testNamespaceWithWatcher() throws Exception { CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); CuratorFramework client = builder.connectString(server.getConnectString()).namespace("aisa") .retryPolicy(new RetryOneTime(1)).build(); client.start();/* w w w .jav a2 s.com*/ try { final BlockingQueue<String> queue = new LinkedBlockingQueue<String>(); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { try { queue.put(event.getPath()); } catch (InterruptedException e) { throw new Error(e); } } }; client.create().forPath("/base"); client.getChildren().usingWatcher(watcher).forPath("/base"); client.create().forPath("/base/child"); String path = queue.take(); Assert.assertEquals(path, "/base"); } finally { client.close(); } }
From source file:io.nats.client.ITClusterTest.java
/** * Ensures that if a ping is not ponged within the pingInterval, that a disconnect/reconnect * takes place./*from w ww. j a v a 2 s . co m*/ * <p> * <p>We test this by setting maxPingsOut < 0 and setting the pingInterval very small. After * the first * disconnect, we measure the reconnect-to-disconnect time to ensure it isn't greater than 2 * * pingInterval. * * @throws Exception if anything goes wrong */ @Test public void testPingReconnect() throws Exception { final int reconnects = 4; final AtomicInteger timesReconnected = new AtomicInteger(); // setLogLevel(Level.DEBUG); try (NatsServer s1 = runServerOnPort(1222)) { Options opts = new Options.Builder(defaultOptions()).dontRandomize().reconnectWait(200).pingInterval(50) .maxPingsOut(-1).timeout(1000).build(); opts.servers = Nats.processUrlArray(testServers); final CountDownLatch wg = new CountDownLatch(reconnects); final BlockingQueue<Long> rch = new LinkedBlockingQueue<Long>(reconnects); final BlockingQueue<Long> dch = new LinkedBlockingQueue<Long>(reconnects); opts.disconnectedCb = new DisconnectedCallback() { public void onDisconnect(ConnectionEvent event) { dch.add(System.nanoTime()); } }; opts.reconnectedCb = new ReconnectedCallback() { @Override public void onReconnect(ConnectionEvent event) { rch.add(System.nanoTime()); wg.countDown(); } }; try (ConnectionImpl c = (ConnectionImpl) opts.connect()) { wg.await(); s1.shutdown(); // Throw away the first one dch.take(); for (int i = 0; i < reconnects - 1; i++) { Long disconnectedAt = dch.take(); Long reconnectedAt = rch.take(); Long pingCycle = TimeUnit.NANOSECONDS.toMillis(disconnectedAt - reconnectedAt); assertFalse(String.format("Reconnect due to ping took %d msec", pingCycle), pingCycle > 2 * c.getOptions().getPingInterval()); } } } }