Example usage for java.util.concurrent ExecutorService submit

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

Introduction

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

Prototype

Future<?> submit(Runnable task);

Source Link

Document

Submits a Runnable task for execution and returns a Future representing that task.

Usage

From source file:no.dusken.aranea.web.spring.ChainedController.java

/**
 * Spawns multiple threads, one for each controller in the list of
 * controllers, and within each thread, delegates to the controller's
 * handleRequest() method. Once all the threads are complete, the
 * ModelAndView objects returned from each of the handleRequest() methods
 * are merged into a single view. The view name for the model is set to the
 * specified view name. If an exception is thrown by any of the controllers
 * in the chain, this exception is propagated up from the handleRequest()
 * method of the ChainedController.// www  .java  2s.  c  om
 *
 * @param request  the HttpServletRequest object.
 * @param response the HttpServletResponse object.
 * @return a merged ModelAndView object.
 * @throws Exception if one is thrown from the controllers in the chain.
 */
@SuppressWarnings("unchecked")
private ModelAndView handleRequestParallely(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    ExecutorService service = Executors.newCachedThreadPool();
    int numberOfControllers = controllers.size();
    CallableController[] callables = new CallableController[numberOfControllers];
    Future<ModelAndView>[] futures = new Future[numberOfControllers];
    for (int i = 0; i < numberOfControllers; i++) {
        callables[i] = new CallableController(controllers.get(i), request, response);
        futures[i] = service.submit(callables[i]);
    }
    ModelAndView mergedModel = new ModelAndView();
    for (Future<ModelAndView> future : futures) {
        ModelAndView model = future.get();
        if (model != null) {
            mergedModel.addAllObjects(model.getModel());
        }
    }
    if (StringUtils.isNotEmpty(this.viewName)) {
        mergedModel.setViewName(this.viewName);
    }
    return mergedModel;
}

From source file:com.microsoft.azure.management.datalake.store.uploader.DataLakeStoreUploader.java

/**
 * Concatenates all the segments defined in the metadata into a single stream.
 *
 * @param metadata The {@link UploadMetadata} to determine the segments to concatenate
 * @throws Exception/*w  w  w  .  j a v  a 2  s  .  co  m*/
 */
private void concatenateSegments(final UploadMetadata metadata) throws Exception {
    final String[] inputPaths = new String[metadata.getSegmentCount()];

    //verify if target stream exists
    if (frontEnd.streamExists(metadata.getTargetStreamPath())) {
        if (this.getParameters().isOverwrite()) {
            frontEnd.deleteStream(metadata.getTargetStreamPath(), false);
        } else {
            throw new OperationsException("Target Stream already exists");
        }
    }

    //ensure all input streams exist and are of the expected length
    //ensure all segments in the metadata are marked as 'complete'
    final List<Exception> exceptions = new ArrayList<>();
    ExecutorService exec = Executors.newFixedThreadPool(this.getParameters().getThreadCount());
    for (int i = 0; i < metadata.getSegmentCount(); i++) {
        final int finalI = i;
        exec.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    if (metadata.getSegments()[finalI].getStatus() != SegmentUploadStatus.Complete) {
                        throw new UploadFailedException(
                                "Cannot perform 'concatenate' operation because not all streams are fully uploaded.");
                    }

                    String remoteStreamPath = metadata.getSegments()[finalI].getPath();
                    int retryCount = 0;
                    long remoteLength = -1;

                    while (retryCount < SingleSegmentUploader.MAX_BUFFER_UPLOAD_ATTEMPT_COUNT) {
                        retryCount++;
                        try {
                            remoteLength = frontEnd.getStreamLength(remoteStreamPath);
                            break;
                        } catch (Exception e) {
                            if (retryCount >= SingleSegmentUploader.MAX_BUFFER_UPLOAD_ATTEMPT_COUNT) {
                                throw new UploadFailedException(MessageFormat.format(
                                        "Cannot perform 'concatenate' operation due to the following exception retrieving file information: {0}",
                                        e));
                            }

                            SingleSegmentUploader.waitForRetry(retryCount,
                                    parameters.isUseSegmentBlockBackOffRetryStrategy());
                        }
                    }

                    if (remoteLength != metadata.getSegments()[finalI].getLength()) {
                        throw new UploadFailedException(MessageFormat.format(
                                "Cannot perform 'concatenate' operation because segment {0} has an incorrect length (expected {1}, actual {2}).",
                                finalI, metadata.getSegments()[finalI].getLength(), remoteLength));
                    }

                    inputPaths[finalI] = remoteStreamPath;

                } catch (Exception ex) {
                    //collect any exceptions, whether we just generated them above or whether they come from the Front End,
                    synchronized (exceptions) {
                        exceptions.add(ex);
                    }
                }
            }
        });
    }

    exec.shutdown();

    try {
        exec.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); // waits ~292 years for completion or interruption.
    } catch (InterruptedException e) {
        // add the exception since it will indicate that it was cancelled.
        exceptions.add(e);
    }

    if (exceptions.size() > 0) {
        throw new AggregateUploadException("At least one concatenate test failed", exceptions.remove(0),
                exceptions);
    }

    //issue the command
    frontEnd.concatenate(metadata.getTargetStreamPath(), inputPaths);
}

From source file:com.jivesoftware.os.jive.utils.http.client.ApacheHttpClient31BackedHttpClient.java

private HttpResponse executeWithTimeout(final HttpMethodBase httpMethod, int timeoutMillis) {
    client.getParams().setParameter("http.method.retry-handler", new DefaultHttpMethodRetryHandler(0, false));
    ExecutorService service = Executors.newSingleThreadExecutor();

    Future<HttpResponse> future = service.submit(new Callable<HttpResponse>() {
        @Override//  ww  w. j a v  a  2s. c  om
        public HttpResponse call() throws IOException {
            return execute(httpMethod);
        }
    });

    try {
        return future.get(timeoutMillis, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        String uriInfo = "";
        try {
            uriInfo = " for " + httpMethod.getURI();
        } catch (Exception ie) {
        }
        LOG.warn("Http connection thread was interrupted or has timed out" + uriInfo, e);
        return new HttpResponse(HttpStatus.SC_REQUEST_TIMEOUT, "Request Timeout", null);
    } finally {
        service.shutdownNow();
    }
}

From source file:com.predic8.membrane.examples.tests.integration.OAuth2RaceCondition.java

@Test
public void testSessionIdStateRaceCondition() throws Exception {
    HttpClient hc = HttpClientBuilder.create().build();

    login(hc);// w ww  . j  a  va 2s .c o  m
    System.out.println("Logged in");
    ExecutorService executor = Executors.newFixedThreadPool(2);
    for (int i = 0; i < 10; i++) {

        //            HttpClient hc1 = HttpClientBuilder.create().build();
        //            login(hc1);
        Future<Exception>[] results = new Future[2];

        int parallelReqs = 2;
        CountDownLatch cdl = new CountDownLatch(parallelReqs);

        for (int j = 0; j < parallelReqs; j++) {
            final int fj = j;
            results[j] = executor.submit(() -> {
                try {
                    int uri = (fj % 2 == 0 ? 1 : 2);
                    String url = "http://localhost:2011/test" + uri;
                    HttpGet get = new HttpGet(url);
                    //setNoRedirects(get);
                    cdl.countDown();
                    cdl.await();
                    try (CloseableHttpResponse getRes = (CloseableHttpResponse) hc.execute(get)) {
                        assertEquals(200, getRes.getStatusLine().getStatusCode());
                        String resText = EntityUtils.toString(getRes.getEntity(), "UTF-8");
                        System.out.println("Called: Test" + uri + ".\nActual: " + resText);
                        assertTrue(resText.contains(Integer.toString(uri)));
                    }
                    return null;
                } catch (Exception e) {
                    return e;
                }
            });
        }
        for (int j = 0; j < parallelReqs; j++) {
            results[j].get();
        }

        for (int j = 0; j < parallelReqs; j++) {
            Exception e = results[j].get();
            if (e != null)
                throw new RuntimeException(e);
        }

    }
    executor.shutdown();
}

From source file:com.photon.maven.plugins.android.AbstractEmulatorMojo.java

/**
 * Sends a user command to the running emulator via its telnet interface.
 *
 * @param port The emulator's telnet port.
 * @param command The command to execute on the emulator's telnet interface.
 * @return Whether sending the command succeeded.
 *///from   w w  w  .ja v  a 2 s . c  om
private boolean sendEmulatorCommand(
        //final Launcher launcher, 
        //final PrintStream logger,
        final int port, final String command) {
    Callable<Boolean> task = new Callable<Boolean>() {
        public Boolean call() throws IOException {
            Socket socket = null;
            BufferedReader in = null;
            PrintWriter out = null;
            try {
                socket = new Socket("127.0.0.1", port);
                out = new PrintWriter(socket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                if (in.readLine() == null) {
                    return false;
                }

                out.write(command);
                out.write("\r\n");
            } finally {
                try {
                    out.close();
                    in.close();
                    socket.close();
                } catch (Exception e) {
                    // Do nothing
                }
            }

            return true;
        }

        private static final long serialVersionUID = 1L;
    };

    boolean result = false;
    try {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<Boolean> future = executor.submit(task);
        result = future.get();
    } catch (Exception e) {
        getLog().error(String.format("Failed to execute emulator command '%s': %s", command, e));
    }

    return result;
}

From source file:com.jivesoftware.os.jive.utils.http.client.ApacheHttpClient31BackedHttpClient.java

private HttpStreamResponse executeStreamWithTimeout(final HttpMethodBase HttpMethod, int timeoutMillis) {
    client.getParams().setParameter("http.method.retry-handler", new DefaultHttpMethodRetryHandler(0, false));
    ExecutorService service = Executors.newSingleThreadExecutor();

    Future<HttpStreamResponse> future = service.submit(new Callable<HttpStreamResponse>() {
        @Override//from w w  w. ja v a 2 s  .c  o m
        public HttpStreamResponse call() throws IOException {
            return executeStream(HttpMethod);
        }
    });

    try {
        return future.get(timeoutMillis, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        String uriInfo = "";
        try {
            uriInfo = " for " + HttpMethod.getURI();
        } catch (Exception ie) {
        }
        LOG.warn("Http connection thread was interrupted or has timed out" + uriInfo, e);
        return null;
    } finally {
        service.shutdownNow();
    }
}

From source file:com.marketplace.Main.java

/**
 * Creates <code>CategoryThread</code> for each Android Marketplace
 * Category./*from w w  w  .  j  a va 2s  . c  om*/
 * 
 * @return a set containing <code>CategoryThread</code>
 */
private Set<Future<?>> createCategoryThread() {
    log.info("Creating threads for fetching apps via category.");

    Fetcher fetcher = new Fetcher();
    ExecutorService executorService = Executors.newFixedThreadPool(22);

    Set<Future<?>> set = new HashSet<Future<?>>();
    Session[] sessions = this.sessionManager.getSessions();
    Iterator<String> categories = Category.getAllCategories();

    for (int i = sessions.length; i > 0; i--) {
        while (categories.hasNext()) {
            set.add(executorService
                    .submit(new CategoryThread(sessions[i - 1], fetcher, categories.next(), this.latest)));
        }
        categories = Category.getAllCategories();
    }

    return set;
}

From source file:com.btoddb.fastpersitentqueue.InMemorySegmentMgrTest.java

@Test
public void testThreading() throws IOException, ExecutionException {
    final int entrySize = 1000;
    final int numEntries = 3000;
    final int numPushers = 3;
    int numPoppers = 3;

    final Random pushRand = new Random(1000L);
    final Random popRand = new Random(1000000L);
    final AtomicInteger pusherFinishCount = new AtomicInteger();
    final AtomicInteger numPops = new AtomicInteger();
    final AtomicLong pushSum = new AtomicLong();
    final AtomicLong popSum = new AtomicLong();

    mgr.setMaxSegmentSizeInBytes(10000);
    mgr.init();// w  ww  . j  a v a  2 s .  c o  m

    ExecutorService execSrvc = Executors.newFixedThreadPool(numPushers + numPoppers);

    Set<Future> futures = new HashSet<Future>();

    // start pushing
    for (int i = 0; i < numPushers; i++) {
        Future future = execSrvc.submit(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < numEntries; i++) {
                    try {
                        long x = idGen.incrementAndGet();
                        pushSum.addAndGet(x);
                        FpqEntry entry = new FpqEntry(x, new byte[entrySize]);
                        mgr.push(entry);
                        if (x % 500 == 0) {
                            System.out.println("pushed ID = " + x);
                        }
                        Thread.sleep(pushRand.nextInt(5));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                pusherFinishCount.incrementAndGet();
            }
        });
        futures.add(future);
    }

    // start popping
    for (int i = 0; i < numPoppers; i++) {
        Future future = execSrvc.submit(new Runnable() {
            @Override
            public void run() {
                while (pusherFinishCount.get() < numPushers || !mgr.isEmpty()) {
                    try {
                        FpqEntry entry;
                        while (null != (entry = mgr.pop())) {
                            if (entry.getId() % 500 == 0) {
                                System.out.println("popped ID = " + entry.getId());
                            }

                            popSum.addAndGet(entry.getId());
                            numPops.incrementAndGet();
                            Thread.sleep(popRand.nextInt(5));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        futures.add(future);
    }

    boolean finished = false;
    while (!finished) {
        try {
            for (Future f : futures) {
                f.get();
            }
            finished = true;
        } catch (InterruptedException e) {
            // ignore
            Thread.interrupted();
        }
    }

    assertThat(numPops.get(), is(numEntries * numPushers));
    assertThat(popSum.get(), is(pushSum.get()));
    assertThat(mgr.getNumberOfEntries(), is(0L));
    assertThat(mgr.getNumberOfActiveSegments(), is(1));
    assertThat(mgr.getSegments(), hasSize(1));
    assertThat(FileUtils.listFiles(theDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE), is(empty()));

    // make sure we tested paging in/out
    assertThat(mgr.getNumberOfSwapOut(), is(greaterThan(0L)));
    assertThat(mgr.getNumberOfSwapIn(), is(mgr.getNumberOfSwapOut()));
}

From source file:fr.mby.opa.pics.web.controller.UploadPicturesController.java

/***************************************************
 * URL: /upload/jqueryUpload upload(): receives files
 * /*from  w w  w. j a  v  a2 s .  c o  m*/
 * @param request
 *            : MultipartHttpServletRequest auto passed
 * @param response
 *            : HttpServletResponse auto passed
 * @return LinkedList<FileMeta> as json format
 ****************************************************/
@ResponseBody
@RequestMapping(value = "/jqueryUpload", method = RequestMethod.POST)
public FileMetaList jqueryUpload(@RequestParam final Long albumId, final MultipartHttpServletRequest request,
        final HttpServletResponse response) throws Exception {
    Assert.notNull(albumId, "No Album Id supplied !");

    final FileMetaList files = new FileMetaList();

    // 1. build an iterator
    final Iterator<String> itr = request.getFileNames();

    // 2. get each file
    while (itr.hasNext()) {

        // 2.1 get next MultipartFile
        final MultipartFile mpf = request.getFile(itr.next());

        // Here the file is uploaded

        final String originalFilename = mpf.getOriginalFilename();
        final String contentType = mpf.getContentType();

        final Matcher zipMatcher = UploadPicturesController.ZIP_CONTENT_TYPE_PATTERN.matcher(contentType);
        if (zipMatcher.find()) {

            // 2.3 create new fileMeta
            final FileMeta zipMeta = new FileMeta();
            zipMeta.setFileName(originalFilename);
            zipMeta.setFileSize(mpf.getSize() / 1024 + " Kb");
            zipMeta.setFileType(mpf.getContentType());

            final List<Path> picturesPaths = this.processArchive(mpf);

            final Collection<Future<Void>> futures = new ArrayList<>(picturesPaths.size());
            final ExecutorService executorService = Executors
                    .newFixedThreadPool(Runtime.getRuntime().availableProcessors());

            for (final Path picturePath : picturesPaths) {
                final Future<Void> future = executorService.submit(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        final String pictureFileName = picturePath.getName(picturePath.getNameCount() - 1)
                                .toString();
                        final byte[] pictureContents = Files.readAllBytes(picturePath);

                        final FileMeta pictureMeta = new FileMeta();
                        try {
                            final Picture picture = UploadPicturesController.this.createPicture(albumId,
                                    pictureFileName.toString(), pictureContents);
                            final Long imageId = picture.getImage().getId();
                            final Long thumbnailId = picture.getThumbnail().getId();

                            pictureMeta.setFileName(pictureFileName);
                            pictureMeta.setFileSize(pictureContents.length / 1024 + " Kb");
                            pictureMeta.setFileType(Files.probeContentType(picturePath));
                            pictureMeta.setUrl(
                                    response.encodeURL(ImageController.IMAGE_CONTROLLER_PATH + "/" + imageId));
                            pictureMeta.setThumbnailUrl(response
                                    .encodeURL(ImageController.IMAGE_CONTROLLER_PATH + "/" + thumbnailId));
                        } catch (final PictureAlreadyExistsException e) {
                            // Picture already exists !
                            pictureMeta.setError(
                                    UploadPicturesController.PICTURE_ALREADY_EXISTS_MSG + e.getFilename());
                        } catch (final UnsupportedPictureTypeException e) {
                            // Picture already exists !
                            pictureMeta.setError(
                                    UploadPicturesController.UNSUPPORTED_PICTURE_TYPE_MSG + e.getFilename());
                        }

                        files.add(pictureMeta);

                        return null;
                    }
                });
                futures.add(future);
            }

            for (final Future<Void> future : futures) {
                future.get();
            }

            files.add(zipMeta);
        }

        final Matcher imgMatcher = UploadPicturesController.IMG_CONTENT_TYPE_PATTERN.matcher(contentType);
        if (imgMatcher.find()) {
            // 2.3 create new fileMeta
            final FileMeta fileMeta = new FileMeta();
            try {
                final byte[] fileContents = mpf.getBytes();
                final Picture picture = this.createPicture(albumId, originalFilename, fileContents);

                final Long imageId = picture.getImage().getId();
                final Long thumbnailId = picture.getThumbnail().getId();

                fileMeta.setFileName(originalFilename);
                fileMeta.setFileSize(mpf.getSize() / 1024 + " Kb");
                fileMeta.setFileType(mpf.getContentType());
                fileMeta.setBytes(fileContents);
                fileMeta.setUrl(response.encodeURL(ImageController.IMAGE_CONTROLLER_PATH + "/" + imageId));
                fileMeta.setThumbnailUrl(
                        response.encodeURL(ImageController.IMAGE_CONTROLLER_PATH + "/" + thumbnailId));

                // 2.4 add to files
                files.add(fileMeta);
            } catch (final PictureAlreadyExistsException e) {
                // Picture already exists !
                fileMeta.setError(UploadPicturesController.PICTURE_ALREADY_EXISTS_MSG);
            }
        }

    }

    // result will be like this
    // {files:[{"fileName":"app_engine-85x77.png","fileSize":"8 Kb","fileType":"image/png"},...]}
    return files;
}

From source file:com.mobilecashout.osprey.remote.RemoteClient.java

private synchronized void executeInParallel(RemoteRunnable runnable, DeploymentContext context,
        String[] roles) {//  w w  w.  ja  v a2 s . c  o m
    if (null == roles) {
        roles = new String[] { "all" };
    }
    while (!semaphore.tryAcquire()) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            context.setFailed();
            throw new RuntimeException(e);
        }
    }

    final ExecutorService pool = Executors.newFixedThreadPool(sessions.size());
    final ArrayList<Future<Boolean>> futures = new ArrayList<>();

    for (RemoteTarget remoteTarget : sessions) {
        if (!remoteTarget.getTarget().hasAnyRole(roles)) {
            continue;
        }
        Future<Boolean> executor = pool.submit(() -> {
            runnable.run(remoteTarget, context);
            return true;
        });
        futures.add(executor);
    }

    pool.shutdown();

    try {
        pool.awaitTermination(Integer.MAX_VALUE, TimeUnit.MINUTES);
        for (Future future : futures) {
            future.get();
        }
        Thread.sleep(100);
    } catch (InterruptedException | ExecutionException e) {
        context.setFailed();
        logger.fatal(e.getMessage(), e);
    } finally {
        semaphore.release();
    }
}