Example usage for java.util.concurrent BlockingQueue take

List of usage examples for java.util.concurrent BlockingQueue take

Introduction

In this page you can find the example usage for java.util.concurrent BlockingQueue take.

Prototype

E take() throws InterruptedException;

Source Link

Document

Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.

Usage

From source file:org.spf4j.recyclable.impl.ObjectPoolVsApache.java

private long testPoolApache(final RetryExecutor<Integer> exec,
        final org.apache.commons.pool.impl.GenericObjectPool pool,
        final BlockingQueue<Future<Integer>> completionQueue) throws InterruptedException, ExecutionException {
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < TEST_TASKS; i++) {
        exec.submit(new TestCallableApache(pool, i));
    }// ww  w  .j ava2s  .  co  m
    for (int i = 0; i < TEST_TASKS; i++) {
        completionQueue.take().get();
    }
    long elapsedTime = System.currentTimeMillis() - startTime;
    System.out.println("Completed all " + TEST_TASKS + " tasks in " + elapsedTime + "ms ");
    return elapsedTime;
}

From source file:gemlite.core.internal.admin.service.ExportDataService.java

@SuppressWarnings("unused")
@Override// w w w.  j  av a 2 s  .c o  m
public Object doExecute(Map<String, Object> args) {
    Cache cache = CacheFactory.getAnyInstance();
    String memberId = (String) args.get("MEMBERID");
    String ip = (String) args.get("IP");
    String regionName = (String) args.get("REGIONPATH");
    String filePath = (String) args.get("FILEPATH");
    String showLog = (String) args.get("showLog");
    if (StringUtils.isEmpty(filePath)) {
        filePath = System.getProperty(ITEMS.GS_WORK.name());
    }
    if ("ALL".equals(regionName)) {
        // ?region,?gfd?
        if (filePath.endsWith(".gfd")) {
            int loc1 = filePath.lastIndexOf("\\");
            int loc2 = filePath.lastIndexOf("/");
            filePath = filePath.substring(0, loc1 > loc2 ? loc1 : loc2);
        }
    }
    if (!filePath.endsWith(".gfd") && !filePath.endsWith("\\") && !filePath.endsWith("/"))
        filePath += File.separator;

    args.put("FILEPATH", filePath);
    DistributedMember targetMember = AdminUtil.getDistributedMemberById(cache, memberId, ip);
    try {
        Set<String> regions = new HashSet<String>();
        if ("ALL".equals(regionName)) {
            regions = AdminUtil.getRegionNames(cache);
        } else {
            regions.add(regionName);
        }
        if (targetMember == null) {
            LogUtil.getCoreLog().error("error:targetMember is null!memberId is:" + memberId
                    + " and total members:" + AdminUtil.getAllMemberIds(cache));
            return "error:targetMember is null!memberId is:" + memberId + " and total members:"
                    + AdminUtil.getAllMemberIds(cache);
        }
        if (regions == null || regions.isEmpty()) {
            LogUtil.getCoreLog().error("error:regions is empty!");
            return "error:regions is empty!";
        }
        // region?
        for (String r : regions) {
            Execution execution = null;
            String path = filePath;
            if (!filePath.endsWith(".gfd"))
                path += (r + ".gfd");
            args.put("REGIONPATH", r);
            args.put("FILEPATH", path);
            String last = "last";
            MsgCollector msgCollector = new MsgCollector(last);
            execution = FunctionService.onMember(targetMember).withArgs(args).withCollector(msgCollector);
            ResultCollector rc = execution.execute(new ExportDataFunction().getId());
            BlockingQueue queue = msgCollector.getResult();
            String msg = "";
            try {
                DataLogService service = JpaContext.getService(DataLogService.class);
                // ???
                while ((msg = (String) queue.take()) != last) {
                    GmDataLog data = new GmDataLog(msg);
                    service.saveData(data);
                }
            } catch (Exception e) {
                LogUtil.getCoreLog().error("Write Data Log error:", e);
            }
        }
    } catch (CacheClosedException e) {
        e.printStackTrace();
        LogUtil.getCoreLog().error("error CacheClosedException:", e);
        return e.getMessage();
    } catch (FunctionInvocationTargetException e) {
        e.printStackTrace();
        LogUtil.getCoreLog().error("error FunctionInvocationTargetException:", e);
        return e.getMessage();
    }
    return true;
}

From source file:com.wolvereness.overmapped.lib.MultiProcessor.java

public ProperProcessor(int threadCount, final ThreadFactory factory) {
    final ImmutableList.Builder<Thread> threads = ImmutableList.builder();
    while (threadCount-- >= 1) {
        final Thread thread = factory.newThread(new Runnable() {
            @Override//from w  ww.java 2  s.c  o  m
            public void run() {
                final BlockingQueue<Task<?>> queue = ProperProcessor.this.queue;
                try {
                    while (!shutdown) {
                        queue.take().handle();
                    }
                } catch (final InterruptedException ex) {
                    if (!shutdown)
                        throw new IllegalStateException("Interrupted innappropriately", ex);
                }
            }
        });
        threads.add(thread);
    }

    final Iterator<Thread> it = (this.threads = threads.build()).iterator();
    final Thread initial = it.next();
    if (it.hasNext()) {
        submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                while (it.hasNext()) {
                    it.next().start();
                }
                return null;
            }
        });
    }
    initial.start();
}

From source file:org.apache.hadoop.hbase.master.cleaner.HFileCleaner.java

protected void consumerLoop(BlockingQueue<HFileDeleteTask> queue) {
    try {/*from   w w w.  j  av  a  2  s.co  m*/
        while (running) {
            HFileDeleteTask task = null;
            try {
                task = queue.take();
            } catch (InterruptedException e) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Interrupted while trying to take a task from queue", e);
                }
                break;
            }
            if (task != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Removing: " + task.filePath + " from archive");
                }
                boolean succeed;
                try {
                    succeed = this.fs.delete(task.filePath, false);
                } catch (IOException e) {
                    LOG.warn("Failed to delete file " + task.filePath, e);
                    succeed = false;
                }
                task.setResult(succeed);
                if (succeed) {
                    countDeletedFiles(queue == largeFileQueue);
                }
            }
        }
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Exit thread: " + Thread.currentThread());
        }
    }
}

From source file:org.openscore.lang.systemtests.TriggerFlows.java

public ScoreEvent runSync(CompilationArtifact compilationArtifact,
        Map<String, ? extends Serializable> userInputs, Map<String, ? extends Serializable> systemProperties) {
    final BlockingQueue<ScoreEvent> finishEvent = new LinkedBlockingQueue<>();
    ScoreEventListener finishListener = new ScoreEventListener() {
        @Override/*from  ww  w . java2 s. c  o  m*/
        public void onEvent(ScoreEvent event) throws InterruptedException {
            finishEvent.add(event);
        }
    };
    slang.subscribeOnEvents(finishListener, FINISHED_EVENT);

    slang.run(compilationArtifact, userInputs, systemProperties);

    try {
        ScoreEvent event = finishEvent.take();
        if (event.getEventType().equals(SLANG_EXECUTION_EXCEPTION)) {
            LanguageEventData languageEvent = (LanguageEventData) event.getData();
            throw new RuntimeException((String) languageEvent.get(LanguageEventData.EXCEPTION));
        }
        slang.unSubscribeOnEvents(finishListener);
        return event;
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:io.cloudslang.lang.systemtests.TriggerFlows.java

public ScoreEvent runSync(CompilationArtifact compilationArtifact,
        Map<String, ? extends Serializable> userInputs, Set<SystemProperty> systemProperties) {
    final BlockingQueue<ScoreEvent> finishEvent = new LinkedBlockingQueue<>();
    ScoreEventListener finishListener = new ScoreEventListener() {
        @Override/* ww w.j  a  v  a  2 s  .  c  om*/
        public synchronized void onEvent(ScoreEvent event) throws InterruptedException {
            finishEvent.add(event);
        }
    };
    slang.subscribeOnEvents(finishListener, FINISHED_EVENTS);

    slang.run(compilationArtifact, userInputs, systemProperties);

    try {
        ScoreEvent event = finishEvent.take();
        if (event.getEventType().equals(ScoreLangConstants.SLANG_EXECUTION_EXCEPTION)) {
            LanguageEventData languageEvent = (LanguageEventData) event.getData();
            throw new RuntimeException(languageEvent.getException());
        }
        slang.unSubscribeOnEvents(finishListener);
        return event;
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:fi.jumi.core.suite.SuiteFactoryTest.java

@Test
public void reports_uncaught_exceptions_from_actors_as_internal_errors() throws InterruptedException {
    createSuiteFactory();/*from   ww w. j a v  a 2 s .  c o  m*/

    BlockingQueue<String> spy = new LinkedBlockingQueue<>();
    factory.start(new NullSuiteListener() {
        @Override
        public void onInternalError(String message, StackTrace cause) {
            spy.add(message);
        }
    });

    ActorThread actorThread = factory.actors.startActorThread();
    ActorRef<Runnable> dummyActor = actorThread.bindActor(Runnable.class, () -> {
        throw new RuntimeException("dummy exception");
    });
    dummyActor.tell().run();

    assertThat(spy.take(), startsWith("Uncaught exception in thread jumi-actor-"));
}

From source file:org.apache.hadoop.hbase.client.TestAsyncTable.java

@Test
public void testSimpleMultiple() throws Exception {
    AsyncTableBase table = getTable.get();
    int count = 100;
    CountDownLatch putLatch = new CountDownLatch(count);
    IntStream.range(0, count)//w w  w  . j  ava 2  s. co m
            .forEach(i -> table.put(new Put(concat(row, i)).addColumn(FAMILY, QUALIFIER, concat(VALUE, i)))
                    .thenAccept(x -> putLatch.countDown()));
    putLatch.await();
    BlockingQueue<Boolean> existsResp = new ArrayBlockingQueue<>(count);
    IntStream.range(0, count).forEach(i -> table.exists(new Get(concat(row, i)).addColumn(FAMILY, QUALIFIER))
            .thenAccept(x -> existsResp.add(x)));
    for (int i = 0; i < count; i++) {
        assertTrue(existsResp.take());
    }
    BlockingQueue<Pair<Integer, Result>> getResp = new ArrayBlockingQueue<>(count);
    IntStream.range(0, count).forEach(i -> table.get(new Get(concat(row, i)).addColumn(FAMILY, QUALIFIER))
            .thenAccept(x -> getResp.add(Pair.newPair(i, x))));
    for (int i = 0; i < count; i++) {
        Pair<Integer, Result> pair = getResp.take();
        assertArrayEquals(concat(VALUE, pair.getFirst()), pair.getSecond().getValue(FAMILY, QUALIFIER));
    }
    CountDownLatch deleteLatch = new CountDownLatch(count);
    IntStream.range(0, count)
            .forEach(i -> table.delete(new Delete(concat(row, i))).thenAccept(x -> deleteLatch.countDown()));
    deleteLatch.await();
    IntStream.range(0, count).forEach(i -> table.exists(new Get(concat(row, i)).addColumn(FAMILY, QUALIFIER))
            .thenAccept(x -> existsResp.add(x)));
    for (int i = 0; i < count; i++) {
        assertFalse(existsResp.take());
    }
    IntStream.range(0, count).forEach(i -> table.get(new Get(concat(row, i)).addColumn(FAMILY, QUALIFIER))
            .thenAccept(x -> getResp.add(Pair.newPair(i, x))));
    for (int i = 0; i < count; i++) {
        Pair<Integer, Result> pair = getResp.take();
        assertTrue(pair.getSecond().isEmpty());
    }
}

From source file:org.apache.hadoop.hbase.ipc.RpcExecutor.java

protected void consumerLoop(final BlockingQueue<CallRunner> myQueue) {
    boolean interrupted = false;
    double handlerFailureThreshhold = conf == null ? 1.0
            : conf.getDouble(HConstants.REGION_SERVER_HANDLER_ABORT_ON_ERROR_PERCENT,
                    HConstants.DEFAULT_REGION_SERVER_HANDLER_ABORT_ON_ERROR_PERCENT);
    try {/* w  ww .  j av a  2 s. c om*/
        while (running) {
            try {
                CallRunner task = myQueue.take();
                try {
                    activeHandlerCount.incrementAndGet();
                    task.run();
                } catch (Throwable e) {
                    if (e instanceof Error) {
                        int failedCount = failedHandlerCount.incrementAndGet();
                        if (handlerFailureThreshhold >= 0
                                && failedCount > handlerCount * handlerFailureThreshhold) {
                            String message = "Number of failed RpcServer handler exceeded threshhold "
                                    + handlerFailureThreshhold + "  with failed reason: "
                                    + StringUtils.stringifyException(e);
                            if (abortable != null) {
                                abortable.abort(message, e);
                            } else {
                                LOG.error("Received " + StringUtils.stringifyException(e)
                                        + " but not aborting due to abortable being null");
                                throw e;
                            }
                        } else {
                            LOG.warn("RpcServer handler threads encountered errors "
                                    + StringUtils.stringifyException(e));
                        }
                    } else {
                        LOG.warn("RpcServer handler threads encountered exceptions "
                                + StringUtils.stringifyException(e));
                    }
                } finally {
                    activeHandlerCount.decrementAndGet();
                }
            } catch (InterruptedException e) {
                interrupted = true;
            }
        }
    } finally {
        if (interrupted) {
            Thread.currentThread().interrupt();
        }
    }
}

From source file:com.tesobe.obp.transport.spi.ConnectorNov2016Test.java

@Before
public void connector() {
    Transport.Factory factory = Transport.factory(Transport.Version.Nov2016, Transport.Encoding.json)
            .map(Function.identity()).orElseThrow(IllegalArgumentException::new);
    Receiver receiver = new ReceiverNov2016(new MockResponder(), factory.codecs());
    final BlockingQueue<String> in = new SynchronousQueue<>();
    final BlockingQueue<Message> out = new SynchronousQueue<>();
    final Sender sender = request -> {
        out.put(request);//from   ww  w  .  j  av a2s  .c om

        return in.take();
    };

    // north: sender
    connector = factory.connector(sender);

    // south: receiver in a background thread
    service.submit(new Callable<Void>() {
        @Override
        @SuppressWarnings({ "InfiniteLoopStatement" })
        public Void call() throws InterruptedException {
            for (;;) {
                in.put(receiver.respond(out.take()));
            }
        }
    });
}