List of usage examples for java.util.concurrent ExecutorService execute
void execute(Runnable command);
From source file:org.apache.accumulo.test.merkle.cli.GenerateHashes.java
public void run(final Connector conn, final String inputTableName, final String outputTableName, final String digestName, int numThreads, final boolean iteratorPushdown, final Collection<Range> ranges) throws TableNotFoundException, AccumuloSecurityException, AccumuloException, NoSuchAlgorithmException { if (!conn.tableOperations().exists(outputTableName)) { throw new IllegalArgumentException(outputTableName + " does not exist, please create it"); }//from www .ja v a2 s . c o m ExecutorService svc = Executors.newFixedThreadPool(numThreads); final BatchWriter bw = conn.createBatchWriter(outputTableName, new BatchWriterConfig()); try { for (final Range range : ranges) { final MessageDigest digest = getDigestAlgorithm(digestName); svc.execute(new Runnable() { @Override public void run() { Scanner s; try { s = conn.createScanner(inputTableName, Authorizations.EMPTY); } catch (Exception e) { log.error("Could not get scanner for " + inputTableName, e); throw new RuntimeException(e); } s.setRange(range); Value v = null; Mutation m = null; if (iteratorPushdown) { IteratorSetting cfg = new IteratorSetting(50, DigestIterator.class); cfg.addOption(DigestIterator.HASH_NAME_KEY, digestName); s.addScanIterator(cfg); // The scanner should only ever return us one Key-Value, otherwise this approach won't work Entry<Key, Value> entry = Iterables.getOnlyElement(s); v = entry.getValue(); m = RangeSerialization.toMutation(range, v); } else { ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (Entry<Key, Value> entry : s) { DataOutputStream out = new DataOutputStream(baos); try { entry.getKey().write(out); entry.getValue().write(out); } catch (Exception e) { log.error("Error writing {}", entry, e); throw new RuntimeException(e); } digest.update(baos.toByteArray()); baos.reset(); } v = new Value(digest.digest()); m = RangeSerialization.toMutation(range, v); } // Log some progress log.info("{} computed digest for {} of {}", Thread.currentThread().getName(), range, Hex.encodeHexString(v.get())); try { bw.addMutation(m); } catch (MutationsRejectedException e) { log.error("Could not write mutation", e); throw new RuntimeException(e); } } }); } svc.shutdown(); // Wait indefinitely for the scans to complete while (!svc.isTerminated()) { try { Thread.sleep(1000); } catch (InterruptedException e) { log.error("Interrupted while waiting for executor service to gracefully complete. Exiting now"); svc.shutdownNow(); return; } } } finally { // We can only safely close this when we're exiting or we've completely all tasks bw.close(); } }
From source file:org.apache.accumulo.test.replication.merkle.cli.GenerateHashes.java
public void run(final Connector conn, final String inputTableName, final String outputTableName, final String digestName, int numThreads, final boolean iteratorPushdown, final Collection<Range> ranges) throws TableNotFoundException, AccumuloSecurityException, AccumuloException, NoSuchAlgorithmException { if (!conn.tableOperations().exists(outputTableName)) { throw new IllegalArgumentException(outputTableName + " does not exist, please create it"); }/*from w w w.j a v a 2 s . c o m*/ // Get some parallelism ExecutorService svc = Executors.newFixedThreadPool(numThreads); final BatchWriter bw = conn.createBatchWriter(outputTableName, new BatchWriterConfig()); try { for (final Range range : ranges) { final MessageDigest digest = getDigestAlgorithm(digestName); svc.execute(new Runnable() { @Override public void run() { Scanner s; try { s = conn.createScanner(inputTableName, Authorizations.EMPTY); } catch (Exception e) { log.error("Could not get scanner for " + inputTableName, e); throw new RuntimeException(e); } s.setRange(range); Value v = null; Mutation m = null; if (iteratorPushdown) { IteratorSetting cfg = new IteratorSetting(50, DigestIterator.class); cfg.addOption(DigestIterator.HASH_NAME_KEY, digestName); s.addScanIterator(cfg); // The scanner should only ever return us one Key-Value, otherwise this approach won't work Entry<Key, Value> entry = Iterables.getOnlyElement(s); v = entry.getValue(); m = RangeSerialization.toMutation(range, v); } else { ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (Entry<Key, Value> entry : s) { DataOutputStream out = new DataOutputStream(baos); try { entry.getKey().write(out); entry.getValue().write(out); } catch (Exception e) { log.error("Error writing {}", entry, e); throw new RuntimeException(e); } digest.update(baos.toByteArray()); baos.reset(); } v = new Value(digest.digest()); m = RangeSerialization.toMutation(range, v); } // Log some progress log.info("{} computed digest for {} of {}", Thread.currentThread().getName(), range, Hex.encodeHexString(v.get())); try { bw.addMutation(m); } catch (MutationsRejectedException e) { log.error("Could not write mutation", e); throw new RuntimeException(e); } } }); } svc.shutdown(); // Wait indefinitely for the scans to complete while (!svc.isTerminated()) { try { Thread.sleep(1000); } catch (InterruptedException e) { log.error("Interrupted while waiting for executor service to gracefully complete. Exiting now"); svc.shutdownNow(); return; } } } finally { // We can only safely close this when we're exiting or we've completely all tasks bw.close(); } }
From source file:org.apache.carbondata.processing.loading.sort.impl.UnsafeParallelReadMergeSorterWithColumnRangeImpl.java
@Override public Iterator<CarbonRowBatch>[] sort(Iterator<CarbonRowBatch>[] iterators) throws CarbonDataLoadingException { UnsafeSortDataRows[] sortDataRows = new UnsafeSortDataRows[columnRangeInfo.getNumOfRanges()]; intermediateFileMergers = new UnsafeIntermediateMerger[columnRangeInfo.getNumOfRanges()]; SortParameters[] sortParameterArray = new SortParameters[columnRangeInfo.getNumOfRanges()]; try {//from ww w . java2 s .c o m for (int i = 0; i < columnRangeInfo.getNumOfRanges(); i++) { SortParameters parameters = originSortParameters.getCopy(); parameters.setPartitionID(i + ""); parameters.setRangeId(i); sortParameterArray[i] = parameters; setTempLocation(parameters); intermediateFileMergers[i] = new UnsafeIntermediateMerger(parameters); sortDataRows[i] = new UnsafeSortDataRows(parameters, intermediateFileMergers[i], inMemoryChunkSizeInMB); sortDataRows[i].initialize(); } } catch (Exception e) { throw new CarbonDataLoadingException(e); } ExecutorService executorService = Executors.newFixedThreadPool(iterators.length); this.threadStatusObserver = new ThreadStatusObserver(executorService); final int batchSize = CarbonProperties.getInstance().getBatchSize(); try { for (int i = 0; i < iterators.length; i++) { executorService.execute(new SortIteratorThread(iterators[i], sortDataRows, rowCounter, this.insideRowCounterList, this.threadStatusObserver)); } executorService.shutdown(); executorService.awaitTermination(2, TimeUnit.DAYS); processRowToNextStep(sortDataRows, originSortParameters); } catch (Exception e) { checkError(); throw new CarbonDataLoadingException("Problem while shutdown the server ", e); } checkError(); try { for (int i = 0; i < intermediateFileMergers.length; i++) { intermediateFileMergers[i].finish(); } } catch (Exception e) { throw new CarbonDataLoadingException(e); } Iterator<CarbonRowBatch>[] batchIterator = new Iterator[columnRangeInfo.getNumOfRanges()]; for (int i = 0; i < sortDataRows.length; i++) { batchIterator[i] = new MergedDataIterator(sortParameterArray[i], batchSize, intermediateFileMergers[i]); } return batchIterator; }
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);/*ww w. jav a2 s . co m*/ } }); } exec.shutdown(); assertTrue(exec.awaitTermination(10, TimeUnit.SECONDS)); assertEquals(0, concurrencyFailures); }
From source file:org.fao.geonet.kernel.ThesaurusManager.java
/** * Start task to build thesaurus table once the servlet is up. * * @param context ServiceContext used to check when servlet is up only * @param thesauriDir directory containing thesauri *///w w w . jav a2 s . c om private void batchBuildTable(ServiceContext context, File thesauriDir) { ExecutorService executor = Executors.newFixedThreadPool(1); try { Runnable worker = new InitThesauriTableTask(context, thesauriDir); executor.execute(worker); } finally { executor.shutdown(); } }
From source file:org.construct_infrastructure.io.MessageReader.java
/** * Creates a new message reader that will operate over the given InputStream. * /*from w ww . j a v a 2 s . c o m*/ * @param an_inputStream * the InputStream on which to read messages. */ public MessageReader(final InputStream an_inputStream, ExecutorService an_executorService) { my_errorOccured = false; my_logger = Logger.getLogger(getClass().getName()); my_logger.setLevel(Level.WARNING); my_inputStream = new BufferedInputStream(an_inputStream); my_messageList = new LinkedList(); my_keepReading = true; an_executorService.execute(this); my_service_scheduler = Executors.newScheduledThreadPool(1); my_service_scheduler.scheduleWithFixedDelay(my_listenerMonitor, 0, FIVE_HUNDRED, TimeUnit.MILLISECONDS); my_timeout = TWO_MINS_IN_MS; my_stopwatch = new StopWatch(); my_stopwatch.start(); }
From source file:io.siddhi.extension.io.file.FileSourceTextFullModeTestCase.java
@Test public void siddhiIoFileTest4() throws InterruptedException { log.info("test SiddhiIoFile [mode = text.full] 4"); String streams = "" + "@App:name('TestSiddhiApp')" + "@source(type='file', mode='text.full'," + "dir.uri='file:/" + dirUri + "/text_full_single', " + "action.after.process='delete', " + "@map(type='json'))" + "define stream FooStream (symbol string, price float, volume long); " + "define stream BarStream (symbol string, price float, volume long); "; String query = "" + "from FooStream " + "select * " + "insert into BarStream; "; SiddhiManager siddhiManager = new SiddhiManager(); SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query); siddhiAppRuntime.addCallback("BarStream", new StreamCallback() { @Override/* w ww. jav a2 s. c om*/ public void receive(Event[] events) { EventPrinter.print(events); int n = count.incrementAndGet(); for (Event event : events) { switch (n) { case 1: AssertJUnit.assertEquals("apache", event.getData(0)); break; case 2: AssertJUnit.assertEquals("google", event.getData(0)); break; default: AssertJUnit.fail("More events received than expected."); } } } }); Thread t1 = new Thread(new Runnable() { @Override public void run() { siddhiAppRuntime.start(); } }); ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.execute(t1); SiddhiTestHelper.waitForEvents(waitTime, 1, count, timeout); Thread t2 = new Thread(new Runnable() { @Override public void run() { File source = new File(dirUri + "/text_full/google.json"); File dest = new File(dirUri + "/text_full_single/google.json"); while (true) { if (count.intValue() == 1) { try { FileUtils.copyFile(source, dest); break; } catch (IOException e) { AssertJUnit.fail( "Failed to add a new file to directory '" + dirUri + "/text_full_single'."); } } } } }); executorService.execute(t2); SiddhiTestHelper.waitForEvents(waitTime, 2, count, timeout); executorService.shutdown(); File file = new File(dirUri + "/text_full_single"); AssertJUnit.assertEquals(0, file.list().length); //assert event count AssertJUnit.assertEquals("Number of events", 2, count.get()); siddhiAppRuntime.shutdown(); }
From source file:org.apache.sentry.tests.e2e.dbprovider.TestConcurrentClients.java
/** * Test when concurrent sentry clients talking to sentry server, threads data are synchronized * @throws Exception// www. ja v a 2 s . co m */ @Test public void testConcurrentSentryClient() throws Exception { final String HIVE_KEYTAB_PATH = System.getProperty("sentry.e2etest.hive.policyOwnerKeytab"); final SentryPolicyServiceClient client = getSentryClient("hive", HIVE_KEYTAB_PATH); ExecutorService executor = Executors.newFixedThreadPool(NUM_OF_THREADS); final TestRuntimeState state = new TestRuntimeState(); for (int i = 0; i < NUM_OF_TASKS; i++) { LOGGER.info("Start to test sentry client with task id [" + i + "]"); executor.execute(new Runnable() { @Override public void run() { if (state.failed) { LOGGER.error("found one failed state, abort test from here."); return; } try { String randStr = randomString(5); String test_role = "test_role_" + randStr; LOGGER.info("Start to test role: " + test_role); Long startTime = System.currentTimeMillis(); Long elapsedTime = 0L; while (Long.compare(elapsedTime, SENTRY_CLIENT_TEST_DURATION_MS) <= 0) { LOGGER.info("Test role " + test_role + " runs " + elapsedTime + " ms."); client.createRole(ADMIN1, test_role); client.listRoles(ADMIN1); client.grantServerPrivilege(ADMIN1, test_role, "server1", false); client.listAllPrivilegesByRoleName(ADMIN1, test_role); client.dropRole(ADMIN1, test_role); elapsedTime = System.currentTimeMillis() - startTime; } state.setNumSuccess(); } catch (Exception e) { LOGGER.error("Sentry Client Testing Exception: ", e); state.setFirstException(e); } } }); } executor.shutdown(); while (!executor.isTerminated()) { Thread.sleep(1000); //millisecond } Throwable ex = state.getFirstException(); assertFalse(ex == null ? "Test failed" : ex.toString(), state.failed); assertEquals(NUM_OF_TASKS, state.getNumSuccess()); }
From source file:org.commonjava.util.partyline.TwoConcurrentReadsOnTheSameFileTest.java
/** * Test that verifies concurrent reading tasks on the same file are allowable, this setup an script of events for * one single file, where:// w w w. j ava 2 s . c o m * <ol> * <li>Multiple reads happen simultaneously, read the content</li> * <li>Simulate reading time as 1s before stream close</li> * <li>Reading processes on the same file have no interaction between each other</li> * </ol> * @throws Exception */ @BMRules(rules = { // setup the rendezvous for all reading threads, which will mean suspending everything until all threads are started. @BMRule(name = "init rendezvous", targetClass = "JoinableFileManager", targetMethod = "<init>", targetLocation = "ENTRY", action = "createRendezvous(\"begin\", 2);" + "debug(\"<<<init rendezvous for begin.\")"), // setup the rendezvous to wait for all threads to be ready before proceeding. @BMRule(name = "openInputStream start", targetClass = "JoinableFileManager", targetMethod = "openInputStream", targetLocation = "ENTRY", action = "debug(\">>>Waiting for ALL to start.\");" + "rendezvous(\"begin\");" + "debug(\"<<<\"+Thread.currentThread().getName() + \": openInputStream() thread proceeding.\" )"), // hold inputStream waiting for 1s before its close @BMRule(name = "hold closed", targetClass = "JoinableFile$JoinInputStream", targetMethod = "close", targetLocation = "ENTRY", action = "debug(\">>>waiting for closed.\");" + "java.lang.Thread.sleep(1000);") }) @Test @BMUnitConfig(debug = true) public void run() throws Exception { final ExecutorService execs = Executors.newFixedThreadPool(2); final CountDownLatch latch = new CountDownLatch(2); final JoinableFileManager manager = new JoinableFileManager(); final String content = "This is a bmunit test"; final File file = temp.newFile("file_both_read.txt"); FileUtils.write(file, content); List<String> returning = new ArrayList<String>(); for (int i = 0; i < 2; i++) { final int k = i; execs.execute(() -> { Thread.currentThread().setName("openInputStream-" + k); try (InputStream s = manager.openInputStream(file)) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); int read = -1; final byte[] buf = new byte[512]; System.out.println(String.format( "<<<concurrent reading>>> will start to read from the resource with inputStream %s", s.getClass().getName())); while ((read = s.read(buf)) > -1) { baos.write(buf, 0, read); } baos.close(); s.close(); System.out.println(String.format( "<<<concurrent reading>>> reading from the resource done with inputStream %s", s.getClass().getName())); returning.add(new String(baos.toByteArray(), "UTF-8")); } catch (Exception e) { e.printStackTrace(); fail("Failed to open inputStream: " + e.getMessage()); } finally { latch.countDown(); } }); } latch.await(); assertThat(returning.get(0), equalTo(content)); assertThat(returning.get(1), equalTo(content)); }
From source file:org.wso2.appserver.integration.lazy.loading.artifacts.SuperTenantGhostDeploymentTestCase.java
@Test(groups = "wso2.as.lazy.loading", description = "Send concurrent requests when Web-App is in Ghost form. " + "All request should get expected output", dependsOnMethods = "testWebAppAutoUnLoadAndReloadInGhostFormInGhostDeploymentOnSuperTenant", enabled = false) public void testConcurrentWebAPPInvocationsWhenWebAppIsInGhostFormInGhostDeploymentOnSuperTenant() throws Exception { //This test method case disable because of CARBON-15271 serverManager.restartGracefully();/* w ww .java 2 s . c o m*/ HttpResponse httpResponseApp2 = HttpURLConnectionClient.sendGetRequest(webApp2URL, null); assertTrue(httpResponseApp2.getData().contains(WEB_APP2_RESPONSE), "Invocation of Web-App fail :" + webApp2URL); WebAppStatusBean webAppStatusTenant1WebApp2 = getWebAppStatus(superTenantDomain, WEB_APP_FILE_NAME2); assertTrue(webAppStatusTenant1WebApp2.isWebAppStarted(), "Web-App: " + WEB_APP_FILE_NAME2 + " is not started in Tenant:" + superTenantDomain); assertFalse(webAppStatusTenant1WebApp2.isWebAppGhost(), "Web-App: " + WEB_APP_FILE_NAME2 + " is in ghost mode after invoking in Tenant:" + superTenantDomain); WebAppStatusBean webAppStatusTenant1WebApp1 = getWebAppStatus(superTenantDomain, WEB_APP_FILE_NAME1); assertTrue(webAppStatusTenant1WebApp1.isWebAppStarted(), "Web-App: " + WEB_APP_FILE_NAME1 + " is not started in Tenant:" + superTenantDomain); assertTrue(webAppStatusTenant1WebApp1.isWebAppGhost(), "Web-App: " + WEB_APP_FILE_NAME1 + " is in not ghost mode before invoking in Tenant:" + superTenantDomain); ExecutorService executorService = Executors.newFixedThreadPool(CONCURRENT_THREAD_COUNT); log.info("Concurrent invocation Start"); log.info("Expected Response Data:" + WEB_APP1_RESPONSE); for (int i = 0; i < CONCURRENT_THREAD_COUNT; i++) { final int requestId = i; executorService.execute(new Runnable() { public void run() { HttpResponse httpResponse = null; try { httpResponse = HttpURLConnectionClient.sendGetRequest(webApp1URL, null); } catch (IOException e) { log.error("Error when sending a get request for :" + webApp1URL, e); } synchronized (this) { String responseDetailedInfo; String responseData; if (httpResponse != null) { responseDetailedInfo = "Request ID " + requestId + " Response Data :" + httpResponse.getData() + "\tResponse Code:" + httpResponse.getResponseCode(); responseData = httpResponse.getData(); } else { responseDetailedInfo = "Request ID " + requestId + " Response Data : NULL Object return from" + " HttpURLConnectionClient"; responseData = "NULL Object return"; } responseDataList.add(responseData); log.info(responseDetailedInfo); responseDetailedInfoList.add(responseDetailedInfo); } } }); } executorService.shutdown(); executorService.awaitTermination(5, TimeUnit.MINUTES); log.info("Concurrent invocation End"); int correctResponseCount = 0; for (String responseData : responseDataList) { if (WEB_APP1_RESPONSE.equals(responseData)) { correctResponseCount += 1; } } StringBuilder allDetailResponseStringBuffer = new StringBuilder(); allDetailResponseStringBuffer.append("\n"); for (String responseInfo : responseDetailedInfoList) { allDetailResponseStringBuffer.append(responseInfo); allDetailResponseStringBuffer.append("\n"); } String allDetailResponse = allDetailResponseStringBuffer.toString(); webAppStatusTenant1WebApp1 = getWebAppStatus(superTenantDomain, WEB_APP_FILE_NAME1); assertTrue(webAppStatusTenant1WebApp1.isWebAppStarted(), "Web-App: " + WEB_APP_FILE_NAME1 + " is not started in Tenant:" + superTenantDomain); assertFalse(webAppStatusTenant1WebApp1.isWebAppGhost(), "Web-App: " + WEB_APP_FILE_NAME1 + " is in ghost mode after invoking in Tenant:" + superTenantDomain); assertEquals(correctResponseCount, CONCURRENT_THREAD_COUNT, allDetailResponse + "All the concurrent requests not get correct response."); }