List of usage examples for com.google.common.util.concurrent MoreExecutors getExitingExecutorService
@Beta @GwtIncompatible("TODO") public static ExecutorService getExitingExecutorService(ThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit)
From source file:org.apache.abdera2.common.misc.MoreExecutors2.java
public static ExecutorService getExitingExecutor(int timeout, TimeUnit unit) { ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newCachedThreadPool(); return MoreExecutors.getExitingExecutorService(tpe, timeout, unit); }
From source file:org.apache.abdera2.common.misc.MoreExecutors2.java
public static ExecutorService getExitingExecutor(ThreadFactory factory, int timeout, TimeUnit unit) { ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newCachedThreadPool(factory); return MoreExecutors.getExitingExecutorService(tpe, timeout, unit); }
From source file:org.apache.abdera2.common.misc.MoreExecutors2.java
public static ExecutorService getExitingFixedExecutor(int n, int timeout, TimeUnit unit) { ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newFixedThreadPool(n); return MoreExecutors.getExitingExecutorService(tpe, timeout, unit); }
From source file:org.apache.abdera2.common.misc.MoreExecutors2.java
public static ExecutorService getExitingFixedExecutor(int n, ThreadFactory factory, int timeout, TimeUnit unit) {/*from w w w .ja v a 2 s. co m*/ ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newFixedThreadPool(n, factory); return MoreExecutors.getExitingExecutorService(tpe, timeout, unit); }
From source file:com.google.cloud.tools.appengine.experimental.internal.process.CliProcessManager.java
private CliProcessManager(Process process, OutputHandler outputHandler, StringResultConverter<T> stringResultConverter) { this.process = process; this.executor = MoreExecutors.listeningDecorator(MoreExecutors.getExitingExecutorService( (ThreadPoolExecutor) Executors.newFixedThreadPool(3), 2, TimeUnit.SECONDS)); this.outputHandler = outputHandler; this.stringResultConverter = stringResultConverter; }
From source file:org.apache.gobblin.metrics.reporter.EventReporter.java
public EventReporter(Builder builder) { super(builder.context, builder.name, builder.filter, builder.rateUnit, builder.durationUnit); this.closer = Closer.create(); this.immediateReportExecutor = MoreExecutors .getExitingExecutorService(/* w ww.j a va 2 s. c o m*/ (ThreadPoolExecutor) Executors.newFixedThreadPool(1, ExecutorsUtils.newThreadFactory(Optional.of(LOGGER), Optional.of("EventReporter-" + builder.name + "-%d"))), 5, TimeUnit.MINUTES); this.metricContext = builder.context; this.notificationTargetKey = builder.context.addNotificationTarget(new Function<Notification, Void>() { @Nullable @Override public Void apply(Notification notification) { notificationCallback(notification); return null; } }); this.reportingQueue = Queues.newLinkedBlockingQueue(QUEUE_CAPACITY); }
From source file:org.apache.gobblin.metrics.MetricContext.java
private synchronized ExecutorService getExecutorService() { if (!this.executorServiceOptional.isPresent()) { this.executorServiceOptional = Optional.of(MoreExecutors.getExitingExecutorService( (ThreadPoolExecutor) Executors.newCachedThreadPool(ExecutorsUtils .newThreadFactory(Optional.of(LOG), Optional.of("MetricContext-" + getName() + "-%d"))), 5, TimeUnit.MINUTES)); }//from w w w . j a v a 2s . c o m return this.executorServiceOptional.get(); }
From source file:com.facebook.buck.rules.CassandraArtifactCache.java
public CassandraArtifactCache(String hosts, int port, int timeoutSeconds, boolean doStore, BuckEventBus buckEventBus) throws ConnectionException { this.doStore = doStore; this.buckEventBus = Preconditions.checkNotNull(buckEventBus); this.numConnectionExceptionReports = new AtomicInteger(0); this.timeoutSeconds = timeoutSeconds; final AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder().forCluster(CLUSTER_NAME) .forKeyspace(KEYSPACE_NAME)/* w ww . j a v a 2s . co m*/ .withAstyanaxConfiguration(new AstyanaxConfigurationImpl().setCqlVersion("3.0.0") .setTargetCassandraVersion("1.2").setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)) .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl(POOL_NAME).setSeeds(hosts) .setPort(port).setMaxConnsPerHost(1)) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .buildKeyspace(ThriftFamilyFactory.getInstance()); ExecutorService connectionService = MoreExecutors.getExitingExecutorService( (ThreadPoolExecutor) Executors.newFixedThreadPool(1), 0, TimeUnit.SECONDS); this.keyspaceAndTtlFuture = connectionService.submit(new Callable<KeyspaceAndTtl>() { @Override public KeyspaceAndTtl call() throws ConnectionException { context.start(); Keyspace keyspace = context.getClient(); try { verifyMagic(keyspace); int ttl = getTtl(keyspace); return new KeyspaceAndTtl(keyspace, ttl); } catch (ConnectionException e) { reportConnectionFailure("Attempting to get keyspace and ttl from server.", e); throw e; } } }); this.futures = Sets .newSetFromMap(new ConcurrentHashMap<ListenableFuture<OperationResult<Void>>, Boolean>()); this.isWaitingToClose = new AtomicBoolean(false); this.isKilled = new AtomicBoolean(false); }
From source file:gobblin.util.ExecutorsUtils.java
/** * A utility method to parallelize loops. Applies the {@link Function} to every element in the {@link List} in * parallel by spawning threads. A list containing the result obtained by applying the function is returned. The * method is a blocking call and will wait for all the elements in the list to be processed or timeoutInSecs which * ever is earlier./*from w w w. j a v a2 s . co m*/ * <p> * <b>NOTE: The method is an all or none implementation. Meaning, if any of the thread fails, the method will throw an * {@link ExecutionException} even if other threads completed successfully</b> * </p> * * <ul> * <li>Uses a Fixed thread pool of size threadCount. * <li>Uses {@link #shutdownExecutorService(ExecutorService, Optional, long, TimeUnit)} to shutdown the executor * service * <li>All threads are daemon threads * </ul> * * @param list input list on which the function is applied in parallel * @param function to be applied on every element of the list * @param threadCount to be used to process the list * @param timeoutInSecs to wait for all the threads to complete * @param logger an {@link Optional} wrapping a {@link Logger} to be used during shutdown * * @return a list containing the result obtained by applying the function on each element of the input list in the * same order * * @throws IllegalArgumentException if input list or function is null * @throws ExecutionException <ul> * <li>if any computation threw an exception * <li>if any computation was cancelled * <li>if any thread was interrupted while waiting * <ul> */ public static <F, T> List<T> parallelize(final List<F> list, final Function<F, T> function, int threadCount, int timeoutInSecs, Optional<Logger> logger) throws ExecutionException { Preconditions.checkArgument(list != null, "Input list can not be null"); Preconditions.checkArgument(function != null, "Function can not be null"); final List<T> results = Lists.newArrayListWithCapacity(list.size()); List<Future<T>> futures = Lists.newArrayListWithCapacity(list.size()); ExecutorService executorService = MoreExecutors.getExitingExecutorService((ThreadPoolExecutor) Executors .newFixedThreadPool(threadCount, ExecutorsUtils.newThreadFactory(logger)), 2, TimeUnit.MINUTES); for (final F l : list) { futures.add(executorService.submit(new Callable<T>() { @Override public T call() throws Exception { return function.apply(l); } })); } ExecutorsUtils.shutdownExecutorService(executorService, logger, timeoutInSecs, TimeUnit.SECONDS); for (Future<T> future : futures) { try { results.add(future.get()); } catch (InterruptedException e) { throw new ExecutionException("Thread interrupted", e); } } return results; }