Java Code Examples for com.google.common.util.concurrent.MoreExecutors#shutdownAndAwaitTermination()

The following examples show how to use com.google.common.util.concurrent.MoreExecutors#shutdownAndAwaitTermination() . 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: TlsTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@After
public void tearDown() {
  if (server != null) {
    server.shutdown();
  }
  if (channel != null) {
    channel.shutdown();
  }
  MoreExecutors.shutdownAndAwaitTermination(executor, 5, TimeUnit.SECONDS);
}
 
Example 2
Source File: Http.java    From StubbornJava with MIT License 5 votes vote down vote up
public static void getInParallel(OkHttpClient client, String url, int count) {
    ExecutorService exec = Executors.newFixedThreadPool(count);
    for (int i = 0; i < count; i++) {
        exec.submit(() -> Http.get(client, url));
    }
    MoreExecutors.shutdownAndAwaitTermination(exec, 30, TimeUnit.SECONDS);
}
 
Example 3
Source File: ListingConnector.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public void destroy() {
  repository.close();
  repositoryContext.getEventBus().unregister(this);
  if (listeningExecutorService != null) {
    logger.log(Level.INFO, "Shutting down the executor service.");
    MoreExecutors.shutdownAndAwaitTermination(listeningExecutorService, 5L, TimeUnit.MINUTES);
  }
}
 
Example 4
Source File: FullSyncIdentityConnector.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
/** Releases resources and shuts down worker processes. */
@Override
public void destroy() {
  identityRepository.close();
  try {
    stateManager.close();
  } catch (IOException e) {
    logger.log(Level.WARNING, "Failed to close identity state sucessfully", e);
  }
  if (listeningExecutorService != null) {
    MoreExecutors.shutdownAndAwaitTermination(listeningExecutorService, 5, TimeUnit.MINUTES);
  }
}
 
Example 5
Source File: Http.java    From StubbornJava with MIT License 5 votes vote down vote up
public static void getInParallel(OkHttpClient client, String url, int count) {
    ExecutorService exec = Executors.newFixedThreadPool(count);
    for (int i = 0; i < count; i++) {
        exec.submit(() -> Http.get(client, url));
    }
    MoreExecutors.shutdownAndAwaitTermination(exec, 30, TimeUnit.SECONDS);
}
 
Example 6
Source File: SingularitySmtpSender.java    From Singularity with Apache License 2.0 5 votes vote down vote up
@Override
public void stop() {
  if (mailSenderExecutorService.isPresent()) {
    MoreExecutors.shutdownAndAwaitTermination(
      mailSenderExecutorService.get(),
      1,
      TimeUnit.SECONDS
    );
  }
}
 
Example 7
Source File: TlsTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@After
public void tearDown() {
  if (server != null) {
    server.shutdown();
  }
  if (channel != null) {
    channel.shutdown();
  }
  MoreExecutors.shutdownAndAwaitTermination(executor, 5, TimeUnit.SECONDS);
}
 
Example 8
Source File: CompletableAsync.java    From helloiot with GNU General Public License v3.0 4 votes vote down vote up
public static void shutdown() {       
    MoreExecutors.shutdownAndAwaitTermination(service, 60, TimeUnit.SECONDS);
}
 
Example 9
Source File: Issue30Test.java    From caffeine with Apache License 2.0 4 votes vote down vote up
@AfterClass
public void afterClass() {
  MoreExecutors.shutdownAndAwaitTermination(executor, 1, TimeUnit.MINUTES);
}
 
Example 10
Source File: DexFileMerger.java    From bazel with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
static void buildMergedDexFiles(Options options) throws IOException {
  ListeningExecutorService executor;
  checkArgument(!options.inputArchives.isEmpty(), "Need at least one --input");
  checkArgument(
      options.mainDexListFile == null || options.inputArchives.size() == 1,
      "--main-dex-list only supported with exactly one --input, use DexFileSplitter for more");
  if (options.multidexMode.isMultidexAllowed()) {
    executor = createThreadPool();
  } else {
    checkArgument(
        options.mainDexListFile == null,
        "--main-dex-list is only supported with multidex enabled, but mode is: %s",
        options.multidexMode);
    checkArgument(
        !options.minimalMainDex,
        "--minimal-main-dex is only supported with multidex enabled, but mode is: %s",
        options.multidexMode);
    // We'll only ever merge and write one dex file, so multi-threading is pointless.
    executor = MoreExecutors.newDirectExecutorService();
  }

  ImmutableSet<String> classesInMainDex = options.mainDexListFile != null
      ? ImmutableSet.copyOf(Files.readAllLines(options.mainDexListFile, UTF_8))
      : null;
  PrintStream originalStdOut = System.out;
  try (DexFileAggregator out = createDexFileAggregator(options, executor)) {
    if (!options.verbose) {
      // com.android.dx.merge.DexMerger prints status information to System.out that we silence
      // here unless it was explicitly requested.  (It also prints debug info to DxContext.out,
      // which we populate accordingly below.)
      System.setOut(Dexing.nullout);
    }

    LinkedHashSet<String> seen = new LinkedHashSet<>();
    for (Path inputArchive : options.inputArchives) {
      // Simply merge files from inputs in order.  Doing that with a main dex list doesn't work,
      // but we rule out more than one input with a main dex list above.
      try (ZipFile zip = new ZipFile(inputArchive.toFile())) {
        ArrayList<ZipEntry> dexFiles = filesToProcess(zip);
        if (classesInMainDex == null) {
          processDexFiles(zip, dexFiles, seen, out);
        } else {
          // To honor --main_dex_list make two passes:
          // 1. process only the classes listed in the given file
          // 2. process the remaining files
          Predicate<ZipEntry> mainDexFilter =
              ZipEntryPredicates.classFileFilter(classesInMainDex);
          processDexFiles(zip, Iterables.filter(dexFiles, mainDexFilter), seen, out);
          // Fail if main_dex_list is too big, following dx's example
          checkState(out.getDexFilesWritten() == 0, "Too many classes listed in main dex list "
              + "file %s, main dex capacity exceeded", options.mainDexListFile);
          if (options.minimalMainDex) {
            out.flush(); // Start new .dex file if requested
          }
          processDexFiles(
              zip, Iterables.filter(dexFiles, Predicates.not(mainDexFilter)), seen, out);
        }
      }
    }
  } finally {
    // Kill threads in the pool so we don't hang
    MoreExecutors.shutdownAndAwaitTermination(executor, 1, SECONDS);
    System.setOut(originalStdOut);
  }
}
 
Example 11
Source File: ThreadPoolUtil.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * @see gracefulShutdown
 */
public static boolean gracefulShutdown( ExecutorService threadPool, int shutdownTimeout,
		TimeUnit timeUnit) {
	return threadPool == null || MoreExecutors.shutdownAndAwaitTermination(threadPool, shutdownTimeout, timeUnit);
}
 
Example 12
Source File: BlackBoxTestEnvironment.java    From bazel with Apache License 2.0 4 votes vote down vote up
/**
 * This method must be called when the test group execution is finished, for example, from
 * &#64;AfterClass method.
 */
public final void dispose() {
  Preconditions.checkNotNull(executorService);
  MoreExecutors.shutdownAndAwaitTermination(executorService, 1, TimeUnit.SECONDS);
  executorService = null;
}
 
Example 13
Source File: LSPExecutorService.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/** An orderly shutdown of this executor service. */
public synchronized void shutdown() {
	MoreExecutors.shutdownAndAwaitTermination(delegate, 2500, TimeUnit.MILLISECONDS);
	cancelAll();
}
 
Example 14
Source File: ProjectStatePersister.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Close this persister and wait for pending write operations to complete.
 */
public void close() {
	MoreExecutors.shutdownAndAwaitTermination(writer, 5, TimeUnit.SECONDS);
}
 
Example 15
Source File: ThreadPoolUtil.java    From vjtools with Apache License 2.0 4 votes vote down vote up
/**
 * @see gracefulShutdown
 */
public static boolean gracefulShutdown(@Nullable ExecutorService threadPool, int shutdownTimeout,
		TimeUnit timeUnit) {
	return threadPool == null || MoreExecutors.shutdownAndAwaitTermination(threadPool, shutdownTimeout, timeUnit);
}
 
Example 16
Source File: StateManagerImpl.java    From connector-sdk with Apache License 2.0 4 votes vote down vote up
@Override
public void close() throws IOException {
  MoreExecutors.shutdownAndAwaitTermination(callbackExecutor, 5, TimeUnit.MINUTES);
  isRunning.set(false);
}
 
Example 17
Source File: ProcessRunnerTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
@AfterClass
public static void tearDownExecutor() {
  MoreExecutors.shutdownAndAwaitTermination(executorService, 5, TimeUnit.SECONDS);
}
 
Example 18
Source File: MoreFuturesTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
@After
public final void shutdownExecutor() throws Exception  {
  MoreExecutors.shutdownAndAwaitTermination(executorService, TestUtils.WAIT_TIMEOUT_SECONDS,
      TimeUnit.SECONDS);

}
 
Example 19
Source File: AsyncScheduler.java    From FastLogin with MIT License 4 votes vote down vote up
public void shutdown() {
    MoreExecutors.shutdownAndAwaitTermination(processingPool, 1, TimeUnit.MINUTES);
    //MoreExecutors.shutdownAndAwaitTermination(databaseExecutor, 1, TimeUnit.MINUTES);
}
 
Example 20
Source File: ThreadPoolUtil.java    From vjtools with Apache License 2.0 2 votes vote down vote up
/**
 * 按照ExecutorService JavaDoc示例代码编写的Graceful Shutdown方法.
 * 
 * 先使用shutdown, 停止接收新任务并尝试完成所有已存在任务.
 * 
 * 如果1/2超时时间后, 则调用shutdownNow,取消在workQueue中Pending的任务,并中断所有阻塞函数.
 * 
 * 如果1/2超时仍然超時,則強制退出.
 * 
 * 另对在shutdown时线程本身被调用中断做了处理.
 * 
 * 返回线程最后是否被中断.
 * 
 * 使用了Guava的工具类
 * @see MoreExecutors#shutdownAndAwaitTermination(ExecutorService, long, TimeUnit)
 */
public static boolean gracefulShutdown(@Nullable ExecutorService threadPool, int shutdownTimeoutMills) {
	return threadPool == null
			|| MoreExecutors.shutdownAndAwaitTermination(threadPool, shutdownTimeoutMills, TimeUnit.MILLISECONDS);
}