List of usage examples for java.util.concurrent ExecutorService awaitTermination
boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
From source file:org.springframework.integration.monitor.IntegrationMBeanExporter.java
private void waitForExecutors(List<ExecutorService> executorServices) { for (ExecutorService executorService : executorServices) { try {// w w w . ja v a2 s . co m if (!executorService.awaitTermination(this.shutdownDeadline - System.currentTimeMillis(), TimeUnit.MILLISECONDS)) { logger.error("Executor service " + executorService + " failed to terminate"); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); logger.error("Interrupted while shutting down executor service " + executorService); throw new MessagingException("Interrupted while shutting down", e); } if (System.currentTimeMillis() > this.shutdownDeadline) { logger.error("Timed out before waiting for all executor services"); } } }
From source file:com.yahoo.yqlplus.engine.tools.YQLPlusRun.java
@SuppressWarnings("unchecked") public int run(CommandLine command) throws Exception { String script = null;//ww w.j av a 2 s. c om String filename = null; if (command.hasOption("command")) { script = command.getOptionValue("command"); filename = "<command line>"; } List<String> scriptAndArgs = (List<String>) command.getArgList(); if (filename == null && scriptAndArgs.size() < 1) { System.err.println("No script specified."); return -1; } else if (script == null) { filename = scriptAndArgs.get(0); Path scriptPath = Paths.get(filename); if (!Files.isRegularFile(scriptPath)) { System.err.println(scriptPath + " is not a file."); return -1; } script = Charsets.UTF_8.decode(ByteBuffer.wrap(Files.readAllBytes(scriptPath))).toString(); } List<String> paths = Lists.newArrayList(); if (command.hasOption("path")) { paths.addAll(Arrays.asList(command.getOptionValues("path"))); } // TODO: this isn't going to be very interesting without some sources Injector injector = Guice.createInjector(new JavaEngineModule()); YQLPlusCompiler compiler = injector.getInstance(YQLPlusCompiler.class); CompiledProgram program = compiler.compile(script); if (command.hasOption("source")) { program.dump(System.out); return 0; } // TODO: read command line arguments to pass to program ExecutorService outputThreads = Executors.newSingleThreadExecutor(); ProgramResult result = program.run(Maps.<String, Object>newHashMap(), true); for (String name : result.getResultNames()) { final ListenableFuture<YQLResultSet> future = result.getResult(name); future.addListener(new Runnable() { @Override public void run() { try { YQLResultSet resultSet = future.get(); System.out.println(new String((byte[]) resultSet.getResult())); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } } }, outputThreads); } Future<TraceRequest> done = result.getEnd(); try { done.get(10000L, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException | TimeoutException e) { e.printStackTrace(); } outputThreads.awaitTermination(1L, TimeUnit.SECONDS); return 0; }
From source file:pl.nask.hsn2.service.urlfollower.WebClientWorker.java
private void closeExecutorWithJSDisabled(ExecutorService ex) { wc.getOptions().setJavaScriptEnabled(false); try {/*from w ww . j av a2 s. c o m*/ ex.awaitTermination(TERMINATION_TIMEOUT, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { //ignore } wc.getOptions().setJavaScriptEnabled(true); }
From source file:org.apache.helix.messaging.handling.HelixTaskExecutor.java
private void shutdownAndAwaitTermination(ExecutorService pool) { LOG.info("Shutting down pool: " + pool); pool.shutdown(); // Disable new tasks from being submitted try {//w w w. ja va 2 s . co m // Wait a while for existing tasks to terminate if (!pool.awaitTermination(200, TimeUnit.MILLISECONDS)) { List<Runnable> waitingTasks = pool.shutdownNow(); // Cancel currently executing tasks LOG.info("Tasks that never commenced execution: " + waitingTasks); // Wait a while for tasks to respond to being cancelled if (!pool.awaitTermination(200, TimeUnit.MILLISECONDS)) { LOG.error("Pool did not fully terminate in 200ms. pool: " + pool); } } } catch (InterruptedException ie) { // (Re-)Cancel if current thread also interrupted LOG.error("Interruped when waiting for shutdown pool: " + pool, ie); pool.shutdownNow(); // Preserve interrupt status Thread.currentThread().interrupt(); } }
From source file:com.relativitas.maven.plugins.formatter.FormatterMojo.java
private void formatAll(final ResultCollector rc, final Properties hashCache) throws MojoExecutionException { final ExecutorService service; if (isCleanBuild) { //This seems to be the best ratio time gained/threads //More threads only reduce a little more. service = Executors.newFixedThreadPool(2); } else {/*from ww w . j av a 2 s. com*/ service = Executors.newFixedThreadPool(1); } formatSourceDirectory(service, rc, hashCache, sourceDirectory); formatSourceDirectory(service, rc, hashCache, testSourceDirectory); service.shutdown(); try { //If we wait 10 minutes,something is really wrong or you have Hundreds of thousands of files.This is bad! service.awaitTermination(10, TimeUnit.MINUTES); } catch (InterruptedException e) { } }
From source file:com.blacklocus.jres.request.index.JresUpdateDocumentScriptTest.java
@Test public void testRetryOnConflict() throws InterruptedException { final String index = "JresUpdateDocumentScriptTest.testRetryOnConflict".toLowerCase(); final String type = "test"; final String id = "warzone"; final AtomicInteger total = new AtomicInteger(); final AtomicReference<String> error = new AtomicReference<String>(); final Random random = new Random(System.currentTimeMillis()); final int numThreads = 16, numIterations = 100; ExecutorService x = Executors.newFixedThreadPool(numThreads); for (int i = 0; i < numThreads; i++) { x.submit(new Runnable() { @Override//from w w w .j ava2 s. c o m public void run() { try { for (int j = 0; j < numIterations; j++) { int increment = random.nextInt(5); total.addAndGet(increment); JresUpdateDocumentScript req = new JresUpdateDocumentScript(index, type, id, "ctx._source.value += increment", ImmutableMap.of("increment", increment), ImmutableMap.of("value", increment), null); req.setRetryOnConflict(numIterations * 10); jres.quest(req); } } catch (Exception e) { error.set(e.getMessage()); } } }); } x.shutdown(); x.awaitTermination(1, TimeUnit.MINUTES); Assert.assertNull("With so many retries, all of these should have gotten through without conflict error", error.get()); jres.quest(new JresRefresh(index)); JresGetDocumentReply getReply = jres.quest(new JresGetDocument(index, type, id)); Map<String, Integer> doc = getReply.getSourceAsType(new TypeReference<Map<String, Integer>>() { }); Assert.assertEquals("All increments should have gotten committed", (Object) total.get(), doc.get("value")); Assert.assertEquals("Should have been numThreads * numIterations versions committed", (Object) (numThreads * numIterations), getReply.getVersion()); }
From source file:co.pugo.convert.ConvertServlet.java
/** * download imageData and encode it base64 * @param imageLinks set of image links extracted with extractImageLinks() * @return map, key = imageLink, value = base64 encoded image *//*from ww w . j a va2 s . c o m*/ private HashMap<String, String> downloadImageData(Set<String> imageLinks) { HashMap<String, String> imageData = new HashMap<>(); ExecutorService service = Executors.newCachedThreadPool(); for (final String imageLink : imageLinks) { RunnableFuture<byte[]> future = new FutureTask<>(new Callable<byte[]>() { @Override public byte[] call() { try { URL srcUrl = new URL(imageLink); URLConnection urlConnection = srcUrl.openConnection(); return IOUtils.toByteArray(urlConnection.getInputStream()); } catch (IOException e) { LOG.severe(e.getMessage()); return null; } } }); service.execute(future); try { imageData.put(imageLink, Base64.encodeBase64String(future.get())); } catch (InterruptedException | ExecutionException e) { LOG.severe(e.getMessage()); } } service.shutdown(); try { service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { LOG.severe(e.getMessage()); } return imageData; }
From source file:gobblin.configuration.StateTest.java
/** * This test checks that state object is thread safe. We run 2 threads, one of them continuously adds and removes key/values * to the state and other thread calls getProperties. *///from w w w .ja va 2 s .co m @Test public void testGetPropertiesThreadSafety() { try { final State state = new State(); for (int i = 0; i < 1000; i++) { state.setProp(Integer.toString(i), Integer.toString(i)); } ExecutorService executorService = Executors.newFixedThreadPool(2); executorService.submit(new Runnable() { @Override public void run() { for (int j = 0; j < 1000; j++) { for (int i = 0; i < 1000; i++) { try { state.removeProp(Integer.toString(i)); state.setProp(Integer.toString(i), Integer.toString(i)); } catch (Throwable t) { exceptions.add(t); } } } } }); executorService.submit(new Runnable() { @Override public void run() { for (int i = 0; i < 1000; i++) { try { state.getProperties().get(Integer.toString(i)); } catch (Throwable t) { exceptions.add(t); } } } }); executorService.shutdown(); if (!executorService.awaitTermination(100, TimeUnit.SECONDS)) { throw new RuntimeException("Executor service still running"); } } catch (Throwable t) { Assert.fail("Concurrency test failed", t); } if (!this.exceptions.isEmpty()) { Assert.fail("Concurrency test failed with first exception: " + ExceptionUtils.getFullStackTrace(this.exceptions.poll())); } }
From source file:com.netflix.genie.client.JobClientIntegrationTests.java
/** * Method to test submitting/killing a job. * * @throws Exception If there is any problem. *//* w w w. j a va 2s . c o m*/ @Test public void submitAndKillJob() throws Exception { createClusterAndCommandForTest(); final String jobId = UUID.randomUUID().toString(); final List<ClusterCriteria> clusterCriteriaList = Lists .newArrayList(new ClusterCriteria(Sets.newHashSet("laptop"))); final Set<String> commandCriteria = Sets.newHashSet("bash"); final String depFile1 = this.resourceLoader.getResource("/dep1").getFile().getAbsolutePath(); final Set<String> dependencies = Sets.newHashSet(depFile1); final String setUpFile = this.resourceLoader.getResource("/setupfile").getFile().getAbsolutePath(); final JobRequest jobRequest = new JobRequest.Builder(JOB_NAME, JOB_USER, JOB_VERSION, "-c 'echo HELLO WORLD!!!'", clusterCriteriaList, commandCriteria).withId(jobId) .withDisableLogArchival(true).withSetupFile(setUpFile).withDependencies(dependencies) .withDescription(JOB_DESCRIPTION).build(); final ExecutorService executors = Executors.newFixedThreadPool(2); final Future<String> jobFuture; try { jobFuture = executors.submit(() -> jobClient.submitJob(jobRequest)); executors.submit(() -> { boolean result = true; while (result) { try { jobClient.getJob(jobId); jobClient.killJob(jobId); Thread.sleep(1000); result = false; } catch (Exception ignored) { result = true; } } }); } finally { executors.shutdown(); executors.awaitTermination(Integer.MAX_VALUE, TimeUnit.HOURS); } final Job job = jobClient.getJob(jobId); Assert.assertEquals(jobId, jobFuture.get()); Assert.assertEquals(JobStatus.KILLED, job.getStatus()); }
From source file:com.streamsets.pipeline.stage.origin.kafka.TestKafkaSource.java
private void shutDownExecutorService(ExecutorService executorService) throws InterruptedException { executorService.shutdownNow();/*ww w .jav a 2 s. co m*/ if (!executorService.awaitTermination(5000, TimeUnit.MILLISECONDS)) { //If it cant be stopped then throw exception throw new RuntimeException("Could not shutdown Executor service"); } }