Example usage for java.util.concurrent Callable Callable

List of usage examples for java.util.concurrent Callable Callable

Introduction

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

Prototype

Callable

Source Link

Usage

From source file:com.cloudera.oryx.als.computation.LoadRunner.java

public void runLoad() throws InterruptedException {

    final StorelessUnivariateStatistic recommendedBecause = new Mean();
    final StorelessUnivariateStatistic setPreference = new Mean();
    final StorelessUnivariateStatistic removePreference = new Mean();
    final StorelessUnivariateStatistic ingest = new Mean();
    final StorelessUnivariateStatistic refresh = new Mean();
    final StorelessUnivariateStatistic estimatePreference = new Mean();
    final StorelessUnivariateStatistic mostSimilarItems = new Mean();
    final StorelessUnivariateStatistic similarityToItem = new Mean();
    final StorelessUnivariateStatistic mostPopularItems = new Mean();
    final StorelessUnivariateStatistic recommendToMany = new Mean();
    final StorelessUnivariateStatistic recommend = new Mean();
    final RandomGenerator random = RandomManager.getRandom();

    int numCores = Runtime.getRuntime().availableProcessors();
    final int stepsPerWorker = steps / numCores;
    Collection<Callable<Object>> workers = Lists.newArrayListWithCapacity(numCores);
    for (int i = 0; i < numCores; i++) {
        workers.add(new Callable<Object>() {
            @Override//from  w  w w .jav a  2 s  .  c  o  m
            public Void call() throws Exception {
                for (int i = 0; i < stepsPerWorker; i++) {
                    double r;
                    String userID;
                    String itemID;
                    String itemID2;
                    float value;
                    synchronized (random) {
                        r = random.nextDouble();
                        userID = uniqueUserIDs[random.nextInt(uniqueUserIDs.length)];
                        itemID = uniqueItemIDs[random.nextInt(uniqueItemIDs.length)];
                        itemID2 = uniqueItemIDs[random.nextInt(uniqueItemIDs.length)];
                        value = random.nextInt(10);
                    }
                    long stepStart = System.currentTimeMillis();
                    if (r < 0.05) {
                        client.recommendedBecause(userID, itemID, 10);
                        recommendedBecause.increment(System.currentTimeMillis() - stepStart);
                    } else if (r < 0.07) {
                        client.setPreference(userID, itemID);
                        setPreference.increment(System.currentTimeMillis() - stepStart);
                    } else if (r < 0.08) {
                        client.setPreference(userID, itemID, value);
                        setPreference.increment(System.currentTimeMillis() - stepStart);
                    } else if (r < 0.11) {
                        client.removePreference(userID, itemID);
                        removePreference.increment(System.currentTimeMillis() - stepStart);
                    } else if (r < 0.12) {
                        Reader reader = new StringReader(
                                DelimitedDataUtils.encode(userID, itemID, Float.toString(value)) + '\n');
                        client.ingest(reader);
                        ingest.increment(System.currentTimeMillis() - stepStart);
                    } else if (r < 0.13) {
                        client.refresh();
                        refresh.increment(System.currentTimeMillis() - stepStart);
                    } else if (r < 0.14) {
                        client.similarityToItem(itemID, itemID2);
                        similarityToItem.increment(System.currentTimeMillis() - stepStart);
                    } else if (r < 0.15) {
                        client.mostPopularItems(10);
                        mostPopularItems.increment(System.currentTimeMillis() - stepStart);
                    } else if (r < 0.19) {
                        client.estimatePreference(userID, itemID);
                        estimatePreference.increment(System.currentTimeMillis() - stepStart);
                    } else if (r < 0.20) {
                        client.estimateForAnonymous(itemID, new String[] { itemID2 });
                        estimatePreference.increment(System.currentTimeMillis() - stepStart);
                    } else if (r < 0.25) {
                        client.mostSimilarItems(new String[] { itemID }, 10);
                        mostSimilarItems.increment(System.currentTimeMillis() - stepStart);
                    } else if (r < 0.30) {
                        client.recommendToMany(new String[] { userID, userID }, 10, true, null);
                        recommendToMany.increment(System.currentTimeMillis() - stepStart);
                    } else {
                        client.recommend(userID, 10);
                        recommend.increment(System.currentTimeMillis() - stepStart);
                    }
                }
                return null;
            }
        });
    }

    log.info("Starting load test...");

    long start = System.currentTimeMillis();
    ExecutorService executor = Executors.newFixedThreadPool(numCores);
    Iterable<Future<Object>> futures;
    try {
        futures = executor.invokeAll(workers);
    } finally {
        ExecutorUtils.shutdownNowAndAwait(executor);
    }
    long end = System.currentTimeMillis();

    ExecutorUtils.checkExceptions(futures);

    log.info("Finished {} steps in {}ms", steps, end - start);

    log.info("recommendedBecause: {}", recommendedBecause.getResult());
    log.info("setPreference: {}", setPreference.getResult());
    log.info("removePreference: {}", removePreference.getResult());
    log.info("ingest: {}", ingest.getResult());
    log.info("refresh: {}", refresh.getResult());
    log.info("estimatePreference: {}", estimatePreference.getResult());
    log.info("mostSimilarItems: {}", mostSimilarItems.getResult());
    log.info("similarityToItem: {}", similarityToItem.getResult());
    log.info("mostPopularItems: {}", mostPopularItems.getResult());
    log.info("recommendToMany: {}", recommendToMany.getResult());
    log.info("recommend: {}", recommend.getResult());
}

From source file:com.leclercb.taskunifier.gui.processes.license.ProcessGetTrial.java

@Override
public HttpResponse execute(final Worker<?> worker) throws Exception {
    final ProgressMonitor monitor = worker.getEDTMonitor();

    monitor.addMessage(new DefaultProgressMessage(Translations.getString("license.get_trial")));

    HttpResponse response = worker.executeInterruptibleAction(new Callable<HttpResponse>() {

        @Override/* w  w w  .  j  ava 2  s.c  o m*/
        public HttpResponse call() throws Exception {
            List<NameValuePair> parameters = new ArrayList<NameValuePair>();

            parameters.add(new BasicNameValuePair("item", Constants.ITEM_TRIAL_ID + ""));
            parameters.add(new BasicNameValuePair("first_name", ProcessGetTrial.this.firstName));
            parameters.add(new BasicNameValuePair("last_name", ProcessGetTrial.this.lastName));
            parameters.add(new BasicNameValuePair("email", ProcessGetTrial.this.email));
            parameters.add(new BasicNameValuePair("user_id", Main.getCurrentUserId()));

            return HttpUtils.getHttpPostResponse(new URI(Constants.GET_TRIAL_URL), parameters);
        }

    }, Constants.TIMEOUT_HTTP_CALL);

    if (worker.isCancelled())
        return null;

    if (!response.isSuccessfull()) {
        this.showResult(null, "An error occurred while retrieving the license key");
        return response;
    }

    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.readTree(response.getContent());

    String code = node.get("code").asText();
    String message = node.get("message").asText();

    if (this.showSuccess && EqualsUtils.equals(code, "0"))
        this.showResult(code, message);

    if (this.showFailure && !EqualsUtils.equals(code, "0"))
        this.showResult(code, message);

    return response;
}

From source file:com.microsoft.azure.utility.compute.ComputeTestBase.java

protected static void createStorageManagementClient() throws Exception {
    storageManagementClient = StorageManagementService.create(config);
    if (IS_MOCKED) {
        storageManagementClient.setLongRunningOperationInitialTimeout(0);
        storageManagementClient.setLongRunningOperationRetryTimeout(0);
    }/*from w  w  w  .ja  v  a  2  s . c o  m*/

    addClient((ServiceClient<?>) storageManagementClient, new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            createStorageManagementClient();
            return null;
        }
    });
}

From source file:com.bigdata.dastor.db.CompactionManager.java

/**
 * Call this whenever a compaction might be needed on the given columnfamily.
 * It's okay to over-call (within reason) since the compactions are single-threaded,
 * and if a call is unnecessary, it will just be no-oped in the bucketing phase.
 *//*from w ww . ja va  2  s .com*/
public Future<Integer> submitMinorIfNeeded(final ColumnFamilyStore cfs) {
    Callable<Integer> callable = new Callable<Integer>() {
        public Integer call() throws IOException {
            if (minimumCompactionThreshold <= 0 || maximumCompactionThreshold <= 0) {
                logger.debug("Compaction is currently disabled.");
                return 0;
            }
            logger.debug("Checking to see if compaction of " + cfs.columnFamily_ + " would be useful");
            Set<List<SSTableReader>> buckets = getBuckets(cfs.getSSTables(), 50L * 1024L * 1024L,
                    cfs.getCFMetaData().compactSkipSize); // BIGDATA: compactSkipSize
            updateEstimateFor(cfs, buckets);

            for (List<SSTableReader> sstables : buckets) {
                if (sstables.size() >= minimumCompactionThreshold) {
                    // if we have too many to compact all at once, compact older ones first -- this avoids
                    // re-compacting files we just created.
                    Collections.sort(sstables);
                    return doCompaction(cfs,
                            sstables.subList(0, Math.min(sstables.size(), maximumCompactionThreshold)),
                            getDefaultGCBefore());
                }
            }
            return 0;
        }
    };
    return getExecutor(cfs).submit(callable);
}

From source file:net.bhira.sample.api.controller.DepartmentController.java

/**
 * Fetch the instance of {@link net.bhira.sample.model.Department} represented by given
 * departmentId and return it as JSON object.
 * //from w ww  .ja  va  2  s.c om
 * @param departmentId
 *            the ID for {@link net.bhira.sample.model.Department}.
 * @param response
 *            the http response to which the results will be written.
 * @return an instance of {@link net.bhira.sample.model.Department} as JSON.
 */
@RequestMapping(value = "/department/{departmentId}", method = RequestMethod.GET)
@ResponseBody
public Callable<String> getDepartment(@PathVariable long departmentId, HttpServletResponse response) {
    return new Callable<String>() {
        public String call() throws Exception {
            String body = "";
            try {
                LOG.debug("servicing GET department/{}", departmentId);
                Department department = departmentService.load(departmentId);
                LOG.debug("GET department/{}, found = {}", departmentId, department != null);
                if (department == null) {
                    response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                } else {
                    body = JsonUtil.createGson().toJson(department);
                }
            } catch (Exception ex) {
                response.setStatus(HttpServletResponse.SC_FORBIDDEN);
                body = ex.getLocalizedMessage();
                LOG.warn("Error loading department/{}. {}", departmentId, body);
                LOG.debug("Load error stacktrace: ", ex);
            }
            return body;
        }
    };
}

From source file:com.atomicleopard.thundr.ftp.FtpSession.java

public FTPFile[] listFiles() {
    return timeLogAndCatch("List files", new Callable<FTPFile[]>() {
        @Override//from   w w w.j  a  va2 s .c  o m
        public FTPFile[] call() throws Exception {
            return preparedClient.listFiles();
        }
    });
}

From source file:com.microsoft.azure.management.sql.FirewallRuleOperationsImpl.java

/**
* Creates or updates an Azure SQL Database Server Firewall rule.
*
* @param resourceGroupName Required. The name of the Resource Group to
* which the server belongs./*from   w  ww .j ava  2 s  .c om*/
* @param serverName Required. The name of the Azure SQL Database Server on
* which the database is hosted.
* @param firewallRule Required. The name of the Azure SQL Database Server
* Firewall Rule.
* @param parameters Required. The required parameters for createing or
* updating a firewall rule.
* @return Represents the response to a List Firewall Rules request.
*/
@Override
public Future<FirewallRuleGetResponse> createOrUpdateAsync(final String resourceGroupName,
        final String serverName, final String firewallRule,
        final FirewallRuleCreateOrUpdateParameters parameters) {
    return this.getClient().getExecutorService().submit(new Callable<FirewallRuleGetResponse>() {
        @Override
        public FirewallRuleGetResponse call() throws Exception {
            return createOrUpdate(resourceGroupName, serverName, firewallRule, parameters);
        }
    });
}

From source file:com.netflix.curator.framework.recipes.locks.TestReaper.java

@Test
public void testSparseUseNoReap() throws Exception {
    final int THRESHOLD = 3000;

    Timing timing = new Timing();
    Reaper reaper = null;//from   w ww .  j a  v  a 2s .  c  om
    Future<Void> watcher = null;
    CuratorFramework client = makeClient(timing, null);
    try {
        client.start();
        client.create().creatingParentsIfNeeded().forPath("/one/two/three");

        Assert.assertNotNull(client.checkExists().forPath("/one/two/three"));

        final Queue<Reaper.PathHolder> holders = new ConcurrentLinkedQueue<Reaper.PathHolder>();
        final ExecutorService pool = Executors.newCachedThreadPool();
        ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1) {
            @Override
            public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
                final Reaper.PathHolder pathHolder = (Reaper.PathHolder) command;
                holders.add(pathHolder);
                final ScheduledFuture<?> f = super.schedule(command, delay, unit);
                pool.submit(new Callable<Void>() {
                    @Override
                    public Void call() throws Exception {
                        f.get();
                        holders.remove(pathHolder);
                        return null;
                    }
                });
                return f;
            }
        };

        reaper = new Reaper(client, service, THRESHOLD);
        reaper.start();
        reaper.addPath("/one/two/three");

        long start = System.currentTimeMillis();
        boolean emptyCountIsCorrect = false;
        while (((System.currentTimeMillis() - start) < timing.forWaiting().milliseconds())
                && !emptyCountIsCorrect) // need to loop as the Holder can go in/out of the Reaper's DelayQueue
        {
            for (Reaper.PathHolder holder : holders) {
                if (holder.path.endsWith("/one/two/three")) {
                    emptyCountIsCorrect = (holder.emptyCount > 0);
                    break;
                }
            }
            Thread.sleep(1);
        }
        Assert.assertTrue(emptyCountIsCorrect);

        client.create().forPath("/one/two/three/foo");

        Thread.sleep(2 * (THRESHOLD / Reaper.EMPTY_COUNT_THRESHOLD));
        Assert.assertNotNull(client.checkExists().forPath("/one/two/three"));
        client.delete().forPath("/one/two/three/foo");

        Thread.sleep(THRESHOLD);
        timing.sleepABit();

        Assert.assertNull(client.checkExists().forPath("/one/two/three"));
    } finally {
        if (watcher != null) {
            watcher.cancel(true);
        }
        IOUtils.closeQuietly(reaper);
        IOUtils.closeQuietly(client);
    }
}

From source file:no.ntnu.idi.socialhitchhiking.map.GeoHelper.java

/**
 * Retrieves a {@link List} of addresses that match the given {@link GeoPoint}. 
 * The first element in the list has the best match (but is not guaranteed to be correct). <br><br>
 * /*from www  .  ja  v  a2s .  co  m*/
 * This method tries to use the {@link Geocoder} to transform a (latitude, longitude) 
 * coordinate into addresses, and if this fails (witch it most likely will under emulation), it 
 * tries to use a method from the {@link GeoHelper}-class.
 * 
 * @param location The location that is transformed into a list of addresses
 * @param maxResults The maximum number of addresses to retrieve (should be small).
 * @param maxAddressLines The maximum number of lines in the addresses. This should be high if you want a complete address! If it is smaller than the total number of lines in the address, it cuts off the last part...) 
 * @return Returns the {@link List} of addresses (as {@link String}s).
 */
public static List<String> getAddressesAtPoint(final GeoPoint location, final int maxResults,
        int maxAddressLines) {
    List<String> addressList = new ArrayList<String>();
    List<Address> possibleAddresses = new ArrayList<Address>();
    Address address = new Address(Locale.getDefault());
    String addressString = "Could not find the address...";
    ExecutorService executor = Executors.newSingleThreadExecutor();

    Callable<List<Address>> callable = new Callable<List<Address>>() {
        @Override
        public List<Address> call() throws IOException {
            return fancyGeocoder.getFromLocation(location.getLatitudeE6() / 1E6,
                    location.getLongitudeE6() / 1E6, maxResults);
        }
    };
    Future<List<Address>> future = executor.submit(callable);
    try {
        possibleAddresses = future.get();
    } catch (InterruptedException e1) {
        possibleAddresses = GeoHelper.getAddressesFromLocation(location.getLatitudeE6() / 1E6,
                location.getLongitudeE6() / 1E6, maxResults);
    } catch (ExecutionException e1) {
        possibleAddresses = GeoHelper.getAddressesFromLocation(location.getLatitudeE6() / 1E6,
                location.getLongitudeE6() / 1E6, maxResults);
    }
    executor.shutdown();

    if (possibleAddresses.size() > 0) {
        for (int i = 0; i < possibleAddresses.size(); i++) {
            addressString = "";
            address = possibleAddresses.get(i);
            for (int j = 0; j <= address.getMaxAddressLineIndex() && j <= maxAddressLines; j++) {
                addressString += address.getAddressLine(j);
                addressString += "\n";
            }
            addressList.add(addressString.trim());
        }
    }
    return addressList;
}