java.util.concurrent.ExecutorService Java Examples

The following examples show how to use java.util.concurrent.ExecutorService. 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: StorageConsumerTest.java    From zipkin-sparkstreaming with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 1000L)
public void get_memoizes() throws InterruptedException {
  AtomicInteger provisionCount = new AtomicInteger();

  StorageConsumer storageConsumer = new StorageConsumer() {
    @Override protected StorageComponent tryCompute() {
      provisionCount.incrementAndGet();
      return new InMemoryStorage();
    }
  };

  int getCount = 1000;
  CountDownLatch latch = new CountDownLatch(getCount);
  ExecutorService exec = Executors.newFixedThreadPool(10);
  for (int i = 0; i < getCount; i++) {
    exec.execute(() -> {
      storageConsumer.get();
      latch.countDown();
    });
  }
  latch.await();
  exec.shutdown();
  exec.awaitTermination(1, TimeUnit.SECONDS);

  assertThat(provisionCount.get()).isEqualTo(1);
}
 
Example #2
Source File: AbstractClient.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected void setAsyncMessageObserverIfNeeded(Exchange exchange) {
    if (!exchange.isSynchronous()) {
        ExecutorService executor = (ExecutorService)cfg.getRequestContext().get(EXECUTOR_SERVICE_PROPERTY);
        if (executor != null) {
            exchange.put(Executor.class, executor);

            final ClientMessageObserver observer = new ClientMessageObserver(cfg);

            exchange.put(MessageObserver.class, message -> {
                if (!message.getExchange().containsKey(Executor.class.getName() + ".USING_SPECIFIED")) {
                    executor.execute(() -> {
                        observer.onMessage(message);
                    });
                } else {
                    observer.onMessage(message);
                }
            });
        }
    }
}
 
Example #3
Source File: JpaTicketRegistryTests.java    From springboot-shiro-cas-mybatis with MIT License 6 votes vote down vote up
@Test
@IfProfileValue(name="cas.jpa.concurrent", value="true")
public void verifyConcurrentServiceTicketGeneration() throws Exception {
    final TicketGrantingTicket newTgt = newTGT();
    addTicketInTransaction(newTgt);
    final ExecutorService executor = Executors.newFixedThreadPool(CONCURRENT_SIZE);
    try {
        final List<ServiceTicketGenerator> generators = new ArrayList<>(CONCURRENT_SIZE);
        for (int i = 0; i < CONCURRENT_SIZE; i++) {
            generators.add(new ServiceTicketGenerator(newTgt.getId(), this.jpaTicketRegistry, this.txManager));
        }
        final List<Future<String>> results = executor.invokeAll(generators);
        for (final Future<String> result : results) {
            assertNotNull(result.get());
        }
    } catch (final Exception e) {
        logger.debug("testConcurrentServiceTicketGeneration produced an error", e);
        fail("testConcurrentServiceTicketGeneration failed.");
    } finally {
        executor.shutdownNow();
    }
}
 
Example #4
Source File: ThreadPoolExecutorTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * invokeAny(c) throws ExecutionException if no task completes
 */
public void testInvokeAny4() throws Exception {
    final ExecutorService e =
        new ThreadPoolExecutor(2, 2,
                               LONG_DELAY_MS, MILLISECONDS,
                               new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(e)) {
        List<Callable<String>> l = new ArrayList<Callable<String>>();
        l.add(new NPETask());
        try {
            e.invokeAny(l);
            shouldThrow();
        } catch (ExecutionException success) {
            assertTrue(success.getCause() instanceof NullPointerException);
        }
    }
}
 
Example #5
Source File: CompletableFuturesTest.java    From protonpack with MIT License 6 votes vote down vote up
@Test
public void failsFastOnAnyFutureFailure() throws ExecutionException, InterruptedException {
    ExecutorService threadPool = Executors.newFixedThreadPool(10);
    IllegalStateException expectedException = new IllegalStateException("19! Aaargh!");
    CompletableFuture<List<Integer>> integers = IntStream.range(0, 1000)
            .mapToObj(i -> CompletableFuture.supplyAsync(() -> {
                try {
                    Thread.sleep(random.nextInt(100));
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (i == 19) {
                    throw expectedException;
                }
                return i;
            }, threadPool))
            .collect(CompletableFutures.toFutureList());

    AtomicReference<Throwable> exc = new AtomicReference<>();
    integers.handle((success, failure) -> { exc.set(failure.getCause()); return null; }).get();

    assertThat(exc.get(), equalTo(expectedException));
}
 
Example #6
Source File: ThriftConnection.java    From hbase with Apache License 2.0 6 votes vote down vote up
public ThriftConnection(Configuration conf, ExecutorService pool, final User user)
    throws IOException {
  this.conf = conf;
  this.user = user;
  this.host = conf.get(Constants.HBASE_THRIFT_SERVER_NAME);
  this.port = conf.getInt(Constants.HBASE_THRIFT_SERVER_PORT, -1);
  Preconditions.checkArgument(port > 0);
  Preconditions.checkArgument(host != null);
  this.isFramed = conf.getBoolean(Constants.FRAMED_CONF_KEY, Constants.FRAMED_CONF_DEFAULT);
  this.isCompact = conf.getBoolean(Constants.COMPACT_CONF_KEY, Constants.COMPACT_CONF_DEFAULT);
  this.operationTimeout = conf.getInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT,
      HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT);
  this.connectTimeout = conf.getInt(SOCKET_TIMEOUT_CONNECT, DEFAULT_SOCKET_TIMEOUT_CONNECT);

  String className = conf.get(Constants.HBASE_THRIFT_CLIENT_BUIDLER_CLASS,
      DefaultThriftClientBuilder.class.getName());
  try {
    Class<?> clazz = Class.forName(className);
    Constructor<?> constructor = clazz
        .getDeclaredConstructor(ThriftConnection.class);
    constructor.setAccessible(true);
    clientBuilder = (ThriftClientBuilder) constructor.newInstance(this);
  }catch (Exception e) {
    throw new IOException(e);
  }
}
 
Example #7
Source File: DoubleAccumulatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * accumulates by multiple threads produce correct result
 */
public void testAccumulateAndGetMT() {
    final int incs = 1000000;
    final int nthreads = 4;
    final ExecutorService pool = Executors.newCachedThreadPool();
    DoubleAccumulator a = new DoubleAccumulator(Double::max, 0.0);
    Phaser phaser = new Phaser(nthreads + 1);
    for (int i = 0; i < nthreads; ++i)
        pool.execute(new AccTask(a, phaser, incs));
    phaser.arriveAndAwaitAdvance();
    phaser.arriveAndAwaitAdvance();
    double expected = incs - 1;
    double result = a.get();
    assertEquals(expected, result);
    pool.shutdown();
}
 
Example #8
Source File: AstyanaxEventReaderDAO.java    From emodb with Apache License 2.0 6 votes vote down vote up
private static ExecutorService defaultCleanupExecutor(String metricsGroup, LifeCycleRegistry lifeCycle, MetricRegistry metricRegistry) {
    final Meter meter = metricRegistry.meter(MetricRegistry.name(metricsGroup, "AstyanaxEventReaderDAO", "discarded_slab_cleanup"));
    String nameFormat = "Events Slab Reader Cleanup-" + metricsGroup.substring(metricsGroup.lastIndexOf('.') + 1) + "-%d";
    ExecutorService executor = new ThreadPoolExecutor(
            NUM_CLEANUP_THREADS, NUM_CLEANUP_THREADS,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(MAX_CLEANUP_QUEUE_LENGTH),
            new ThreadFactoryBuilder().setNameFormat(nameFormat).build(),
            new ThreadPoolExecutor.DiscardPolicy() {
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
                    meter.mark();
                }
            });
    lifeCycle.manage(new ExecutorServiceManager(executor, Duration.seconds(5), nameFormat));
    return executor;
}
 
Example #9
Source File: OmrExecutors.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Get the pool ready to use.
 */
public synchronized ExecutorService getPool ()
{
    if (!creationAllowed) {
        logger.info("No longer allowed to create pool: {}", getName());

        throw new ProcessingCancellationException("Executor closed");
    }

    if (!isActive()) {
        logger.debug("Creating pool: {}", getName());
        pool = createPool();
    }

    return pool;
}
 
Example #10
Source File: Verifier.java    From lmdbjava with Apache License 2.0 6 votes vote down vote up
/**
 * Execute the verifier for the given duration.
 *
 * <p>
 * This provides a simple way to execute the verifier for those applications
 * which do not wish to manage threads directly.
 *
 * @param duration amount of time to execute
 * @param unit     units used to express the duration
 * @return number of database rows successfully verified
 */
public long runFor(final long duration, final TimeUnit unit) {
  final long deadline = System.currentTimeMillis() + unit.toMillis(duration);
  final ExecutorService es = Executors.newSingleThreadExecutor();
  final Future<Long> future = es.submit(this);
  try {
    while (System.currentTimeMillis() < deadline && !future.isDone()) {
      Thread.sleep(unit.toMillis(1));
    }
  } catch (final InterruptedException ignored) {
  } finally {
    stop();
  }
  final long result;
  try {
    result = future.get();
  } catch (final InterruptedException | ExecutionException ex) {
    throw new IllegalStateException(ex);
  } finally {
    es.shutdown();
  }
  return result;
}
 
Example #11
Source File: CustomerAsyncs.java    From Java-Coding-Problems with MIT License 6 votes vote down vote up
public static void fetchOrderSummaryExecutor()
        throws InterruptedException, ExecutionException {

    ExecutorService executor = Executors.newSingleThreadExecutor();

    CompletableFuture<String> cfOrderSummary = CompletableFuture.supplyAsync(() -> {
        try {
            logger.info(() -> "Fetch order summary by: " + Thread.currentThread().getName());
            Thread.sleep(500);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        return "Order Summary #91022";
    }, executor);

    String summary = cfOrderSummary.get(); // wait for summary to be available, this is blocking
    logger.info(() -> "Order summary: " + summary + "\n");

    executor.shutdownNow();
}
 
Example #12
Source File: LocalJobRunner.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the executor service used to run map tasks.
 *
 * @return an ExecutorService instance that handles map tasks
 */
protected synchronized ExecutorService createMapExecutor() {

  // Determine the size of the thread pool to use
  int maxMapThreads = job.getInt(LOCAL_MAX_MAPS, 1);
  if (maxMapThreads < 1) {
    throw new IllegalArgumentException(
        "Configured " + LOCAL_MAX_MAPS + " must be >= 1");
  }
  maxMapThreads = Math.min(maxMapThreads, this.numMapTasks);
  maxMapThreads = Math.max(maxMapThreads, 1); // In case of no tasks.

  LOG.debug("Starting mapper thread pool executor.");
  LOG.debug("Max local threads: " + maxMapThreads);
  LOG.debug("Map tasks to process: " + this.numMapTasks);

  // Create a new executor service to drain the work queue.
  ThreadFactory tf = new ThreadFactoryBuilder()
    .setNameFormat("LocalJobRunner Map Task Executor #%d")
    .build();
  ExecutorService executor = Executors.newFixedThreadPool(maxMapThreads, tf);

  return executor;
}
 
Example #13
Source File: AssemblyLine.java    From Java-Coding-Problems with MIT License 6 votes vote down vote up
private static boolean shutdownExecutor(ExecutorService executor) {
    executor.shutdown();
    try {
        if (!executor.awaitTermination(TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
            executor.shutdownNow();

            return executor.awaitTermination(TIMEOUT_MS, TimeUnit.MILLISECONDS);
        }

        return true;
    } catch (InterruptedException ex) {
        executor.shutdownNow();
        Thread.currentThread().interrupt();
        logger.severe(() -> "Exception: " + ex);
    }
    return false;
}
 
Example #14
Source File: MithraObjectGraphExtractor.java    From reladomo with Apache License 2.0 6 votes vote down vote up
private void writeToFiles(ExecutorService executor, Map<Pair<RelatedFinder, Object>, List<MithraDataObject>> extract)
{
    UnifiedMap<File, UnifiedMap<RelatedFinder, List<MithraDataObject>>> dataByFile = UnifiedMap.newMap();
    for (Pair<RelatedFinder, Object> key : extract.keySet())
    {
        File outputFile = this.outputStrategy.getOutputFile(key.getOne(), key.getTwo());
        if (outputFile != null)
        {
            dataByFile.getIfAbsentPut(outputFile, UnifiedMap.<RelatedFinder, List<MithraDataObject>>newMap()).put(key.getOne(), extract.get(key));
        }
    }
    for (File file : dataByFile.keySet())
    {
        executor.submit(new FileWriterTask(file, dataByFile.get(file)));
    }
}
 
Example #15
Source File: BookKeeperClientRFLibrary.java    From rubix with Apache License 2.0 6 votes vote down vote up
/**
 * Execute multiple tasks concurrently.
 *
 * @param numThreads   The number of available threads for concurrent execution.
 * @param tasks        The tasks to execute.
 * @param staggerTasks If true, add delay between task submissions.
 * @param <T>          The return type of the task.
 * @return A list of results for each task executed.
 * @throws InterruptedException if task execution is interrupted.
 */
private <T> List<Future<T>> executeConcurrentTasks(int numThreads, List<Callable<T>> tasks, boolean staggerTasks) throws InterruptedException
{
  final ExecutorService service = Executors.newFixedThreadPool(numThreads);
  List<Future<T>> futures = new ArrayList<>();

  if (staggerTasks) {
    // Necessary to preserve order of requests for certain tests.
    for (Callable<T> task : tasks) {
      futures.add(service.submit(task));
      Thread.sleep(100);
    }
  }
  else {
    futures = service.invokeAll(tasks);
  }
  return futures;
}
 
Example #16
Source File: AbstractExecutorServiceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * A submitted failed privileged exception action reports exception
 */
public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
    Runnable r = new CheckedRunnable() {
        public void realRun() throws Exception {
            ExecutorService e = new DirectExecutorService();
            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
                public Object run() throws Exception {
                    throw new IndexOutOfBoundsException();
                }}));

            try {
                future.get();
                shouldThrow();
            } catch (ExecutionException success) {
                assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
            }}};

    runWithPermissions(r);
}
 
Example #17
Source File: PriorityJobScheduler.java    From tutorials with MIT License 5 votes vote down vote up
protected void close(ExecutorService scheduler) {
    scheduler.shutdown();
    try {
        if (!scheduler.awaitTermination(5, TimeUnit.SECONDS)) {
            scheduler.shutdownNow();
        }
    } catch (InterruptedException e) {
        scheduler.shutdownNow();
    }
}
 
Example #18
Source File: ExecutorIT.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Override
public void transactionMarker() throws Exception {
    ExecutorService executor = createExecutorService();
    List<Callable<Void>> callables = Lists.newArrayList();
    callables.add(new Callable<Void>() {
        @Override
        public Void call() {
            new CreateTraceEntry().traceEntryMarker();
            return null;
        }
    });
    callables.add(new Callable<Void>() {
        @Override
        public Void call() {
            new CreateTraceEntry().traceEntryMarker();
            return null;
        }
    });
    callables.add(new Callable<Void>() {
        @Override
        public Void call() {
            new CreateTraceEntry().traceEntryMarker();
            return null;
        }
    });
    executor.invokeAny(callables);
}
 
Example #19
Source File: S3UtilProgram.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static List<CompletableFuture<Integer>> submitLineCountingRequestsForFilesAsync(
		ExecutorService executor, AmazonS3 s3client, String bucket, List<String> files, String s3filePrefix) {
	List<CompletableFuture<Integer>> requests = new ArrayList<>();
	Predicate<String> keyPredicate = getKeyFilterByFileNamePrefix(s3filePrefix);
	files.forEach(file -> {
		if (keyPredicate.test(file)) {
			CompletableFuture<Integer> result = new CompletableFuture<>();
			executor.execute(() ->
				result.complete(Integer.parseInt(S3QueryUtil.queryFile(s3client, bucket, file, countQuery))));
			requests.add(result);
		}
	});
	return requests;
}
 
Example #20
Source File: ThreadPoolExecutorSubclassTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * completed submit of callable returns result
 */
public void testSubmitCallable() throws Exception {
    final ExecutorService e =
        new CustomTPE(2, 2,
                      LONG_DELAY_MS, MILLISECONDS,
                      new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(e)) {
        Future<String> future = e.submit(new StringTask());
        String result = future.get();
        assertSame(TEST_STRING, result);
    }
}
 
Example #21
Source File: FixClientFactory.java    From nanofix with Apache License 2.0 5 votes vote down vote up
private static FixClient createFixClient(final InetSocketAddress socketAddress, final SystemConfig systemConfig)
{
    final PublishingConnectionObserver publishingTransportObserver = new PublishingConnectionObserver();

    final ExecutorService executorService = Executors.newSingleThreadExecutor(new NamedThreadFactory("InboundConnection", true, UNCAUGHT_EXCEPTION_HANDLER));
    final AsyncTcpSocketFactory asyncTcpSocketFactory = new AsyncTcpSocketFactory(executorService);
    final TcpTransport transport = new TcpTransport(publishingTransportObserver, socketAddress, asyncTcpSocketFactory, systemConfig);
    publishingTransportObserver.addObserver(transport);
    return buildFixClient(transport, publishingTransportObserver, MAX_MESSAGE_SIZE);
}
 
Example #22
Source File: ExecutorsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * A new newFixedThreadPool can execute runnables
 */
public void testNewFixedThreadPool1() {
    final ExecutorService e = Executors.newFixedThreadPool(2);
    try (PoolCleaner cleaner = cleaner(e)) {
        e.execute(new NoOpRunnable());
        e.execute(new NoOpRunnable());
        e.execute(new NoOpRunnable());
    }
}
 
Example #23
Source File: ExecutorCompletionServiceTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * If timed poll returns non-null, the returned task is completed
 */
public void testPoll2() throws InterruptedException {
    final ExecutorService e = Executors.newCachedThreadPool();
    final ExecutorCompletionService ecs = new ExecutorCompletionService(e);
    try (PoolCleaner cleaner = cleaner(e)) {
        assertNull(ecs.poll());
        Callable c = new StringTask();
        ecs.submit(c);
        Future f = ecs.poll(SHORT_DELAY_MS, MILLISECONDS);
        if (f != null)
            assertTrue(f.isDone());
    }
}
 
Example #24
Source File: MRRunningJobManagerTest.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void testMRRunningJobManagerRecoverYarnAppWithLock() throws Exception {
    Assert.assertTrue(curator.checkExists().forPath(SHARE_RESOURCES) != null);
    curator.setData().forPath(SHARE_RESOURCES, generateZkSetData());
    ExecutorService service = Executors.newFixedThreadPool(QTY);
    for (int i = 0; i < QTY; ++i) {
        Callable<Void> task = () -> {
            try {
                MRRunningJobManager mrRunningJobManager = new MRRunningJobManager(zkStateConfig);
                for (int j = 0; j < REPETITIONS; ++j) {
                    if(j % 3 == 0) {
                        mrRunningJobManager.delete("yarnAppId", "jobId");
                    } else {
                        mrRunningJobManager.recoverYarnApp("yarnAppId");
                    }
                }
            } catch (Exception e) {
                // log or do something
            }
            return null;
        };
        service.submit(task);
    }

    service.shutdown();
    service.awaitTermination(10, TimeUnit.MINUTES);
    verify(log, never()).error(anyString(), any(Throwable.class));
}
 
Example #25
Source File: CommonsTestHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Run something in parallel
 *
 * @param nCalls
 *        The number of invocations of the passed runnable. Must be &ge; 0.
 * @param aRunnable
 *        The runnable to execute. May not be <code>null</code>.
 */
public static void testInParallel (@Nonnegative final int nCalls, @Nonnull final IThrowingRunnable <? extends Exception> aRunnable)
{
  ValueEnforcer.isGE0 (nCalls, "Calls");
  ValueEnforcer.notNull (aRunnable, "Runnable");

  // More than 20s thread would be overkill!
  final ExecutorService aES = Executors.newFixedThreadPool (20);
  final ICommonsList <String> aErrors = new CommonsVector <> ();
  for (int i = 0; i < nCalls; ++i)
  {
    aES.submit ( () -> {
      try
      {
        aRunnable.run ();
      }
      catch (final Exception ex)
      {
        // Remember thread stack
        aErrors.add (ex.getMessage () + "\n" + StackTraceHelper.getStackAsString (ex));
      }
    });
  }
  ExecutorServiceHelper.shutdownAndWaitUntilAllTasksAreFinished (aES);

  // No errors should have occurred
  if (!aErrors.isEmpty ())
    _fail (StringHelper.getImploded (aErrors));
}
 
Example #26
Source File: ThreadServiceManager.java    From metacat with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param registry registry for spectator
 * @param maxThreads maximum number of threads
 * @param maxQueueSize maximum queue size
 * @param usage an identifier where this pool is used
 */
public ThreadServiceManager(final Registry registry, final int maxThreads,
    final int maxQueueSize, final String usage) {
    final ExecutorService executorService = newFixedThreadPool(
        maxThreads,
        "metacat-" + usage + "-pool-%d",
        maxQueueSize
    );
    this.executor = MoreExecutors.listeningDecorator(executorService);
    RegistryUtil.registerThreadPool(registry, "metacat-service-pool", (ThreadPoolExecutor) executorService);
}
 
Example #27
Source File: SemaphoresManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenDelayQueue_whenTimePass_thenSlotsAvailable() throws InterruptedException {
    final int slots = 50;
    final ExecutorService executorService = Executors.newFixedThreadPool(slots);
    final DelayQueueUsingTimedSemaphore delayQueue = new DelayQueueUsingTimedSemaphore(1, slots);
    IntStream.range(0, slots)
      .forEach(user -> executorService.execute(delayQueue::tryAdd));
    executorService.shutdown();
    executorService.awaitTermination(10, TimeUnit.SECONDS);

    assertEquals(0, delayQueue.availableSlots());
    Thread.sleep(1000);
    assertTrue(delayQueue.availableSlots() > 0);
    assertTrue(delayQueue.tryAdd());
}
 
Example #28
Source File: SimpleAsyncManager.java    From lastaflute with Apache License 2.0 5 votes vote down vote up
protected YourFuture actuallyAsync(ConcurrentAsyncCall callback, ExecutorService service, String title) {
    if (isDestructiveAsyncToNormalSync()) { // destructive (for e.g. UnitTest)
        return destructiveNormalSync(callback);
    } else { // basically here
        final String keyword = title + buildExecutorHashExp(service);
        final Callable<WaitingAsyncResult> task = createCallableTask(callback, keyword);
        final Future<WaitingAsyncResult> nativeFuture = service.submit(task); // real asynchronous
        return new BasicYourFuture(nativeFuture);
    }
}
 
Example #29
Source File: BasicTasksTest.java    From streams with Apache License 2.0 5 votes vote down vote up
@Test
public void testBranching() {
  int numMessages = 100;
  PassthroughDatumCounterProcessor processor = new PassthroughDatumCounterProcessor("");
  StreamsProcessorTask task = new StreamsProcessorTask(processor);
  BlockingQueue<StreamsDatum> outQueue1 = new LinkedBlockingQueue<>();
  BlockingQueue<StreamsDatum> outQueue2 = new LinkedBlockingQueue<>();
  BlockingQueue<StreamsDatum> inQueue = createInputQueue(numMessages);
  task.addOutputQueue(outQueue1);
  task.addOutputQueue(outQueue2);
  task.addInputQueue(inQueue);
  assertEquals(numMessages, task.getInputQueues().get(0).size());
  ExecutorService service = Executors.newFixedThreadPool(1);
  service.submit(task);
  int attempts = 0;
  while(inQueue.size() != 0 ) {
    Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
    ++attempts;
    if(attempts == 10) {
      assertEquals("Processor task failed to output "+(numMessages)+" in a timely fashion.", 0, inQueue.size());
    }
  }
  task.stopTask();

  service.shutdown();
  try {
    if(!service.awaitTermination(5, TimeUnit.SECONDS)){
      service.shutdownNow();
      fail("Service did not terminate.");
    }
    assertTrue("Task should have completed running in allotted time.", service.isTerminated());
  } catch (InterruptedException e) {
    fail("Test Interrupted.");
  }
  assertEquals(numMessages, processor.getMessageCount());
  assertEquals(numMessages, outQueue1.size());
  assertEquals(numMessages, outQueue2.size());
}
 
Example #30
Source File: AllChannelHandler.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
public void caught(Channel channel, Throwable exception) throws RemotingException {
    ExecutorService cexecutor = getExecutorService();
    try {
        cexecutor.execute(new ChannelEventRunnable(channel, handler, ChannelState.CAUGHT, exception));
    } catch (Throwable t) {
        throw new ExecutionException("caught event", channel, getClass() + " error when process caught event .", t);
    }
}