List of usage examples for java.util.concurrent ExecutorService execute
void execute(Runnable command);
From source file:org.wso2.appserver.integration.lazy.loading.artifacts.CarbonAppGhostDeploymentTestCase.java
@Test(groups = "wso2.as.lazy.loading", description = "Send concurrent requests when tenant context is loaded." + " But Web-App that is deployed as Carbon application is in Ghost form. All request should get expected output", dependsOnMethods = "testConcurrentWebAPPInCarbonAppInvocationsWhenTenantContextNotLoadedInGhostDeployment", enabled = false) public void testConcurrentWebAPPInCarbonAppInvocationsWhenTenantContextLoadedInGhostDeployment() throws Exception { //This test method case disable because of CARBON-15270 serverManager.restartGracefully();// www. j av a 2 s . c om responseDataList.clear(); responseDetailedInfoList.clear(); assertFalse(getTenantStatus(tenantDomain1).isTenantContextLoaded(), "Tenant context is loaded before access. Tenant name: " + tenantDomain1); HttpResponse httpResponseApp2 = HttpURLConnectionClient.sendGetRequest(tenant1WebApp2URL, null); assertTrue(httpResponseApp2.getData().contains(WEB_APP2_RESPONSE), "Invocation of Web-App fail :" + tenant1WebApp2URL); assertTrue(getTenantStatus(tenantDomain1).isTenantContextLoaded(), "Tenant context is not loaded after access. Tenant name: " + tenantDomain1); WebAppStatusBean webAppStatusTenant1WebApp2 = getWebAppStatus(tenantDomain1, CARBON_APP2_WEB_APP_FILE); assertTrue(webAppStatusTenant1WebApp2.isWebAppStarted(), "Web-App: " + CARBON_APP2_WEB_APP_FILE + " is not started in Tenant:" + tenantDomain1); assertFalse(webAppStatusTenant1WebApp2.isWebAppGhost(), "Web-App: " + CARBON_APP2_WEB_APP_FILE + " is in ghost mode after invoking in Tenant:" + tenantDomain1); WebAppStatusBean webAppStatusTenant1WebApp1 = getWebAppStatus(tenantDomain1, CARBON_APP1_WEB_APP_FILE); assertTrue(webAppStatusTenant1WebApp1.isWebAppStarted(), "Web-App: " + CARBON_APP1_WEB_APP_FILE + " is not started in Tenant:" + tenantDomain1); assertTrue(webAppStatusTenant1WebApp1.isWebAppGhost(), "Web-App: " + CARBON_APP1_WEB_APP_FILE + " is in not ghost mode before invoking in Tenant:" + tenantDomain1); 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++) { executorService.execute(new Runnable() { public void run() { HttpResponse httpResponse = null; try { httpResponse = HttpURLConnectionClient.sendGetRequest(tenant1WebApp1URL, null); } catch (IOException e) { log.error("Error when sending a get request for :" + tenant1WebApp1URL, e); } synchronized (this) { String responseDetailedInfo; String responseData; if (httpResponse != null) { responseDetailedInfo = "Response Data :" + httpResponse.getData() + "\tResponse Code:" + httpResponse.getResponseCode(); responseData = httpResponse.getData(); } else { responseDetailedInfo = "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(tenantDomain1, CARBON_APP1_WEB_APP_FILE); assertTrue(webAppStatusTenant1WebApp1.getTenantStatus().isTenantContextLoaded(), " Tenant Context is" + " not loaded. Tenant:" + tenantDomain1); assertTrue(webAppStatusTenant1WebApp1.isWebAppStarted(), "Web-App: " + CARBON_APP1_WEB_APP_FILE + " is not started in Tenant:" + tenantDomain1); assertFalse(webAppStatusTenant1WebApp1.isWebAppGhost(), "Web-App: " + CARBON_APP1_WEB_APP_FILE + " is in ghost mode after invoking in Tenant:" + tenantDomain1); assertEquals(correctResponseCount, CONCURRENT_THREAD_COUNT, allDetailResponse + "All the concurrent requests" + " not get correct response."); }
From source file:com.google.code.fqueue.memcached.TestFqueueServer.java
public void mutiThreadGet() throws InterruptedException, TimeoutException, MemcachedException { int threadCount = 8; ExecutorService pool = Executors.newFixedThreadPool(threadCount); CountDownLatch latch = new CountDownLatch(threadCount); // MemcachedBenchJob.test = tester; MemcachedTestGet[] muti = new MemcachedTestGet[threadCount]; for (int i = 0; i < threadCount; i++) { muti[i] = new MemcachedTestGet(latch); }// w w w. j a v a 2 s . co m log.info("start"); long start = System.currentTimeMillis(); for (int i = 0; i < threadCount; i++) { pool.execute(muti[i]); } latch.await(); long spend = System.currentTimeMillis() - start; log.info(threadCount + "threads ?:" + threadCount * 10000); assertEquals(0, getSize()); }
From source file:net.mindengine.galen.GalenMain.java
private void runSuitesInThreads(List<GalenSuite> suites, int parallelSuites, final CompleteListener listener) { ExecutorService executor = Executors.newFixedThreadPool(parallelSuites); for (final GalenSuite suite : suites) { Runnable thread = new Runnable() { @Override//from w ww . j a v a2s.com public void run() { GalenSuiteRunner suiteRunner = new GalenSuiteRunner(); suiteRunner.setSuiteListener(listener); suiteRunner.setValidationListener(listener); suiteRunner.runSuite(suite); } }; executor.execute(thread); } executor.shutdown(); while (!executor.isTerminated()) { } }
From source file:org.wso2.appserver.integration.lazy.loading.artifacts.JaggeryApplicationGhostDeploymentTestCase.java
@Test(groups = "wso2.as.lazy.loading", description = "Send concurrent requests when tenant context is loaded." + " But Jaggery application is in Ghost form. All request should get expected output", dependsOnMethods = "testConcurrentJaggeryAPPInvocationsWhenTenantContextNotLoadedInGhostDeployment", enabled = false) public void testConcurrentJaggeryAPPInvocationsWhenTenantContextLoadedInGhostDeployment() throws Exception { //This test method case disable because of CARBON-15270 serverManager.restartGracefully();/*from w ww . jav a 2 s . c om*/ responseDataList.clear(); responseDetailedInfoList.clear(); assertFalse(getTenantStatus(tenantDomain1).isTenantContextLoaded(), "Tenant context is loaded before access. Tenant name: " + tenantDomain1); HttpResponse httpResponseApp2 = HttpURLConnectionClient.sendGetRequest(tenant1JaggApp2Url, null); assertTrue(httpResponseApp2.getData().contains(JAGG_APP2_RESPONSE), "Invocation of Web-App fail :" + tenant1JaggApp2Url); assertTrue(getTenantStatus(tenantDomain1).isTenantContextLoaded(), "Tenant context is not loaded after access. Tenant name: " + tenantDomain1); WebAppStatusBean webAppStatusTenant1WebApp2 = getWebAppStatus(tenantDomain1, JAGGERY_APP_NAME2); assertTrue(webAppStatusTenant1WebApp2.isWebAppStarted(), "Web-App: " + JAGGERY_APP_NAME2 + " is not started in Tenant:" + tenantDomain1); assertFalse(webAppStatusTenant1WebApp2.isWebAppGhost(), "Web-App: " + JAGGERY_APP_NAME2 + " is in ghost mode after invoking in Tenant:" + tenantDomain1); WebAppStatusBean webAppStatusTenant1WebApp1 = getWebAppStatus(tenantDomain1, JAGGERY_APP_NAME1); assertTrue(webAppStatusTenant1WebApp1.isWebAppStarted(), "Web-App: " + JAGGERY_APP_NAME1 + " is not started in Tenant:" + tenantDomain1); assertTrue(webAppStatusTenant1WebApp1.isWebAppGhost(), "Web-App: " + JAGGERY_APP_NAME1 + " is in not ghost mode before invoking in Tenant:" + tenantDomain1); ExecutorService executorService = Executors.newFixedThreadPool(CONCURRENT_THREAD_COUNT); log.info("Concurrent invocation Start"); log.info("Expected Response Data:" + JAGG_APP1_RESPONSE); for (int i = 0; i < CONCURRENT_THREAD_COUNT; i++) { executorService.execute(new Runnable() { public void run() { HttpResponse httpResponse = null; try { httpResponse = HttpURLConnectionClient.sendGetRequest(tenant1JaggApp1Url, null); } catch (IOException e) { log.error("Error when sending a get request for :" + tenant1JaggApp1Url, e); } synchronized (this) { String responseDetailedInfo; String responseData; if (httpResponse != null) { responseDetailedInfo = "Response Data :" + httpResponse.getData() + "\tResponse Code:" + httpResponse.getResponseCode(); responseData = httpResponse.getData(); } else { responseDetailedInfo = "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 (JAGG_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(tenantDomain1, JAGGERY_APP_NAME1); assertTrue(webAppStatusTenant1WebApp1.getTenantStatus().isTenantContextLoaded(), " Tenant Context is not loaded. Tenant:" + tenantDomain1); assertTrue(webAppStatusTenant1WebApp1.isWebAppStarted(), "Web-App: " + JAGGERY_APP_NAME1 + " is not started in Tenant:" + tenantDomain1); assertFalse(webAppStatusTenant1WebApp1.isWebAppGhost(), "Web-App: " + JAGGERY_APP_NAME1 + " is in ghost mode after invoking in Tenant:" + tenantDomain1); assertEquals(correctResponseCount, CONCURRENT_THREAD_COUNT, allDetailResponse + "All the concurrent requests" + " not get correct response."); }
From source file:org.opennms.netmgt.events.commands.StressCommand.java
@Override protected Object doExecute() { // Apply sane lower bounds to all of the configurable options eventsPerSecondPerThread = Math.max(1, eventsPerSecondPerThread); numberOfThreads = Math.max(1, numberOfThreads); numSeconds = Math.max(1, numSeconds); reportIntervalInSeconds = Math.max(1, reportIntervalInSeconds); batchSize = Math.max(1, batchSize); boolean useJexl = jexlExpressions != null && jexlExpressions.size() > 0; // Display the effective settings and rates double eventsPerSecond = eventsPerSecondPerThread * numberOfThreads; System.out.printf("Generating %d events per second accross %d threads for %d seconds\n", eventsPerSecondPerThread, numberOfThreads, numSeconds); System.out.printf("\t with UEI: %s\n", eventUei); System.out.printf("\t with batch size: %d\n", batchSize); System.out.printf("\t with synchronous calls: %s\n", isSynchronous); System.out.printf("Which will yield an effective\n"); System.out.printf("\t %.2f events per second\n", eventsPerSecond); System.out.printf("\t %.2f total events\n", eventsPerSecond * numSeconds); if (useJexl) { System.out.printf("Using JEXL expressions:\n"); for (String jexlExpression : jexlExpressions) { System.out.printf("\t%s\n", jexlExpression); }//from ww w .j a v a2 s . c o m } // Setup the reporter ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS).build(); // Setup the executor final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Event Generator #%d").build(); final ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads, threadFactory); System.out.println("Starting."); try { reporter.start(reportIntervalInSeconds, TimeUnit.SECONDS); for (int i = 0; i < numberOfThreads; i++) { final EventGenerator eventGenerator = useJexl ? new JexlEventGenerator(jexlExpressions) : new EventGenerator(); executor.execute(eventGenerator); } System.out.println("Started."); // Wait until we timeout or get interrupted try { Thread.sleep(SECONDS.toMillis(numSeconds)); } catch (InterruptedException e) { } // Stop! try { System.out.println("Stopping."); executor.shutdownNow(); if (!executor.awaitTermination(2, TimeUnit.MINUTES)) { System.err.println("The threads did not stop in time."); } else { System.out.println("Stopped."); } } catch (InterruptedException e) { } } finally { // Make sure we always stop the reporter reporter.stop(); } // And display one last report... reporter.report(); return null; }
From source file:org.wso2.appserver.integration.lazy.loading.artifacts.WebApplicationGhostDeploymentTestCase.java
@Test(groups = "wso2.as.lazy.loading", description = "Send concurrent requests when tenant context is not loaded." + "All request should get expected output", dependsOnMethods = "testTenantUnloadInIdleTimeAfterWebAPPUsageInGhostDeployment", enabled = false) public void testConcurrentWebAPPInvocationsWhenTenantContextNotLoadedInGhostDeployment() throws Exception { serverManager.restartGracefully();// w ww .j av a 2 s . c om assertFalse(getTenantStatus(tenantDomain1).isTenantContextLoaded(), "Tenant context is loaded before access. Tenant name: " + tenantDomain1); 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(tenant1WebApp1URL, null); } catch (IOException e) { log.error("Error when sending a get request for :" + tenant1WebApp1URL, 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(); WebAppStatusBean webAppStatusTenant1WebApp1 = getWebAppStatus(tenantDomain1, WEB_APP_FILE_NAME1); assertTrue(webAppStatusTenant1WebApp1.getTenantStatus().isTenantContextLoaded(), " Tenant Context is" + " not loaded. Tenant:" + tenantDomain1); assertTrue(webAppStatusTenant1WebApp1.isWebAppStarted(), "Web-App: " + WEB_APP_FILE_NAME1 + " is not" + " started in Tenant:" + tenantDomain1); assertFalse(webAppStatusTenant1WebApp1.isWebAppGhost(), "Web-App: " + WEB_APP_FILE_NAME1 + " is in " + "ghost mode after invoking in Tenant:" + tenantDomain1); assertEquals(correctResponseCount, CONCURRENT_THREAD_COUNT, allDetailResponse + "All the concurrent requests " + "not get correct response."); }
From source file:oz.hadoop.yarn.api.core.LocalApplicationLaunchTests.java
@Test(timeout = 60000) public void validateLongLivedJavaContainerLaunchWithGracefullShutdown() throws Exception { ExecutorService executor = Executors.newSingleThreadExecutor(); YarnApplication<DataProcessor> yarnApplication = YarnAssembly .forApplicationContainer(SimpleRandomDelayContainer.class).containerCount(4).withApplicationMaster() .build("sample-yarn-application"); final DataProcessor dataProcessor = yarnApplication.launch(); assertEquals(4, dataProcessor.containers()); executor.execute(new Runnable() { @Override/*from w w w . j a va 2 s .co m*/ public void run() { for (int i = 0; i < 1000000; i++) { for (int j = 0; j < dataProcessor.containers(); j++) { try { dataProcessor.process(ByteBuffer.wrap(("Hello Yarn!-" + i).getBytes())); } catch (Exception e) { e.printStackTrace(); throw new IllegalStateException("Failed to submit data for processing", e); } } } } }); assertTrue(yarnApplication.isRunning()); Thread.sleep(new Random().nextInt(5000)); yarnApplication.shutDown(); assertFalse(yarnApplication.isRunning()); }
From source file:oz.hadoop.yarn.api.core.LocalApplicationLaunchTests.java
@Test(timeout = 30000) public void validateLongLivedJavaContainerLaunchWithTermination() throws Exception { ExecutorService executor = Executors.newSingleThreadExecutor(); YarnApplication<DataProcessor> yarnApplication = YarnAssembly .forApplicationContainer(SimpleRandomDelayContainer.class).containerCount(4).withApplicationMaster() .build("sample-yarn-application"); final DataProcessor dataProcessor = yarnApplication.launch(); assertEquals(4, dataProcessor.containers()); executor.execute(new Runnable() { @Override/*w w w . j a v a 2 s .c om*/ public void run() { for (int i = 0; i < 1000000; i++) { for (int j = 0; j < dataProcessor.containers(); j++) { try { dataProcessor.process(ByteBuffer.wrap(("Hello Yarn!-" + i).getBytes())); } catch (Exception e) { e.printStackTrace(); throw new IllegalStateException("Failed to submit data for processing", e); } } } } }); assertTrue(yarnApplication.isRunning()); Thread.sleep(new Random().nextInt(5000)); yarnApplication.terminate(); assertFalse(yarnApplication.isRunning()); }
From source file:org.apache.hadoop.hbase.master.BulkReOpen.java
/** * Unassign all regions, so that they go through the regular region * assignment flow (in assignment manager) and are re-opened. *//*from w w w. ja v a 2 s . c om*/ @Override protected void populatePool(ExecutorService pool) { LOG.debug("Creating threads for each region server "); for (Map.Entry<ServerName, List<HRegionInfo>> e : rsToRegions.entrySet()) { final List<HRegionInfo> hris = e.getValue(); // add plans for the regions that need to be reopened Map<String, RegionPlan> plans = new HashMap<String, RegionPlan>(); for (HRegionInfo hri : hris) { RegionPlan reOpenPlan = assignmentManager.getRegionReopenPlan(hri); plans.put(hri.getEncodedName(), reOpenPlan); } assignmentManager.addPlans(plans); pool.execute(new Runnable() { public void run() { try { unassign(hris); } catch (Throwable t) { LOG.warn("Failed bulking re-open " + hris.size() + " region(s)", t); } } }); } }
From source file:com.alibaba.wasp.master.BulkReOpen.java
/** * Unassign all entityGroups, so that they go through the regular entityGroup assignment * flow (in assignment manager) and are re-opened. *///from ww w . ja v a 2 s.c o m @Override protected void populatePool(ExecutorService pool) { LOG.debug("Creating threads for each entityGroup server "); for (Map.Entry<ServerName, List<EntityGroupInfo>> e : fsToEntityGroups.entrySet()) { final List<EntityGroupInfo> egis = e.getValue(); // add plans for the entityGroups that need to be reopened Map<String, EntityGroupPlan> plans = new HashMap<String, EntityGroupPlan>(); for (EntityGroupInfo egi : egis) { EntityGroupPlan reOpenPlan = assignmentManager.getEntityGroupReopenPlan(egi); plans.put(egi.getEncodedName(), reOpenPlan); } assignmentManager.addPlans(plans); pool.execute(new Runnable() { public void run() { assignmentManager.unassign(egis); } }); } }