List of usage examples for java.util.concurrent ExecutorService shutdown
void shutdown();
From source file:com.stimulus.archiva.incoming.IAPRunnable.java
public void shutdownAndAwaitTermination(ExecutorService pool, String job) { pool.shutdown(); try {/*from ww w .j a va 2 s. com*/ logger.debug("awaiting termination of " + job); if (!pool.awaitTermination(DEAD_PERIOD, TimeUnit.MILLISECONDS)) { logger.debug("awaiting " + job + " did not terminate"); pool.shutdownNow(); if (!pool.awaitTermination(60, TimeUnit.SECONDS)) logger.debug("awaiting " + job + " still did not terminate"); } else { logger.debug("awaiting " + job + " terminated"); } } catch (InterruptedException ie) { logger.debug("awaiting " + job + " were interrupted. shutting thread pool down immediately."); pool.shutdownNow(); Thread.currentThread().interrupt(); } catch (Throwable e) { logger.debug("awaiting " + job + " were interrupted:" + e.getMessage()); pool.shutdownNow(); Thread.currentThread().interrupt(); } }
From source file:com.vaushell.superpipes.tools.http.ImageExtractor.java
/** * Return the biggest image URI of this webpage. * * @param rootURI Webpage URI//from w ww. j av a 2 s. c o m * @return Biggest image * @throws IOException */ public BufferedImage extractBiggest(final URI rootURI) throws IOException { final List<URI> imagesURIs = new ArrayList<>(); HttpEntity responseEntity = null; try { // Exec request final HttpGet get = new HttpGet(rootURI); try (final CloseableHttpResponse response = client.execute(get)) { final StatusLine sl = response.getStatusLine(); if (sl.getStatusCode() != 200) { throw new IOException(sl.getReasonPhrase()); } responseEntity = response.getEntity(); try (final InputStream is = responseEntity.getContent()) { final Document doc = Jsoup.parse(is, "UTF-8", rootURI.toString()); final Elements elts = doc.select("img"); if (elts != null) { for (final Element elt : elts) { final String src = elt.attr("src"); if (src != null && !src.isEmpty()) { try { imagesURIs.add(rootURI.resolve(src)); } catch (final IllegalArgumentException ex) { // Ignore wrong encoded URI } } } } } } } finally { if (responseEntity != null) { EntityUtils.consume(responseEntity); } } final BufferedImage[] images = new BufferedImage[imagesURIs.size()]; final ExecutorService service = Executors.newCachedThreadPool(); for (int i = 0; i < imagesURIs.size(); ++i) { final int num = i; service.execute(new Runnable() { @Override public void run() { try { images[num] = HTTPhelper.loadPicture(client, imagesURIs.get(num)); } catch (final IOException ex) { images[num] = null; } } }); } service.shutdown(); try { service.awaitTermination(1L, TimeUnit.DAYS); } catch (final InterruptedException ex) { // Ignore } BufferedImage biggest = null; int biggestSize = Integer.MIN_VALUE; for (int i = 0; i < imagesURIs.size(); ++i) { if (images[i] != null) { final int actualSize = images[i].getWidth() * images[i].getHeight(); if (actualSize > biggestSize) { biggest = images[i]; biggestSize = actualSize; } } } return biggest; }
From source file:com.serotonin.bacnet4j.LocalDevice.java
public synchronized void terminate() { transport.terminate();/*from ww w .j a va 2 s. co m*/ initialized = false; if (ownsExecutorService) { ExecutorService temp = executorService; executorService = null; if (temp != null) { temp.shutdown(); try { temp.awaitTermination(3, TimeUnit.SECONDS); } catch (InterruptedException e) { // no op } } } }
From source file:com.example.AzureADAuthenticationFilter.java
private AuthenticationResult getAccessTokenFromRefreshToken(String refreshToken, String currentUri) throws Throwable { AuthenticationContext context = null; AuthenticationResult result = null;//from w w w . j av a 2 s. c o m ExecutorService service = null; try { service = Executors.newFixedThreadPool(1); context = new AuthenticationContext(authority + tenant + "/", true, service); Future<AuthenticationResult> future = context.acquireTokenByRefreshToken(refreshToken, new ClientCredential(clientId, clientSecret), null, null); result = future.get(); } catch (ExecutionException e) { throw e.getCause(); } finally { service.shutdown(); } if (result == null) { throw new ServiceUnavailableException("authentication result was null"); } return result; }
From source file:com.graphhopper.jsprit.io.algorithm.VehicleRoutingAlgorithms.java
private static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, XMLConfiguration config, int nuOfThreads, final SolutionCostCalculator solutionCostCalculator, final StateManager stateManager, ConstraintManager constraintManager, boolean addDefaultCostCalculators, boolean addCoreConstraints) { // map to store constructed modules TypedMap definedClasses = new TypedMap(); // algorithm listeners Set<PrioritizedVRAListener> algorithmListeners = new HashSet<PrioritizedVRAListener>(); // insertion listeners List<InsertionListener> insertionListeners = new ArrayList<InsertionListener>(); //threading// w ww. java2s. c o m final ExecutorService executorService; if (nuOfThreads > 0) { log.debug("setup executor-service with " + nuOfThreads + " threads"); executorService = Executors.newFixedThreadPool(nuOfThreads); algorithmListeners.add(new PrioritizedVRAListener(Priority.LOW, new AlgorithmEndsListener() { @Override public void informAlgorithmEnds(VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) { log.debug("shutdown executor-service"); executorService.shutdown(); } })); Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread arg0, Throwable arg1) { System.err.println(arg1.toString()); } }); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { if (!executorService.isShutdown()) { System.err.println("shutdowHook shuts down executorService"); executorService.shutdown(); } } }); } else executorService = null; //create fleetmanager final VehicleFleetManager vehicleFleetManager = createFleetManager(vrp); String switchString = config.getString("construction.insertion.allowVehicleSwitch"); final boolean switchAllowed; if (switchString != null) { switchAllowed = Boolean.parseBoolean(switchString); } else switchAllowed = true; ActivityTimeTracker.ActivityPolicy activityPolicy; if (stateManager.timeWindowUpdateIsActivated()) { UpdateVehicleDependentPracticalTimeWindows timeWindowUpdater = new UpdateVehicleDependentPracticalTimeWindows( stateManager, vrp.getTransportCosts(), vrp.getActivityCosts()); timeWindowUpdater .setVehiclesToUpdate(new UpdateVehicleDependentPracticalTimeWindows.VehiclesToUpdate() { Map<VehicleTypeKey, Vehicle> uniqueTypes = new HashMap<VehicleTypeKey, Vehicle>(); @Override public Collection<Vehicle> get(VehicleRoute vehicleRoute) { if (uniqueTypes.isEmpty()) { for (Vehicle v : vrp.getVehicles()) { if (!uniqueTypes.containsKey(v.getVehicleTypeIdentifier())) { uniqueTypes.put(v.getVehicleTypeIdentifier(), v); } } } Collection<Vehicle> vehicles = new ArrayList<Vehicle>(); vehicles.addAll(uniqueTypes.values()); return vehicles; } }); stateManager.addStateUpdater(timeWindowUpdater); activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_TIME_WINDOW_OPENS; } else { activityPolicy = ActivityTimeTracker.ActivityPolicy.AS_SOON_AS_ARRIVED; } stateManager.addStateUpdater( new UpdateActivityTimes(vrp.getTransportCosts(), activityPolicy, vrp.getActivityCosts())); stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager, activityPolicy)); final SolutionCostCalculator costCalculator; if (solutionCostCalculator == null) costCalculator = getDefaultCostCalculator(stateManager); else costCalculator = solutionCostCalculator; PrettyAlgorithmBuilder prettyAlgorithmBuilder = PrettyAlgorithmBuilder.newInstance(vrp, vehicleFleetManager, stateManager, constraintManager); if (addCoreConstraints) prettyAlgorithmBuilder.addCoreStateAndConstraintStuff(); //construct initial solution creator final InsertionStrategy initialInsertionStrategy = createInitialSolution(config, vrp, vehicleFleetManager, stateManager, algorithmListeners, definedClasses, executorService, nuOfThreads, costCalculator, constraintManager, addDefaultCostCalculators); if (initialInsertionStrategy != null) prettyAlgorithmBuilder.constructInitialSolutionWith(initialInsertionStrategy, costCalculator); //construct algorithm, i.e. search-strategies and its modules int solutionMemory = config.getInt("strategy.memory"); List<HierarchicalConfiguration> strategyConfigs = config .configurationsAt("strategy.searchStrategies.searchStrategy"); for (HierarchicalConfiguration strategyConfig : strategyConfigs) { String name = getName(strategyConfig); SolutionAcceptor acceptor = getAcceptor(strategyConfig, vrp, algorithmListeners, definedClasses, solutionMemory); SolutionSelector selector = getSelector(strategyConfig, vrp, algorithmListeners, definedClasses); SearchStrategy strategy = new SearchStrategy(name, selector, acceptor, costCalculator); strategy.setName(name); List<HierarchicalConfiguration> modulesConfig = strategyConfig.configurationsAt("modules.module"); for (HierarchicalConfiguration moduleConfig : modulesConfig) { SearchStrategyModule module = buildModule(moduleConfig, vrp, vehicleFleetManager, stateManager, algorithmListeners, definedClasses, executorService, nuOfThreads, constraintManager, addDefaultCostCalculators); strategy.addModule(module); } prettyAlgorithmBuilder.withStrategy(strategy, strategyConfig.getDouble("probability")); } //construct algorithm VehicleRoutingAlgorithm metaAlgorithm = prettyAlgorithmBuilder.build(); int maxIterations = getMaxIterations(config); if (maxIterations > -1) metaAlgorithm.setMaxIterations(maxIterations); //define prematureBreak PrematureAlgorithmTermination prematureAlgorithmTermination = getPrematureTermination(config, algorithmListeners); if (prematureAlgorithmTermination != null) metaAlgorithm.setPrematureAlgorithmTermination(prematureAlgorithmTermination); else { List<HierarchicalConfiguration> terminationCriteria = config .configurationsAt("terminationCriteria.termination"); for (HierarchicalConfiguration terminationConfig : terminationCriteria) { PrematureAlgorithmTermination termination = getTerminationCriterion(terminationConfig, algorithmListeners); if (termination != null) metaAlgorithm.addTerminationCriterion(termination); } } for (PrioritizedVRAListener l : algorithmListeners) { metaAlgorithm.getAlgorithmListeners().add(l); } return metaAlgorithm; }
From source file:kmi.taa.core.Crawler.java
public void crawlAll(TreeMap<Integer, String> targetUrls, String service, String proxy, String otfile) { SortedSet<Integer> results = Collections.synchronizedSortedSet(new TreeSet<Integer>()); ExecutorService pool = Executors.newFixedThreadPool(100); int howManyUrls = targetUrls.size(); System.out.println("total " + howManyUrls + " to be processed"); List<String> output = Collections.synchronizedList(new ArrayList<String>()); for (Integer targetId : targetUrls.navigableKeySet()) { String uri = targetUrls.get(targetId); pool.execute(new Explorer(targetId, uri, service, proxy, results, otfile, output)); }//from ww w . ja v a2 s . c o m pool.shutdown(); while (results.size() < howManyUrls) { System.out.println("already processed " + results.size() + " subject links"); try { Thread.sleep(1000); } catch (InterruptedException e) { log.error("crawlAll error", e); } } resultToFile(output, otfile); System.out.println("already processed " + results.size() + " subject links"); }
From source file:com.linkedin.pinot.integration.tests.MetadataAndDictionaryAggregationPlanClusterIntegrationTest.java
private void createAndUploadSegments(List<File> avroFiles, String tableName, boolean createStarTreeIndex, List<String> rawIndexColumns, Schema pinotSchema) throws Exception { TestUtils.ensureDirectoriesExistAndEmpty(_segmentDir, _tarDir); ExecutorService executor = Executors.newCachedThreadPool(); ClusterIntegrationTestUtils.buildSegmentsFromAvro(avroFiles, 0, _segmentDir, _tarDir, tableName, createStarTreeIndex, rawIndexColumns, pinotSchema, executor); executor.shutdown(); executor.awaitTermination(10, TimeUnit.MINUTES); uploadSegments(_tarDir);//from w ww .ja va 2 s .c o m }
From source file:com.amazonaws.services.cloudtrail.processinglibrary.AWSCloudTrailProcessingExecutor.java
/** * Helper function to gracefully stop an {@link ExecutorService}. * * @param threadPool the thread pool to stop. */// w w w . j ava2 s . c om private void stopThreadPool(ExecutorService threadPool) { LibraryUtils.checkCondition(threadPool == null, "Thread pool is null when calling stop"); if (threadPool.isShutdown()) { logger.debug(threadPool.toString() + " is already stopped."); } else { logger.debug(threadPool.toString() + " is about to shutdown."); threadPool.shutdown(); // Shutdown thread pool try { // Wait for shutdown threadPool.awaitTermination(this.config.getThreadTerminationDelaySeconds(), TimeUnit.SECONDS); } catch (InterruptedException e) { logger.debug("Wait thread pool termination is interrupted."); } if (!threadPool.isShutdown()) { // ShutdownNow after waiting logger.debug(threadPool.toString() + " is force to shutdown now."); threadPool.shutdownNow(); } logger.debug(threadPool.toString() + " is stopped."); } }
From source file:io.wcm.caravan.pipeline.impl.JsonPipelineMultipleSubscriptionsTest.java
@Test public void subscribeConcurrentlyToPlainPipelineOutputs() throws InterruptedException, JSONException { firstStep = newPipelineWithResponseBody("{id:123}"); // use a synchronized set to collect the pipeline output from multiple threads Set<JsonPipelineOutput> distinctOutputs = Collections.synchronizedSet(new HashSet<JsonPipelineOutput>()); // create multiple simultaneous threads that subscribe to the same pipeline output // and use a CountDownLatch to delay the subscription until all threads have been started ExecutorService executorService = Executors.newCachedThreadPool(); CountDownLatch countDown = new CountDownLatch(100); while (countDown.getCount() > 0) { executorService.submit(() -> { countDown.await();//www . j a v a2 s . co m distinctOutputs.add(firstStep.getOutput().toBlocking().single()); return null; // this is required for the lambda to be considered a Callable<Void> and therefore be allowed to throw exceptions }); countDown.countDown(); } executorService.shutdown(); executorService.awaitTermination(1, TimeUnit.MINUTES); // ensure all threads received the same JsonPipelineOutput instance with the expected JSON output assertEquals(1, distinctOutputs.size()); JSONAssert.assertEquals("{id: 123}", firstStep.getStringOutput().toBlocking().first(), JSONCompareMode.STRICT); }
From source file:com.liveramp.hank.partition_server.UpdateManager.java
@Override public void update() throws IOException { HankTimer timer = new HankTimer(); try {//from w w w. ja v a 2 s. com // Delete unknown files deleteUnknownFiles(); // Perform update Semaphore concurrentUpdatesSemaphore = new Semaphore(configurator.getNumConcurrentUpdates()); List<Throwable> encounteredThrowables = new ArrayList<Throwable>(); PartitionUpdateTaskStatisticsAggregator partitionUpdateTaskStatisticsAggregator = new PartitionUpdateTaskStatisticsAggregator(); Map<String, Queue<PartitionUpdateTask>> dataDirectoryToUpdateTasks = new HashMap<String, Queue<PartitionUpdateTask>>(); List<PartitionUpdateTask> allUpdateTasks = buildPartitionUpdateTasks( partitionUpdateTaskStatisticsAggregator, encounteredThrowables); // Build and organize update tasks per data directory for (PartitionUpdateTask updateTask : allUpdateTasks) { String dataDirectory = updateTask.getDataDirectory(); Queue<PartitionUpdateTask> updateTasks = dataDirectoryToUpdateTasks.get(dataDirectory); if (updateTasks == null) { updateTasks = new LinkedList<PartitionUpdateTask>(); dataDirectoryToUpdateTasks.put(dataDirectory, updateTasks); } updateTasks.add(updateTask); } // Logging LOG.info("Number of update tasks: " + allUpdateTasks.size()); for (Map.Entry<String, Queue<PartitionUpdateTask>> entry : dataDirectoryToUpdateTasks.entrySet()) { LOG.info("Number of update tasks scheduled in " + entry.getKey() + ": " + entry.getValue().size()); } // Build executor services Map<String, ExecutorService> dataDirectoryToExecutorService = new HashMap<String, ExecutorService>(); for (String dataDirectory : dataDirectoryToUpdateTasks.keySet()) { dataDirectoryToExecutorService.put(dataDirectory, new UpdateThreadPoolExecutor(configurator.getMaxConcurrentUpdatesPerDataDirectory(), new UpdaterThreadFactory(dataDirectory), concurrentUpdatesSemaphore)); } LOG.info("Submitting update tasks for " + dataDirectoryToUpdateTasks.size() + " directories."); // Execute tasks. We execute one task for each data directory and loop around so that the tasks // attempt to acquire the semaphore in a reasonable order. boolean remaining = true; while (remaining) { remaining = false; for (Map.Entry<String, Queue<PartitionUpdateTask>> entry : dataDirectoryToUpdateTasks.entrySet()) { // Pop next task Queue<PartitionUpdateTask> partitionUpdateTasks = entry.getValue(); if (!partitionUpdateTasks.isEmpty()) { PartitionUpdateTask partitionUpdateTask = partitionUpdateTasks.remove(); // Execute task dataDirectoryToExecutorService.get(entry.getKey()).execute(partitionUpdateTask); } if (!partitionUpdateTasks.isEmpty()) { remaining = true; } } } LOG.info("All update tasks submitted, shutting down executor services"); // Shutdown executors for (ExecutorService executorService : dataDirectoryToExecutorService.values()) { executorService.shutdown(); } LOG.info("Waiting for executors to finish."); // Wait for executors to finish for (Map.Entry<String, ExecutorService> entry : dataDirectoryToExecutorService.entrySet()) { String directory = entry.getKey(); ExecutorService executorService = entry.getValue(); boolean keepWaiting = true; while (keepWaiting) { try { LOG.info("Waiting for updates to complete on data directory: " + directory); boolean terminated = executorService.awaitTermination( UPDATE_EXECUTOR_TERMINATION_CHECK_TIMEOUT_VALUE, UPDATE_EXECUTOR_TERMINATION_CHECK_TIMEOUT_UNIT); if (terminated) { // We finished executing all tasks // Otherwise, timeout elapsed and current thread was not interrupted. Keep waiting. LOG.info("Finished updates for directory: " + directory); keepWaiting = false; } // Record update ETA Hosts.setUpdateETA(host, partitionUpdateTaskStatisticsAggregator.computeETA()); } catch (InterruptedException e) { // Received interruption (stop request). // Swallow the interrupted state and ask the executor to shutdown immediately. Also, keep waiting. LOG.info( "The update manager was interrupted. Stopping the update process (stop executing new partition update tasks" + " and wait for those that were running to finish)."); // Shutdown all executors for (ExecutorService otherExecutorService : dataDirectoryToExecutorService.values()) { otherExecutorService.shutdownNow(); } // Record failed update exception (we need to keep waiting) encounteredThrowables.add( new IOException("Failed to complete update: update interruption was requested.")); } } } LOG.info("All executors have finished updates"); // Shutdown all executors for (ExecutorService executorService : dataDirectoryToExecutorService.values()) { executorService.shutdownNow(); } LOG.info("Finished with " + encounteredThrowables.size() + " errors."); // Detect failures if (!encounteredThrowables.isEmpty()) { LOG.error(String.format("%d exceptions encountered while running partition update tasks:", encounteredThrowables.size())); int i = 0; for (Throwable t : encounteredThrowables) { LOG.error(String.format("Exception %d/%d:", ++i, encounteredThrowables.size()), t); } throw new IOException(String.format( "Failed to complete update: %d exceptions encountered while running partition update tasks.", encounteredThrowables.size())); } // Garbage collect useless host domains garbageCollectHostDomains(host); // Log statistics partitionUpdateTaskStatisticsAggregator.logStats(); } catch (IOException e) { LOG.info("Update failed and took " + FormatUtils.formatSecondsDuration(timer.getDurationMs() / 1000)); throw e; } LOG.info("Update succeeded and took " + FormatUtils.formatSecondsDuration(timer.getDurationMs() / 1000)); }