List of usage examples for java.util.concurrent ExecutorService awaitTermination
boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
From source file:gobblin.ingestion.google.webmaster.GoogleWebmasterDataFetcherImpl.java
/** * Get all pages in an async mode./* ww w .j a v a 2 s .co m*/ */ private Collection<String> getPages(String startDate, String endDate, List<Dimension> dimensions, ApiDimensionFilter countryFilter, Queue<Pair<String, FilterOperator>> toProcess) throws IOException { String country = GoogleWebmasterFilter.countryFilterToString(countryFilter); ConcurrentLinkedDeque<String> allPages = new ConcurrentLinkedDeque<>(); int r = 0; while (r <= RETRY) { ++r; log.info(String.format("Get pages at round %d with size %d.", r, toProcess.size())); ConcurrentLinkedDeque<Pair<String, FilterOperator>> nextRound = new ConcurrentLinkedDeque<>(); ExecutorService es = Executors.newFixedThreadPool(10, ExecutorsUtils .newDaemonThreadFactory(Optional.of(log), Optional.of(this.getClass().getSimpleName()))); while (!toProcess.isEmpty()) { submitJob(toProcess.poll(), countryFilter, startDate, endDate, dimensions, es, allPages, nextRound); } //wait for jobs to finish and start next round if necessary. try { es.shutdown(); boolean terminated = es.awaitTermination(5, TimeUnit.MINUTES); if (!terminated) { es.shutdownNow(); log.warn(String.format( "Timed out while getting all pages for country-%s at round %d. Next round now has size %d.", country, r, nextRound.size())); } } catch (InterruptedException e) { throw new RuntimeException(e); } if (nextRound.isEmpty()) { break; } toProcess = nextRound; } if (r == RETRY) { throw new RuntimeException(String.format( "Getting all pages reaches the maximum number of retires %d. Date range: %s ~ %s. Country: %s.", RETRY, startDate, endDate, country)); } return allPages; }
From source file:org.apache.hadoop.hbase.regionserver.TestRegionReplicas.java
@Test(timeout = 300000) public void testFlushAndCompactionsInPrimary() throws Exception { long runtime = 30 * 1000; // enable store file refreshing final int refreshPeriod = 100; // 100ms refresh is a lot HTU.getConfiguration().setInt("hbase.hstore.compactionThreshold", 3); HTU.getConfiguration().setInt(StorefileRefresherChore.REGIONSERVER_STOREFILE_REFRESH_PERIOD, refreshPeriod); // restart the region server so that it starts the refresher chore restartRegionServer();/* w w w.j av a2s.co m*/ final int startKey = 0, endKey = 1000; try { openRegion(HTU, getRS(), hriSecondary); //load some data to primary so that reader won't fail HTU.loadNumericRows(table, f, startKey, endKey); TestRegionServerNoMaster.flushRegion(HTU, hriPrimary); // ensure that chore is run Threads.sleep(2 * refreshPeriod); final AtomicBoolean running = new AtomicBoolean(true); @SuppressWarnings("unchecked") final AtomicReference<Exception>[] exceptions = new AtomicReference[3]; for (int i = 0; i < exceptions.length; i++) { exceptions[i] = new AtomicReference<Exception>(); } Runnable writer = new Runnable() { int key = startKey; @Override public void run() { try { while (running.get()) { byte[] data = Bytes.toBytes(String.valueOf(key)); Put put = new Put(data); put.add(f, null, data); table.put(put); key++; if (key == endKey) key = startKey; } } catch (Exception ex) { LOG.warn(ex); exceptions[0].compareAndSet(null, ex); } } }; Runnable flusherCompactor = new Runnable() { Random random = new Random(); @Override public void run() { try { while (running.get()) { // flush or compact if (random.nextBoolean()) { TestRegionServerNoMaster.flushRegion(HTU, hriPrimary); } else { HTU.compact(table.getName(), random.nextBoolean()); } } } catch (Exception ex) { LOG.warn(ex); exceptions[1].compareAndSet(null, ex); } } }; Runnable reader = new Runnable() { Random random = new Random(); @Override public void run() { try { while (running.get()) { // whether to do a close and open if (random.nextInt(10) == 0) { try { closeRegion(HTU, getRS(), hriSecondary); } catch (Exception ex) { LOG.warn("Failed closing the region " + hriSecondary + " " + StringUtils.stringifyException(ex)); exceptions[2].compareAndSet(null, ex); } try { openRegion(HTU, getRS(), hriSecondary); } catch (Exception ex) { LOG.warn("Failed opening the region " + hriSecondary + " " + StringUtils.stringifyException(ex)); exceptions[2].compareAndSet(null, ex); } } int key = random.nextInt(endKey - startKey) + startKey; assertGetRpc(hriSecondary, key, true); } } catch (Exception ex) { LOG.warn("Failed getting the value in the region " + hriSecondary + " " + StringUtils.stringifyException(ex)); exceptions[2].compareAndSet(null, ex); } } }; LOG.info("Starting writer and reader"); ExecutorService executor = Executors.newFixedThreadPool(3); executor.submit(writer); executor.submit(flusherCompactor); executor.submit(reader); // wait for threads Threads.sleep(runtime); running.set(false); executor.shutdown(); executor.awaitTermination(30, TimeUnit.SECONDS); for (AtomicReference<Exception> exRef : exceptions) { Assert.assertNull(exRef.get()); } } finally { HTU.deleteNumericRows(table, HConstants.CATALOG_FAMILY, startKey, endKey); closeRegion(HTU, getRS(), hriSecondary); } }
From source file:org.springframework.integration.handler.MethodInvokingMessageProcessorAnnotationTests.java
@Test public void multiThreadsUUIDToStringConversion() throws Exception { Method method = TestService.class.getMethod("headerId", String.class, String.class); final MethodInvokingMessageProcessor processor = new MethodInvokingMessageProcessor(testService, method); ExecutorService exec = Executors.newFixedThreadPool(100); processor.processMessage(new GenericMessage<String>("foo")); for (int i = 0; i < 100; i++) { exec.execute(new Runnable() { public void run() { Object result = processor.processMessage(new GenericMessage<String>("foo")); assertNotNull(result);//from w w w. j a v a 2s.c om } }); } exec.shutdown(); assertTrue(exec.awaitTermination(10, TimeUnit.SECONDS)); assertEquals(0, concurrencyFailures); }
From source file:org.springframework.test.context.junit4.SpringMethodRoadie.java
/** * Runs the test method on the test instance with the specified * <code>timeout</code>.//from ww w . ja v a 2 s . c o m * * @param timeout the timeout in milliseconds. * @see #runWithRepetitions(Runnable) * @see #runTestMethod() */ protected void runWithTimeout(final long timeout) { runWithRepetitions(new Runnable() { public void run() { final ExecutorService service = Executors.newSingleThreadExecutor(); final Callable<Object> callable = new Callable<Object>() { public Object call() throws Exception { runTestMethod(); return null; } }; final Future<Object> result = service.submit(callable); service.shutdown(); try { boolean terminated = service.awaitTermination(timeout, TimeUnit.MILLISECONDS); if (!terminated) { service.shutdownNow(); } // throws the exception if one occurred during the // invocation result.get(0, TimeUnit.MILLISECONDS); } catch (final TimeoutException e) { addFailure(new Exception(String.format("test timed out after %d milliseconds", timeout))); } catch (final Exception e) { addFailure(e); } } }); }
From source file:org.wso2.carbon.bps.perf.rest.RestClientTestDep.java
public void execute() throws Exception { instanceCount = Integer.parseInt(config.getProperty("instances")); serverURL = config.getProperty("serverURL"); numTreads = Integer.parseInt(config.getProperty("threads")); outPath = config.getProperty("results"); File outFolder = new File(outPath); if (!outFolder.exists()) { log.info("Results folder " + outFolder.getAbsolutePath() + " does not exist. Creating a new folder..."); outFolder.mkdirs();//from w w w. ja v a2 s . c o m } File testReportFile = new File(outFolder, "summary.csv"); StringBuffer summaryBuffer = new StringBuffer(); summaryBuffer.append("Server URL," + serverURL + "\n"); summaryBuffer.append("Number of process instances," + instanceCount + "\n"); summaryBuffer.append("Number of threads," + numTreads + "\n\n\n"); summaryBuffer.append("Process ID,Total time,TPS,Average execution time\n\n"); FileUtils.write(testReportFile, summaryBuffer.toString()); List<ProcessConfig> processConfigs = new ArrayList<>(); boolean processFound = true; String processRef = "process"; int processNum = 1; while (processFound) { String processProp = config.getProperty(processRef + processNum); if (processProp == null) { break; } ProcessConfig processConfig = new ProcessConfig(processProp, null); String[] processParts = processProp.split("\\|"); processConfig.setKey(processParts[0].trim()); if (processParts.length > 1) { String[] varParts = processParts[1].split(";"); for (String varPart : varParts) { String name = varPart.split(",")[0]; String value = varPart.split(",")[1]; processConfig.addVariable(name, value); } } processConfigs.add(processConfig); } for (ProcessConfig processConfig : processConfigs) { ActivitiRestClient client = new ActivitiRestClient(serverURL, numTreads); List<RestProcessExecutor> processExecutors = new ArrayList<>(instanceCount); ExecutorService executorService = Executors.newFixedThreadPool(numTreads); long stime = System.currentTimeMillis(); for (int i = 0; i < instanceCount; i++) { RestProcessExecutor processExecutor = new RestProcessExecutor(null, processConfig.getId(), processConfig.getStartupVariables(), client, i); processExecutors.add(processExecutor); executorService.execute(processExecutor); } executorService.shutdown(); try { executorService.awaitTermination(1, TimeUnit.HOURS); } catch (InterruptedException e) { String msg = "Error occurred while waiting for executors to terminate."; log.error(msg, e); } long etime = System.currentTimeMillis(); StringBuffer buf = new StringBuffer(); double externalTPS = (double) instanceCount * 1000 / (double) (etime - stime); double totalDuration = 0; buf.append("Instance durations\n"); for (RestProcessExecutor processExecutor : processExecutors) { buf.append(processExecutor.getExternalDuration() + "\n"); totalDuration += processExecutor.getExternalDuration(); } double avgExeTimeEngine = totalDuration / instanceCount; log.info("Process " + processConfig.getId() + " completed with duration: " + (etime - stime) + " ms | TPS: " + externalTPS + " | Average execution time: " + avgExeTimeEngine); String processRecord = processConfig.getId() + "," + (etime - stime) + "," + externalTPS + "," + avgExeTimeEngine + "\n"; FileWriter fileWriter = new FileWriter(testReportFile, true); fileWriter.write(processRecord); fileWriter.close(); buf.append("\n\nTPS," + externalTPS + "\n\n"); File processReportFile = new File(outFolder, processConfig.getId() + ".csv"); FileUtils.write(processReportFile, buf.toString()); client.close(); } // Map<String, Object> vars = new HashMap<String, Object>(); // vars.put("testCount", new Long(1)); // // List<RestProcessExecutor> processExecutors = new ArrayList<>(instanceCount); // ExecutorService executorService = Executors.newFixedThreadPool(numTreads); // // long stime = System.currentTimeMillis(); // for (int i = 0; i < instanceCount; i++) { // RestProcessExecutor processExecutor = new RestProcessExecutor(processKey, processId, vars, client, i); // processExecutors.add(processExecutor); // executorService.execute(processExecutor); // } // // executorService.shutdown(); // try { // executorService.awaitTermination(1, TimeUnit.HOURS); // } catch (InterruptedException e) { // String msg = "Error occurred while waiting for executors to terminate."; // log.error(msg, e); // } // // long etime = System.currentTimeMillis(); // // List<Long> startTimes = new ArrayList<Long>(); // List<Long> endTimes = new ArrayList<Long>(); // HistoryService h = engine.getHistoryService(); // for (HistoricProcessInstance hp : h.createHistoricProcessInstanceQuery().list()) { // long startTime = hp.getStartTime().getTime(); // long endTime = hp.getStartTime().getTime(); // startTimes.add(startTime); // endTimes.add(endTime); // long duration = endTime - startTime; // System.out.println("Duration: " + duration + " ms"); // } // Collections.sort(startTimes); // Collections.sort(endTimes); // // long testStartTime = startTimes.get(0); // long testEndTime = endTimes.get(endTimes.size() - 1); // System.out.println("Test duration: " + (testEndTime - testStartTime)); // double throughput = (double) instanceCount * 1000 / (double) (testEndTime - testStartTime); // System.out.println("TPS: " + throughput); // StringBuffer buf = new StringBuffer(); // log.info("External duration: " + (etime - stime)); // double externalTPS = (double) instanceCount * 1000 / (double) (etime - stime); // log.info("External TPS: " + externalTPS); // buf.append("TPS," + externalTPS + "\n\n"); // // double totalDuration = 0; // // buf.append("Instance duration\n"); // for (RestProcessExecutor processExecutor : processExecutors) { // buf.append(processExecutor.getExternalDuration() + "\n"); // totalDuration += processExecutor.getExternalDuration(); // } // log.info("Total duration: " + totalDuration); // double avgExeTimeEngine = totalDuration / instanceCount; // log.info("Average execution time (External): " + avgExeTimeEngine); // // FileUtils.write(new File(outPath), buf.toString()); // client.close(); }
From source file:org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutorOverGraphTest.java
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldAllowTraversalToIterateInDifferentThreadThanOriginallyEvaluatedWithoutAutoCommit() throws Exception { // this test sort of simulates Gremlin Server interaction where a Traversal is eval'd in one Thread, but // then iterated in another. this basically tests the state of the Gremlin Server GremlinExecutor when // being used in session mode final ExecutorService evalExecutor = Executors.newSingleThreadExecutor(testingThreadFactory); final GremlinExecutor gremlinExecutor = GremlinExecutor.build().executorService(evalExecutor).create(); final Map<String, Object> bindings = new HashMap<>(); bindings.put("g", g); final AtomicInteger vertexCount = new AtomicInteger(0); final ExecutorService iterationExecutor = Executors.newSingleThreadExecutor(testingThreadFactory); gremlinExecutor.eval("g.V().out()", bindings).thenAcceptAsync(o -> { final Iterator itty = (Iterator) o; itty.forEachRemaining(v -> vertexCount.incrementAndGet()); }, iterationExecutor).join();/*ww w . j a va2s .c o m*/ assertEquals(6, vertexCount.get()); gremlinExecutor.close(); evalExecutor.shutdown(); evalExecutor.awaitTermination(30000, TimeUnit.MILLISECONDS); iterationExecutor.shutdown(); iterationExecutor.awaitTermination(30000, TimeUnit.MILLISECONDS); }
From source file:byps.test.servlet.MyServerIF.java
@Override public int callClientParallel(int nbOfCalls) throws RemoteException { if (log.isDebugEnabled()) log.debug("callClientParallel(" + nbOfCalls); final ClientIF clientIF = getClientIF(); final AtomicInteger ret = new AtomicInteger(0); ExecutorService tpool = Executors.newCachedThreadPool(); for (int i = 0; i < nbOfCalls; i++) { Runnable run = new Runnable() { public void run() { try { if (log.isDebugEnabled()) log.debug("clientIF.incrementInt("); int v = clientIF.incrementInt(0); if (log.isDebugEnabled()) log.debug(")clientIF.incrementInt"); ret.addAndGet(v);/*from ww w . j a v a2 s . c om*/ } catch (Exception e) { log.error(e); } } }; tpool.execute(run); } tpool.shutdown(); try { tpool.awaitTermination(10, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new BException(BExceptionC.CANCELLED, e.toString(), e); } if (log.isDebugEnabled()) log.debug(")callClientParallel"); return ret.get(); }
From source file:com.emc.ecs.sync.CasMigrationTest.java
protected void delete(FPPool pool, List<String> clipIds) throws Exception { ExecutorService service = Executors.newFixedThreadPool(CAS_THREADS); System.out.print("Deleting clips"); for (String clipId : clipIds) { service.submit(new ClipDeleter(pool, clipId)); }//ww w. j a v a 2 s . c om service.shutdown(); service.awaitTermination(CAS_SETUP_WAIT_MINUTES, TimeUnit.MINUTES); service.shutdownNow(); System.out.println(); }
From source file:com.emc.vipr.sync.CasMigrationTest.java
protected void delete(FPPool pool, List<String> clipIds) throws Exception { ExecutorService service = Executors.newFixedThreadPool(CAS_SETUP_THREADS); System.out.print("Deleting clips"); for (String clipId : clipIds) { service.submit(new ClipDeleter(pool, clipId)); }/* www. jav a 2 s .co m*/ service.shutdown(); service.awaitTermination(CAS_SETUP_WAIT_MINUTES, TimeUnit.MINUTES); service.shutdownNow(); System.out.println(); }
From source file:org.apache.gobblin.ingestion.google.webmaster.GoogleWebmasterDataFetcherImpl.java
/** * Get all pages in an async mode./*from w ww. j a v a2s . co m*/ */ private Collection<String> getPages(String startDate, String endDate, List<Dimension> dimensions, ApiDimensionFilter countryFilter, Queue<Pair<String, FilterOperator>> toProcess, int rowLimit) throws IOException { String country = GoogleWebmasterFilter.countryFilterToString(countryFilter); ConcurrentLinkedDeque<String> allPages = new ConcurrentLinkedDeque<>(); int r = 0; while (r <= GET_PAGES_RETRIES) { ++r; log.info(String.format("Get pages at round %d with size %d.", r, toProcess.size())); ConcurrentLinkedDeque<Pair<String, FilterOperator>> nextRound = new ConcurrentLinkedDeque<>(); ExecutorService es = Executors.newFixedThreadPool(10, ExecutorsUtils .newDaemonThreadFactory(Optional.of(log), Optional.of(this.getClass().getSimpleName()))); while (!toProcess.isEmpty()) { submitJob(toProcess.poll(), countryFilter, startDate, endDate, dimensions, es, allPages, nextRound, rowLimit); } //wait for jobs to finish and start next round if necessary. try { es.shutdown(); boolean terminated = es.awaitTermination(5, TimeUnit.MINUTES); if (!terminated) { es.shutdownNow(); log.warn(String.format( "Timed out while getting all pages for country-%s at round %d. Next round now has size %d.", country, r, nextRound.size())); } } catch (InterruptedException e) { throw new RuntimeException(e); } if (nextRound.isEmpty()) { break; } toProcess = nextRound; } if (r == GET_PAGES_RETRIES) { throw new RuntimeException(String.format( "Getting all pages reaches the maximum number of retires %d. Date range: %s ~ %s. Country: %s.", GET_PAGES_RETRIES, startDate, endDate, country)); } return allPages; }