Example usage for java.util.concurrent ExecutorService awaitTermination

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

Introduction

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

Prototype

boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;

Source Link

Document

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

Usage

From source file:org.carbondata.processing.globalsurrogategenerator.LevelGlobalSurrogateGeneratorThread.java

private void createGlobalSurrogateKey(long currentTimeMillis, long currentTimeMillis1,
        boolean isPartitionColumn, String levelFileName, List<PartitionMemberVo> partitionMemberVoList,
        ExecutorService ex, int maxSeqenceKey) throws InterruptedException {
    int[] key = new int[partitionMemberVoList.get(0).getMembersMap().size()];
    int[] value = new int[partitionMemberVoList.get(0).getMembersMap().size()];

    int[] localKey = null;
    int[] localValue = null;
    int countVal = 0;
    int minSeqenceKey = Integer.MAX_VALUE;
    if (!isPartitionColumn) {
        for (Entry<String, Integer> entryInfo : partitionMemberVoList.get(0).getMembersMap().entrySet()) {
            if (minSeqenceKey > entryInfo.getValue()) {
                minSeqenceKey = entryInfo.getValue();
            }//from w ww  . j  a  va  2  s  .  co m
            key[countVal] = entryInfo.getValue();
            value[countVal] = ++maxSeqenceKey;
            countVal++;
        }

        localKey = key;
        localValue = value;
        LOGGER.info("Time Taken to generate global surrogate for Level: " + levelFileName + " : "
                + (System.currentTimeMillis() - currentTimeMillis));

        currentTimeMillis = System.currentTimeMillis();

        ex.submit(new WriterThread(localKey, localValue, partitionMemberVoList.get(0).getPath(),
                levelFileName + ".globallevel", maxSeqenceKey, minSeqenceKey));

        processNonPartitionedColumn(currentTimeMillis, levelFileName, partitionMemberVoList, ex, maxSeqenceKey);
    } else {
        for (int i = 0; i < partitionMemberVoList.size(); i++) {
            countVal = 0;
            minSeqenceKey = Integer.MAX_VALUE;
            key = new int[partitionMemberVoList.get(i).getMembersMap().size()];
            value = new int[partitionMemberVoList.get(i).getMembersMap().size()];
            for (Entry<String, Integer> entry : partitionMemberVoList.get(i).getMembersMap().entrySet()) {
                if (minSeqenceKey > entry.getValue()) {
                    minSeqenceKey = entry.getValue();
                }
                key[countVal] = entry.getValue();
                value[countVal] = ++maxSeqenceKey;
                countVal++;
            }

            localKey = key;
            localValue = value;
            LOGGER.info("Time Taken to generate global surrogate for Level: " + levelFileName + " : "
                    + (System.currentTimeMillis() - currentTimeMillis));
            currentTimeMillis = System.currentTimeMillis();
            ex.submit(new WriterThread(localKey, localValue, partitionMemberVoList.get(i).getPath(),
                    levelFileName + ".globallevel", maxSeqenceKey, minSeqenceKey));
        }
    }

    LOGGER.info("Time Taken to write global surrogate for Level: " + levelFileName + " : "
            + (System.currentTimeMillis() - currentTimeMillis1));

    ex.shutdown();
    ex.awaitTermination(1, TimeUnit.DAYS);
}

From source file:com.linkedin.pinot.integration.tests.UploadRefreshDeleteIntegrationTest.java

@Test(enabled = false)
public void testUploadRefreshDelete() throws Exception {
    final int THREAD_COUNT = 1;
    final int SEGMENT_COUNT = 5;

    final int MIN_ROWS_PER_SEGMENT = 500;
    final int MAX_ROWS_PER_SEGMENT = 1000;

    final int OPERATIONS_PER_ITERATION = 10;
    final int ITERATION_COUNT = 5;

    final double UPLOAD_PROBABILITY = 0.8d;

    final String[] segmentNames = new String[SEGMENT_COUNT];
    final int[] segmentRowCounts = new int[SEGMENT_COUNT];

    for (int i = 0; i < SEGMENT_COUNT; i++) {
        segmentNames[i] = "segment_" + i;
        segmentRowCounts[i] = 0;//from   w  w  w  .ja v  a2 s  . co  m
    }

    for (int i = 0; i < ITERATION_COUNT; i++) {
        // Create THREAD_COUNT threads
        ExecutorService executorService = Executors.newFixedThreadPool(THREAD_COUNT);

        // Submit OPERATIONS_PER_ITERATION uploads/deletes
        for (int j = 0; j < OPERATIONS_PER_ITERATION; j++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        ThreadLocalRandom random = ThreadLocalRandom.current();

                        // Pick a random segment
                        int segmentIndex = random.nextInt(SEGMENT_COUNT);
                        String segmentName = segmentNames[segmentIndex];

                        // Pick a random operation
                        if (random.nextDouble() < UPLOAD_PROBABILITY) {
                            // Upload this segment
                            LOGGER.info("Will upload segment {}", segmentName);

                            synchronized (segmentName) {
                                // Create a segment with a random number of rows
                                int segmentRowCount = random.nextInt(MIN_ROWS_PER_SEGMENT,
                                        MAX_ROWS_PER_SEGMENT);
                                LOGGER.info("Generating and uploading segment {} with {} rows", segmentName,
                                        segmentRowCount);
                                generateAndUploadRandomSegment(segmentName, segmentRowCount);

                                // Store the number of rows
                                LOGGER.info("Uploaded segment {} with {} rows", segmentName, segmentRowCount);
                                segmentRowCounts[segmentIndex] = segmentRowCount;
                            }
                        } else {
                            // Delete this segment
                            LOGGER.info("Will delete segment {}", segmentName);

                            synchronized (segmentName) {
                                // Delete this segment
                                LOGGER.info("Deleting segment {}", segmentName);
                                String reply = sendDeleteRequest(
                                        ControllerRequestURLBuilder.baseUrl(CONTROLLER_BASE_API_URL)
                                                .forSegmentDelete("myresource", segmentName));
                                LOGGER.info("Deletion returned {}", reply);

                                // Set the number of rows to zero
                                LOGGER.info("Deleted segment {}", segmentName);
                                segmentRowCounts[segmentIndex] = 0;
                            }
                        }
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }

        // Await for all tasks to complete
        executorService.shutdown();
        executorService.awaitTermination(5L, TimeUnit.MINUTES);

        // Count number of expected rows
        int expectedRowCount = 0;
        for (int segmentRowCount : segmentRowCounts) {
            expectedRowCount += segmentRowCount;
        }

        // Wait for up to one minute for the row count to match the expected row count
        LOGGER.info("Awaiting for the row count to match {}", expectedRowCount);
        int pinotRowCount = (int) getCurrentServingNumDocs();
        long timeInOneMinute = System.currentTimeMillis() + 60 * 1000L;
        while (System.currentTimeMillis() < timeInOneMinute && pinotRowCount != expectedRowCount) {
            LOGGER.info("Row count is {}, expected {}, awaiting for row count to match", pinotRowCount,
                    expectedRowCount);
            Thread.sleep(5000L);

            try {
                pinotRowCount = (int) getCurrentServingNumDocs();
            } catch (Exception e) {
                LOGGER.warn("Caught exception while sending query to Pinot, retrying", e);
            }
        }

        // Compare row counts
        Assert.assertEquals(pinotRowCount, expectedRowCount,
                "Expected and actual row counts don't match after waiting one minute");
    }
}

From source file:org.bimserver.tools.ifcloader.BulkLoader.java

private void start() {
    Path basePath = Paths.get("C:\\Bulk");
    Path bulkPath = basePath.resolve("bulk");
    Path regularPath = basePath.resolve("single");
    try (JsonBimServerClientFactory factory = new JsonBimServerClientFactory("http://localhost:8080")) {
        ExecutorService executorService = new ThreadPoolExecutor(16, 16, 1, TimeUnit.HOURS,
                new ArrayBlockingQueue<>(10000));
        try (BimServerClient client = factory
                .create(new UsernamePasswordAuthenticationInfo("admin@bimserver.org", "admin"))) {
            if (Files.exists(bulkPath)) {
                DirectoryStream<Path> stream = Files.newDirectoryStream(bulkPath);
                for (Path path : stream) {
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                SProject project = client.getServiceInterface()
                                        .addProject(path.getFileName().toString(), "ifc2x3tc1");
                                client.bulkCheckin(project.getOid(), path, "Automatic bulk checkin");
                            } catch (ServerException e) {
                                e.printStackTrace();
                            } catch (UserException e) {
                                e.printStackTrace();
                            } catch (PublicInterfaceNotFoundException e) {
                                e.printStackTrace();
                            }//from  w ww .  j  ava2  s . com
                        }
                    });
                }
            }
            if (Files.exists(regularPath)) {
                DirectoryStream<Path> regularStream = Files.newDirectoryStream(regularPath);
                for (Path regularFile : regularStream) {
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            String filename = regularFile.getFileName().toString().toLowerCase();
                            try {
                                if (filename.endsWith(".ifc") || filename.endsWith(".ifczip")) {
                                    String schema = client.getServiceInterface().determineIfcVersion(
                                            extractHead(regularFile),
                                            filename.toLowerCase().endsWith(".ifczip"));
                                    SProject project = client.getServiceInterface().addProject(filename,
                                            schema);
                                    SDeserializerPluginConfiguration deserializer = client.getServiceInterface()
                                            .getSuggestedDeserializerForExtension("ifc", project.getOid());
                                    client.checkinSync(project.getOid(), "Automatic checkin",
                                            deserializer.getOid(), false, regularFile);
                                } else {
                                    LOGGER.info("Skipping " + filename);
                                }
                            } catch (Exception e) {
                                LOGGER.error(filename, e);
                            }
                        }
                    });
                }
            }
            executorService.shutdown();
            executorService.awaitTermination(24, TimeUnit.HOURS);
        }
    } catch (BimServerClientException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.apache.hadoop.hbase.util.TestHBaseFsckOneRS.java

/**
 * This test makes sure that with enough retries both parallel instances
 * of hbck will be completed successfully.
 *
 * @throws Exception/*from  w ww . j  a v  a  2 s  .  com*/
 */
@Test(timeout = 180000)
public void testParallelWithRetriesHbck() throws Exception {
    final ExecutorService service;
    final Future<HBaseFsck> hbck1, hbck2;

    // With the ExponentialBackoffPolicyWithLimit (starting with 200 milliseconds sleep time, and
    // max sleep time of 5 seconds), we can retry around 15 times within 80 seconds before bail out.
    //
    // Note: the reason to use 80 seconds is that in HADOOP-2.6 and later, the create file would
    // retry up to HdfsConstants.LEASE_SOFTLIMIT_PERIOD (60 seconds).  See HBASE-13574 for more
    // details.
    final int timeoutInSeconds = 80;
    final int sleepIntervalInMilliseconds = 200;
    final int maxSleepTimeInMilliseconds = 6000;
    final int maxRetryAttempts = 15;

    class RunHbck implements Callable<HBaseFsck> {

        @Override
        public HBaseFsck call() throws Exception {
            // Increase retry attempts to make sure the non-active hbck doesn't get starved
            Configuration c = new Configuration(conf);
            c.setInt("hbase.hbck.lockfile.maxwaittime", timeoutInSeconds);
            c.setInt("hbase.hbck.lockfile.attempt.sleep.interval", sleepIntervalInMilliseconds);
            c.setInt("hbase.hbck.lockfile.attempt.maxsleeptime", maxSleepTimeInMilliseconds);
            c.setInt("hbase.hbck.lockfile.attempts", maxRetryAttempts);
            return doFsck(c, false);
        }
    }

    service = Executors.newFixedThreadPool(2);
    hbck1 = service.submit(new RunHbck());
    hbck2 = service.submit(new RunHbck());
    service.shutdown();
    //wait for some time, for both hbck calls finish
    service.awaitTermination(timeoutInSeconds * 2, TimeUnit.SECONDS);
    HBaseFsck h1 = hbck1.get();
    HBaseFsck h2 = hbck2.get();
    // Both should be successful
    assertNotNull(h1);
    assertNotNull(h2);
    assert (h1.getRetCode() >= 0);
    assert (h2.getRetCode() >= 0);

}

From source file:com.dumontierlab.pdb2rdf.Pdb2Rdf.java

private static void printRdf(final CommandLine cmd, final Map<String, Double> stats) {
    final File outDir = getOutputDirectory(cmd);
    final RDFWriter writer = getWriter(cmd);
    final ProgressMonitor monitor = getProgressMonitor();
    Pdb2RdfInputIterator i = processInput(cmd);
    final int inputSize = i.size();
    final AtomicInteger progressCount = new AtomicInteger();
    ExecutorService pool = null;
    if (outDir != null) {
        pool = getThreadPool(cmd);/*from   w  w w . j  a v a2 s. c  o  m*/
    } else {
        // if output is going to the STDOUT then we need to do process in
        // sequential mode.
        pool = Executors.newSingleThreadExecutor();
    }

    final Object lock = new Object();

    while (i.hasNext()) {
        final InputSource input = i.next();
        pool.execute(new Runnable() {
            @Override
            public void run() {
                OutputStream out = System.out;
                PdbXmlParser parser = new PdbXmlParser();
                PdbRdfModel model = null;
                try {
                    if (cmd.hasOption("detailLevel")) {
                        try {
                            DetailLevel detailLevel = Enum.valueOf(DetailLevel.class,
                                    cmd.getOptionValue("detailLevel"));
                            model = parser.parse(input, new PdbRdfModel(), detailLevel);
                        } catch (IllegalArgumentException e) {
                            LOG.fatal("Invalid argument value for detailLevel option", e);
                            System.exit(1);
                        }
                    } else {
                        model = parser.parse(input, new PdbRdfModel());
                    }
                    // add the input file information
                    model.addInputFileInformation();
                    // add the outputFile information();
                    model.addRDFFileInformation();
                    if (outDir != null) {
                        File directory = new File(outDir, model.getPdbId().substring(1, 3));
                        synchronized (lock) {
                            if (!directory.exists()) {
                                directory.mkdir();
                            }
                        }
                        File file = new File(directory, model.getPdbId() + ".rdf.gz");
                        out = new GZIPOutputStream(new FileOutputStream(file));
                    }
                    if (cmd.hasOption("format")) {
                        if (cmd.getOptionValue("format").equalsIgnoreCase("NQUADs")) {
                            Dataset ds = TDBFactory.createDataset();
                            ds.addNamedModel(model.getDatasetResource().toString(), model);
                            StringWriter sw = new StringWriter();
                            RDFDataMgr.write(sw, ds, Lang.NQUADS);

                            out.write(sw.toString().getBytes(Charset.forName("UTF-8")));
                            ds.close();

                        }
                    }

                    writer.write(model, out, null);

                    if (stats != null) {
                        updateStats(stats, model);
                    }
                    if (monitor != null) {
                        monitor.setProgress(progressCount.incrementAndGet(), inputSize);
                    }

                } catch (Exception e) {
                    String id = null;
                    if (model != null) {
                        id = model.getPdbId();
                    }
                    LOG.error("Unable to parse input for PDB: " + id, e);
                } finally {
                    try {
                        out.close();
                    } catch (IOException e) {
                        LOG.error("Unable to close output stream", e);
                    }
                }
            }
        });
    }
    pool.shutdown();
    while (!pool.isTerminated()) {
        try {
            pool.awaitTermination(1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            break;
        }
    }
}

From source file:ca.zadrox.dota2esportticker.service.UpdateTeamsService.java

private void updateSearchedTeams(String searchName) {

    LOGD(TAG, "starting search update");

    // actually, first, check for connectivity:
    if (!checkForConnectivity()) {
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(STATUS_NO_CONNECTIVITY));
        LOGD(TAG, "returning due to no connectivity");
        return;//from  w  ww  .  j  a v a  2  s  .  c o  m
    }

    LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(STATUS_UPDATING));

    final String BASE_URL = "http://www.gosugamers.net/dota2/rankings" + "?tname="
            + searchName.replace(' ', '+') + "&tunranked=0#team";
    final String TEAM_LINK_BASE_URL = "http://www.gosugamers.net/dota2/teams/";

    try {

        String rawHtml = new OkHttpClient().newCall(new Request.Builder().url(BASE_URL).build()).execute()
                .body().string();

        String processedHtml = rawHtml.substring(rawHtml.indexOf("<div id=\"col1\" class=\"rows\">"),
                rawHtml.indexOf("<div id=\"col2\" class=\"rows\">"));

        Elements teamRows = Jsoup.parse(processedHtml).getElementsByClass("ranking-link");

        ExecutorService executorService = Executors.newFixedThreadPool(10);

        HashMap<ContentValues, Future<String>> newTeamInfo = new HashMap<ContentValues, Future<String>>();
        HashMap<ContentValues, Future<String>> updateTeamInfo = new HashMap<ContentValues, Future<String>>();

        for (Element teamRow : teamRows) {
            ContentValues contentValues = new ContentValues();

            String teamId = teamRow.attr("data-id");
            contentValues.put(MatchContract.TeamEntry._ID, teamId);

            String untrimmedTeamName = teamRow.getElementsByTag("h4").first().text();
            String teamUrl = TEAM_LINK_BASE_URL + teamId + "-"
                    + untrimmedTeamName.replaceAll("[\\W]?[\\W][\\W]*", "-").toLowerCase();
            contentValues.put(MatchContract.TeamEntry.COLUMN_TEAM_URL, teamUrl);

            String teamName = untrimmedTeamName.replaceAll(" ?\\.?\\-?-?Dot[aA][\\s]?2", "");
            contentValues.put(MatchContract.TeamEntry.COLUMN_TEAM_NAME, teamName);

            if (teamUrl.charAt(teamUrl.length() - 1) == '-') {
                teamUrl = teamUrl.substring(0, teamUrl.length() - 2);
            }

            // then, we query db for id of the team (
            Cursor cursor = getContentResolver().query(
                    MatchContract.TeamEntry.buildTeamUri(Long.parseLong(teamId)), new String[] {
                            MatchContract.TeamEntry.COLUMN_TEAM_NAME, MatchContract.TeamEntry.COLUMN_TEAM_URL },
                    null, null, null);

            // -> if present, and data remains unchanged, continue.
            // -> if present, but data is changed, add to update queue.
            if (cursor.moveToFirst()) {
                LOGD(TAG, "Team in DB, determining if values need updating");
                if (!cursor.getString(0).contentEquals(teamName)
                        || !cursor.getString(1).contentEquals(teamUrl)) {
                    LOGD(TAG, "Team has updated values, double checking logo & writing to DB");
                    updateTeamInfo.put(contentValues, executorService.submit(new TeamGetter(teamUrl)));
                }
            }
            // -> if not present, add to update queue.
            else {
                LOGD(TAG, "Team not in DB. Grabbing logo & writing to DB");
                newTeamInfo.put(contentValues, executorService.submit(new TeamGetter(teamUrl)));
            }

            //                LOGD(TAG, "\n" +
            //                        "data-id: " + teamId + "\n" +
            //                        "team-name: " + teamName + "\n" +
            //                        "team-url: " + teamUrl);
            //
            cursor.close();
        }

        executorService.shutdown();
        executorService.awaitTermination(20, TimeUnit.SECONDS);

        for (ContentValues contentValues : newTeamInfo.keySet()) {
            try {
                String teamLogo = newTeamInfo.get(contentValues).get();
                contentValues.put(MatchContract.TeamEntry.COLUMN_TEAM_LOGO_URL, teamLogo);

            } catch (ExecutionException e) {
                LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(STATUS_ERROR));
                e.printStackTrace();
            }
        }

        for (ContentValues contentValues : updateTeamInfo.keySet()) {
            try {
                String teamLogo = newTeamInfo.get(contentValues).get();
                contentValues.put(MatchContract.TeamEntry.COLUMN_TEAM_LOGO_URL, teamLogo);

                String teamId = contentValues.getAsString(MatchContract.TeamEntry._ID);
                contentValues.remove(MatchContract.TeamEntry._ID);

                int updatedRows = getContentResolver().update(MatchContract.TeamEntry.CONTENT_URI,
                        contentValues,
                        MatchContract.TeamEntry.TABLE_NAME + "." + MatchContract.TeamEntry._ID + " = ?",
                        new String[] { teamId });

                LOGD(TAG, "updatedRows: " + updatedRows);

            } catch (ExecutionException e) {
                LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(STATUS_ERROR));
                e.printStackTrace();
            }
        }

        getContentResolver().bulkInsert(MatchContract.TeamEntry.CONTENT_URI,
                newTeamInfo.keySet().toArray(new ContentValues[newTeamInfo.size()]));

    } catch (IOException e) {
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(STATUS_ERROR));
        e.printStackTrace();
    } catch (InterruptedException e2) {
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(STATUS_ERROR));
        e2.printStackTrace();
    }

    //        String[] projection = new String[]{
    //                MatchContract.TeamEntry.TABLE_NAME + "." + MatchContract.TeamEntry._ID,
    //                MatchContract.TeamEntry.COLUMN_TEAM_NAME,
    //                MatchContract.TeamEntry.COLUMN_TEAM_URL,
    //                MatchContract.TeamEntry.COLUMN_TEAM_LOGO_URL,
    //                MatchContract.TeamEntry.COLUMN_TEAM_STARRED,
    //        };
    //
    //        String sortOrder =
    //                MatchContract.TeamEntry.COLUMN_TEAM_NAME + " ASC";
    //
    //        Cursor c = getContentResolver().query(
    //                MatchContract.TeamEntry.CONTENT_URI,
    //                projection,
    //                MatchContract.TeamEntry.COLUMN_TEAM_NAME + " LIKE '%" + searchName + "%'",
    //                null,
    //                sortOrder
    //        );
    //
    //        LOGD(TAG+"/UST", "Starting Printout: ");
    //        int i = 0;
    //        while (c.moveToNext()) {
    //            String teamPrintOut =
    //                            "teamId: " + c.getInt(0) + " teamName: " + c.getString(1) + "\n" +
    //                            "teamUrl: " + c.getString(2) + "\n" +
    //                            "teamLogoUrl: " + c.getString(3) + "\n" +
    //                            "isFavourited: " + (c.getInt(4) == 0 ? "false" : "true");
    //            LOGD(TAG + "/UST", teamPrintOut);
    //            i++;
    //        }
    //        LOGD(TAG+"/UST", "Stopping Printout. Count: " + i);
    //
    //        c.close();

    // use local broadcast manager to hide loading indicator
    // and signal that cursorloader for top50 can happen.
    LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(STATUS_COMPLETED));
}

From source file:be.fgov.kszbcss.rhq.websphere.WebSphereServerPlugin.java

private void checkAndMarkUnconfiguredResources(LinkedList<Resource> resources, final Tag unconfiguredTag) {
    final Subject user = LookupUtil.getSubjectManager().getOverlord();
    final TagManagerLocal tagManager = LookupUtil.getTagManager();
    final ResourceManagerLocal resourceManager = LookupUtil.getResourceManager();

    // To accelerate things, we schedule a certain number of operations in parallel.
    // JBoss 6 has not yet ManagedExecutorService, so we're using unmanaged threads.
    ExecutorService executorService = Executors.newFixedThreadPool(6);
    for (final Resource resource : resources) {
        if (resource.getResourceType().isSupportsMissingAvailabilityType()) {
            executorService.submit(new Runnable() {
                @Override// w w  w. j a  va  2s  .  c  om
                public void run() {
                    try {
                        // the synchronous calls to retrieve availability don't convert AvailabilityType.MISSING to
                        // DOWN, so we can make use of it to detect unconfigured resources
                        if (resource.getCurrentAvailability()
                                .getAvailabilityType() != AvailabilityType.DISABLED) {
                            AvailabilityType currentAvailability = getCurrentAvailaibility(resource);
                            if (currentAvailability == AvailabilityType.MISSING) {
                                LOG.info("Tagging " + resource.getName() + " (" + resource.getId()
                                        + ") as unconfigured");
                                Set<Tag> tags = resource.getTags();
                                tags.add(unconfiguredTag);
                                // using synchronized, because at least resourceManager didn't seem threadsafe
                                synchronized (tagManager) {
                                    tagManager.updateResourceTags(user, resource.getId(), tags);
                                }
                                synchronized (resourceManager) {
                                    resourceManager.disableResources(user, new int[] { resource.getId() });
                                }
                            }
                        } else {
                            AvailabilityType currentAvailability = getCurrentAvailaibility(resource);
                            if (currentAvailability != AvailabilityType.UNKNOWN
                                    && currentAvailability != AvailabilityType.MISSING) {
                                LOG.info(resource.getName() + " (" + resource.getId()
                                        + ") has reappeared in the WebSphere configuration; reenabling it");
                                Set<Tag> tags = resource.getTags();
                                tags.remove(unconfiguredTag);
                                synchronized (tagManager) {
                                    tagManager.updateResourceTags(user, resource.getId(), tags);
                                }
                                synchronized (resourceManager) {
                                    resourceManager.enableResources(user, new int[] { resource.getId() });
                                }
                            }
                        }
                    } catch (RuntimeException e) {
                        LOG.error("Exception during availability check of resource " + resource.getName() + "("
                                + resource.getId() + ")");
                    }
                }
            });
        }
    }
    try {
        executorService.shutdown();
        executorService.awaitTermination(5, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        throw new IllegalStateException("Interrupted during availability check of autoUninventory", e);
    }
}

From source file:org.apache.lens.server.query.QueryExecutionServiceImpl.java

private void awaitTermination(ExecutorService service) {
    try {//from   www.j ava 2  s  .  c  om
        service.awaitTermination(1, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        log.info("Couldn't finish executor service within 1 minute: {}", service);
    }
}

From source file:org.apache.usergrid.rest.UniqueValuesIT.java

@Test
@Ignore("Intended for use against  prod-like cluster")
public void testDuplicatePrevention() throws Exception {

    int numThreads = 6;
    int poolSize = 40;
    int numUsers = 400;

    Multimap<String, Form> usersCreated = Multimaps.synchronizedMultimap(HashMultimap.create());
    Multimap<String, Form> dupsRejected = Multimaps.synchronizedMultimap(HashMultimap.create());

    ExecutorService execService = Executors.newFixedThreadPool(poolSize);

    Client client = ClientBuilder.newClient();

    final MetricRegistry metrics = new MetricRegistry();
    final Timer responses = metrics.timer(name(UniqueValuesIT.class, "responses"));
    long startTime = System.currentTimeMillis();

    final AtomicBoolean failed = new AtomicBoolean(false);

    String[] targetHosts = { "http://macsnoopdave2013:8080", "http://macsnoopdave2010:9090" };

    for (int i = 0; i < numUsers; i++) {

        if (failed.get()) {
            break;
        }/*w ww  .  j  ava  2s  .c o  m*/

        String randomizer = RandomStringUtils.randomAlphanumeric(8);

        // multiple threads simultaneously trying to create a user with the same propertyName
        for (int j = 0; j < numThreads; j++) {

            if (failed.get()) {
                break;
            }

            String username = "uv_test_user_" + randomizer;
            final String host = targetHosts[j % targetHosts.length];

            execService.submit(() -> {

                Form form = new Form();
                //form.param( "name", username );
                form.param("username", username);
                form.param("email", username + RandomStringUtils.randomAlphanumeric(8) + "@example.org");
                form.param("password", "s3cr3t");

                Timer.Context time = responses.time();
                try {
                    WebTarget target = client.target(host).path("/management/users");

                    //logger.info("Posting user {} to host {}", username, host);

                    Response response = target.request()
                            .post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED));

                    String responseAsString = response.readEntity(String.class);

                    if (response.getStatus() == 200 || response.getStatus() == 201) {
                        usersCreated.put(username, form);
                        successCounter.incrementAndGet();

                    } else if (response.getStatus() == 400
                            && responseAsString.contains("DuplicateUniquePropertyExistsException")) {
                        dupsRejected.put(username, form);
                        dupCounter.incrementAndGet();

                    } else {
                        logger.error("User creation failed status {} message {}", response.getStatus(),
                                responseAsString);
                        errorCounter.incrementAndGet();
                    }

                } catch (ProcessingException e) {
                    errorCounter.incrementAndGet();
                    if (e.getCause() instanceof ConnectException) {
                        logger.error("Error connecting to " + host);
                    } else {
                        logger.error("Error", e);
                    }

                } catch (Exception e) {
                    errorCounter.incrementAndGet();
                    logger.error("Error", e);
                }
                time.stop();

            });
        }
    }
    execService.shutdown();

    try {
        while (!execService.awaitTermination(60, TimeUnit.SECONDS)) {
            System.out.println("Waiting...");
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    long endTime = System.currentTimeMillis();

    logger.info("Total time {}s", (endTime - startTime) / 1000);

    DecimalFormat format = new DecimalFormat("##.###");

    logger.info(
            "Timed {} requests:\n" + "mean rate {}/s\n" + "min       {}s\n" + "max       {}s\n"
                    + "mean      {}s",
            responses.getCount(), format.format(responses.getMeanRate()),
            format.format((double) responses.getSnapshot().getMin() / 1000000000),
            format.format((double) responses.getSnapshot().getMax() / 1000000000),
            format.format(responses.getSnapshot().getMean() / 1000000000));

    logger.info("Error count {} ratio = {}", errorCounter.get(),
            (float) errorCounter.get() / (float) responses.getCount());

    logger.info("Success count = {}", successCounter.get());

    logger.info("Rejected dup count = {}", dupCounter.get());

    //        for ( String username : usersCreated.keys() ) {
    //            System.out.println( username );
    //            Collection<User> users = usersCreated.get( username );
    //            for ( User user : users ) {
    //                System.out.println("   " + user.getUuid() );
    //            }
    //        }

    //        int count = 0;
    //        for ( String username : dupsRejected.keySet() ) {
    //            System.out.println( username );
    //            Collection<User> users = dupsRejected.get( username );
    //            for ( User user : users ) {
    //                System.out.println("   " + (count++) + " rejected " + user.getUsername() + ":" + user.getUuid() );
    //            }
    //        }

    int userCount = 0;
    int usernamesWithDuplicates = 0;
    for (String username : usersCreated.keySet()) {
        Collection<Form> forms = usersCreated.get(username);
        if (forms.size() > 1) {
            usernamesWithDuplicates++;
        }
        userCount++;
    }
    Assert.assertEquals(0, usernamesWithDuplicates);
    Assert.assertEquals(0, errorCounter.get());
    Assert.assertEquals(numUsers, successCounter.get());
    Assert.assertEquals(numUsers, userCount);

}