Example usage for java.util.concurrent ExecutorService execute

List of usage examples for java.util.concurrent ExecutorService execute

Introduction

In this page you can find the example usage for java.util.concurrent ExecutorService execute.

Prototype

void execute(Runnable command);

Source Link

Document

Executes the given command at some time in the future.

Usage

From source file:terrastore.metrics.PerformanceTest.java

@Test
public void writeThenRead() throws Exception {
    final String bucket = UUID.randomUUID().toString();
    final String payload = getPayload();

    int warmup = 1000;
    int writes = 1000;

    warmUp(warmup, bucket, payload);/*w  w  w  .  j av a  2 s.  c o m*/

    System.err.println("Starting writeThenRead performance test.");

    ExecutorService threadPool = Executors.newFixedThreadPool(CONCURRENCY);
    long start = System.currentTimeMillis();
    for (int i = warmup; i < warmup + writes; i++) {
        final int index = i;
        threadPool.execute(new Runnable() {

            public void run() {
                try {
                    PutMethod putValue = null;
                    putValue = makePutMethod(NODE1_PORT, bucket + "/value" + index);
                    putValue.setRequestEntity(new StringRequestEntity(payload, "application/json", null));
                    HTTP_CLIENT.executeMethod(putValue);
                    assertEquals(HttpStatus.SC_NO_CONTENT, putValue.getStatusCode());
                    putValue.releaseConnection();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

    }
    threadPool.shutdown();
    threadPool.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
    long elapsed = System.currentTimeMillis() - start;
    System.err.println("Elapsed write time in millis: " + elapsed);

    threadPool = Executors.newFixedThreadPool(CONCURRENCY);
    start = System.currentTimeMillis();
    for (int i = warmup; i < warmup + writes; i++) {
        final int index = i;

        threadPool.execute(new Runnable() {

            public void run() {
                try {
                    GetMethod getValue = null;
                    getValue = makeGetMethod(NODE1_PORT, bucket + "/value" + index);
                    HTTP_CLIENT.executeMethod(getValue);
                    assertEquals(HttpStatus.SC_OK, getValue.getStatusCode());
                    getValue.releaseConnection();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
    }

    threadPool.shutdown();
    threadPool.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
    elapsed = System.currentTimeMillis() - start;
    System.err.println("Elapsed read time in millis: " + elapsed);
}

From source file:com.hygenics.parser.Upload.java

private void doScp() {
    ArrayList<String> files = new ArrayList<String>();
    File fp = new File(localPath);

    if (fp.isDirectory()) {
        for (String f : fp.list()) {
            files.add(localPath + f);/* w w  w  . ja  va 2 s  .c  o m*/
        }
    } else {
        files.add(localPath);
    }

    int p = 0;
    int partsize = files.size() / MAXFILESPERTHREAD;
    int offset = 0;

    if (partsize < 100) {
        partsize = files.size();
    }

    ExecutorService exec = Executors.newFixedThreadPool(this.numthreads);

    do {
        List<String> subset = files.subList(offset, offset + partsize);
        exec.execute(new SCP(subset, this.remotePath));

        p++;
        if (p == numthreads) {
            try {
                exec.awaitTermination(timeout, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            p = 0;
        }
        offset += partsize;
    } while (offset < files.size());

    if (p > 0) {
        try {
            exec.awaitTermination(timeout, TimeUnit.MILLISECONDS);
            exec.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

From source file:org.commonjava.util.partyline.OpenInputStreamConcurrentReadersGetSameResultTest.java

/**
 * Test that verifies concurrent reading tasks, including timeout one, on the same file are allowable, this setup an script of events for
 * one single file, where:/*from   w w  w.ja v a 2  s.  co  m*/
 * <ol>
 *     <li>Multiple reads happen simultaneously, read the content</li>
 *     <li>Reading processes on the same file have no interaction between each other</li>
 * </ol>
 * @throws Exception
 */
@BMRules(rules = {
        // wait for first openInputStream call to exit
        @BMRule(name = "second openInputStream", targetClass = "JoinableFileManager", targetMethod = "openInputStream", targetLocation = "ENTRY", condition = "$2==100", action = "debug(\">>>wait for service enter first openInputStream.\");"
                + "waitFor(\"first openInputStream\");" + "debug(\"<<<proceed with second openInputStream.\")"),

        // setup the trigger to signal second openInputStream when the first openInputStream exits
        @BMRule(name = "first openInputStream", targetClass = "JoinableFileManager", targetMethod = "openInputStream", targetLocation = "ENTRY", condition = "$2==-1", action = "debug(\"<<<signalling second openInputStream.\"); "
                + "signalWake(\"first openInputStream\", true);"
                + "debug(\"<<<signalled second openInputStream.\")") })

@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 File f = temp.newFile();
    String str = "This is a test";
    FileUtils.write(f, str);

    List<String> returning = new ArrayList<String>();

    for (int i = 0; i < 2; i++) {
        final int k = i;
        execs.execute(() -> {
            Thread.currentThread().setName("openInputStream-" + k);
            InputStream s = null;
            try {
                switch (k) {
                case 0:
                    s = manager.openInputStream(f, -1);
                    break;
                case 1:
                    // note reporting timeout handle error
                    s = manager.openInputStream(f, 100);
                    break;
                }
                returning.add(IOUtils.toString(s));
                s.close();
            } catch (final Exception e) {
                e.printStackTrace();
                fail("Failed to open inputStream: " + e.getMessage());
            } finally {
                latch.countDown();
            }
        });
    }

    latch.await();

    assertThat(returning.size(), equalTo(2));

    assertThat(returning.get(0), equalTo(str));
    assertThat(returning.get(1), equalTo(str));
}

From source file:org.wso2.das.integration.tests.esb.ESBAnalyticsStatisticsTestCase.java

/**
 * Publish sample data for tenants//w w  w . ja v a 2  s  . c  om
 * 
 * @param tenants
 * @throws Exception
 */
private void publishSampleData(int noOfProxies, int requestsPerProxy, int noOfMediators, int NoOfFaults,
        boolean enablePayloads, boolean enableProperties, int[] tenants) throws Exception {
    ExecutorService executorService = Executors.newFixedThreadPool(noOfProxies * tenants.length);
    for (int tenantId : TestConstants.TENANT_IDS) {
        for (int i = 0; i < noOfProxies; i++) {
            DataPublisherClient dataPublisherClient = new DataPublisherClient();
            executorService.execute(new ConcurrentEventsPublisher(dataPublisherClient, tenantId,
                    requestsPerProxy, "AccuracyTestProxy_" + i, noOfMediators, NoOfFaults, enablePayloads,
                    enableProperties, SLEEP_BETWEEN_REQUESTS));
        }
    }
    executorService.shutdown();
    executorService.awaitTermination(WAIT_FOR_PUBLISHING_IN_MINUTES, TimeUnit.MINUTES);
}

From source file:com.uwsoft.editor.controlles.ResolutionManager.java

public void createNewResolution(String name, int width, int height, final String resolutionBase,
        final ProgressHandler handler) {
    this.handler = handler;
    final ResolutionEntryVO newResolution = new ResolutionEntryVO();
    newResolution.name = name;//from w  w w  . j av  a  2 s  . co  m
    newResolution.width = width;
    newResolution.height = height;
    newResolution.base = resolutionBase.equals("width") ? 0 : 1;
    dataManager.getCurrentProjectInfoVO().resolutions.add(newResolution);
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(new Runnable() {
        @Override
        public void run() {
            // create new folder structure
            String projPath = dataManager.getCurrentWorkingPath() + "/"
                    + dataManager.getCurrentProjectVO().projectName;
            String sourcePath = projPath + "/" + "assets/orig/images";
            String targetPath = projPath + "/" + "assets/" + newResolution.name + "/images";
            createIfNotExist(sourcePath);
            createIfNotExist(projPath + "/" + "assets/" + newResolution.name + "/pack");
            copyTexturesFromTo(sourcePath, targetPath);
            int resizeWarnings = resizeTextures(targetPath, newResolution);
            rePackProjectImages(newResolution);
            createResizedAnimations(newResolution);
            changePercentBy(5);
            DialogUtils.showOKDialog(Sandbox.getInstance().getUIStage(), "Warning", resizeWarnings
                    + " images were not resized for smaller resolutions due to already small size ( < 3px )");
        }
    });
    executor.execute(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            DataManager.getInstance().saveCurrentProject();
            handler.progressComplete();
        }
    });
    executor.shutdown();
}

From source file:com.cisco.oss.foundation.monitoring.service.TestMultiService.java

@Test
public void testManyServices() throws Exception {

    MonitoringAgentFactory.getInstance().register(
            new PropertiesConfiguration(TestMultiService.class.getResource("/config.properties").getPath()));

    ExecutorService threadPool = Executors.newFixedThreadPool(10);

    int numOfServices = 1234;

    final CountDownLatch latch = new CountDownLatch(numOfServices);

    final ServiceDetails serviceDetails = new ServiceDetails("default desc", "test", "junit", 12345);
    for (int i = 0; i < numOfServices; i++) {

        final int index = i;

        threadPool.execute(new Runnable() {

            @Override/*from  www  .  java 2  s  . co  m*/
            public void run() {
                String apiName = (index % 3) + "";
                CommunicationInfo.getCommunicationInfo().transactionStarted(serviceDetails, apiName, 125);
                try {
                    Thread.sleep(2);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                CommunicationInfo.getCommunicationInfo().transactionFinished(serviceDetails, apiName,
                        (index % 2 == 0) ? false : true, "kuku");
                latch.countDown();
            }
        });
    }

    try {
        latch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    int numberOfThreads = Thread.getAllStackTraces().keySet().size();
    Assert.assertTrue(numberOfThreads <= 30);

    try {
        Thread.sleep(1500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    Collection<Service> services = ServiceInfo.INSTANCE.getServices();
    System.out.println("services: " + services.size());

    for (Service service : services) {
        System.out.println("service: " + service + ". total: " + service.getTotalRequestCount());
    }

    Assert.assertEquals(3, services.size());

    //        try {
    //            Thread.sleep(60000);
    //        } catch (InterruptedException e) {
    //            e.printStackTrace();
    //        }

}

From source file:com.boylesoftware.web.Router.java

/**
 * Process asynchronous dispatch.//from  w  ww. j a  va2 s.  co  m
 *
 * @param webapp The application.
 * @param request The request.
 * @param response The response.
 *
 * @return {@code true} if request processed.
 *
 * @throws ServletException If an error happens.
 * @throws IOException If an I/O error happens reading the request or
 * sending the response.
 */
private boolean doAsync(final AbstractWebApplication webapp, final HttpServletRequest request,
        final HttpServletResponse response) throws ServletException, IOException {

    final boolean debug = this.log.isDebugEnabled();

    // check if asynchronous executor error
    final Exception asyncError = (Exception) request.getAttribute(ASYNC_EXCEPTION_ATTNAME);
    if (asyncError != null) {
        if (debug)
            this.log.debug("received error from async transaction", asyncError);
        if (asyncError instanceof RequestedResourceException) {
            if (asyncError instanceof ServiceUnavailableException)
                this.log.error("service unavailable error", asyncError);
            response.sendError(((RequestedResourceException) asyncError).getHTTPErrorCode());
            return true;
        }
        if (asyncError instanceof ServletException)
            throw (ServletException) asyncError;
        if (asyncError instanceof IOException)
            throw (IOException) asyncError;
        if (asyncError instanceof RuntimeException)
            throw (RuntimeException) asyncError;
        throw new ServletException(asyncError);
    }

    // restore the route
    final RouterRequest routerReq = RouterRequestLifecycle.restore(request);
    if (routerReq == null) {
        if (debug)
            this.log.debug("request is not a router request");
        return false;
    }

    // re-wrap the router request
    routerReq.rewrap(request, response);

    // check if route requires authenticated user
    final Object authedUser = routerReq.getAttribute(AUTHED_USER_ATTNAME);
    if ((authedUser != null) && (authedUser != ANONYMOUS_USER)) {
        routerReq.setAttribute(Attributes.AUTHED_USER, authedUser);
    } else if ((routerReq.getRoute().getSecurityMode() == SecurityMode.FORCE_REQUIRE_AUTH)
            || routerReq.isAuthenticationRequired()) {
        this.sendRequestForAuthentication(webapp, routerReq, response);
        return true;
    }

    // execute request processing logic
    final RequestTransactionExecutor exec = RequestTransactionExecutor.getExecutor();
    boolean recycleExec = true;
    try {
        if (exec.prepare(webapp, routerReq)) {
            recycleExec = false;
            final ExecutorService execPool = webapp.getExecutorService();
            if (debug)
                this.log.debug("executor service status: " + execPool);
            execPool.execute(exec);
        }
    } finally {
        if (recycleExec)
            exec.recycle();
    }

    // done
    return true;
}

From source file:org.apache.falcon.entity.store.ConfigurationStore.java

private void loadEntity(final EntityType type) throws FalconException {
    try {//from   w  ww .  ja va2 s .  c  om
        final ConcurrentHashMap<String, Entity> entityMap = dictionary.get(type);
        FileStatus[] files = fs.globStatus(new Path(storePath, type.name() + Path.SEPARATOR + "*"));
        if (files != null) {
            final ExecutorService service = Executors.newFixedThreadPool(100);
            for (final FileStatus file : files) {
                service.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String fileName = file.getPath().getName();
                            String encodedEntityName = fileName.substring(0, fileName.length() - 4); // drop
                            // ".xml"
                            String entityName = URLDecoder.decode(encodedEntityName, UTF_8);
                            Entity entity = restore(type, entityName);
                            entityMap.put(entityName, entity);
                        } catch (IOException | FalconException e) {
                            LOG.error("Unable to restore entity of", file);
                        }
                    }
                });
            }
            service.shutdown();
            if (service.awaitTermination(10, TimeUnit.MINUTES)) {
                LOG.info("Restored Configurations for entity type: {} ", type.name());
            } else {
                LOG.warn("Time out happened while waiting for all threads to finish while restoring entities "
                        + "for type: {}", type.name());
            }
            // Checking if all entities were loaded
            if (entityMap.size() != files.length) {
                throw new FalconException("Unable to restore configurations for entity type " + type.name());
            }
            for (Entity entity : entityMap.values()) {
                onReload(entity);
            }
        }
    } catch (IOException e) {
        throw new FalconException("Unable to restore configurations", e);
    } catch (InterruptedException e) {
        throw new FalconException(
                "Failed to restore configurations in 10 minutes for entity type " + type.name());
    }
}

From source file:com.boylesoftware.web.Router.java

/**
 * Process direct request./*from  ww w.  j a v  a2  s .  com*/
 *
 * @param webapp The application.
 * @param request The request.
 * @param response The response.
 *
 * @return {@code true} if request processed.
 *
 * @throws ServletException If an error happens.
 * @throws IOException If an I/O error happens reading the request or
 * sending the response.
 */
private boolean doRequest(final AbstractWebApplication webapp, final HttpServletRequest request,
        final HttpServletResponse response) throws ServletException, IOException {

    final boolean debug = this.log.isDebugEnabled();

    // find matching route
    RouterRequest q;
    try {
        q = webapp.getRouterConfiguration().findRoute(request, response);
        if (q == null) {
            if (debug)
                this.log.debug("no route for request URI " + request.getRequestURI());
            return false;
        }
        if (debug)
            this.log.debug("found route for request URI " + request.getRequestURI());
    } catch (final MethodNotAllowedException e) {
        if (debug)
            this.log.debug("request method " + request.getMethod() + " is not allowed for request URI "
                    + request.getRequestURI(), e);
        response.sendError(e.getHTTPErrorCode());
        return true;
    }
    final RouterRequest routerReq = q;
    RouterRequestLifecycle.associate(routerReq);

    // process request
    boolean recycleReq = true;
    try {

        // check if SSL is required
        final SecurityMode securityMode = routerReq.getRoute().getSecurityMode();
        final boolean requireAuthedUser = ((securityMode == SecurityMode.FORCE_REQUIRE_AUTH)
                || routerReq.isAuthenticationRequired());
        if ((requireAuthedUser || (securityMode == SecurityMode.FORCE_SSL)) && !routerReq.isSecure()) {
            if (debug)
                this.log.debug("non-SSL request using mapping with" + " security mode " + securityMode
                        + " and require auth: " + requireAuthedUser);
            this.sendRedirectToSecureURI(webapp, routerReq, response);
            return true;
        }

        // fix request character encoding
        if (routerReq.getCharacterEncoding() == null)
            routerReq.setCharacterEncoding("UTF-8");

        // get authenticated user
        final AuthenticatorExecutor exec = AuthenticatorExecutor.getExecutor();
        boolean recycleExec = true;
        try {
            exec.init(webapp, routerReq);
            recycleReq = recycleExec = false;
            final ExecutorService execPool = webapp.getExecutorService();
            if (debug)
                this.log.debug("executor service status: " + execPool);
            execPool.execute(exec);
        } finally {
            if (recycleExec)
                exec.recycle();
        }

    } finally {
        if (recycleReq) {
            if (debug)
                this.log.debug("recycling router request " + routerReq);
            RouterRequestLifecycle.recycle(routerReq);
        }
    }

    // done
    return true;
}