Example usage for java.util.concurrent.atomic AtomicInteger AtomicInteger

List of usage examples for java.util.concurrent.atomic AtomicInteger AtomicInteger

Introduction

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

Prototype

public AtomicInteger() 

Source Link

Document

Creates a new AtomicInteger with initial value 0 .

Usage

From source file:org.eclipse.hono.service.credentials.impl.FileBasedCredentialsService.java

protected void saveToFile(final Future<Void> writeResult) {

    if (!dirty) {
        log.trace("credentials registry does not need to be persisted");
        return;/*from  w w  w.  ja v  a  2s .com*/
    }

    final FileSystem fs = vertx.fileSystem();
    if (!fs.existsBlocking(filename)) {
        fs.createFileBlocking(filename);
    }
    final AtomicInteger idCount = new AtomicInteger();
    JsonArray tenants = new JsonArray();
    for (Entry<String, Map<String, JsonArray>> entry : credentials.entrySet()) {
        JsonArray credentialsArray = new JsonArray();
        for (Entry<String, JsonArray> credentialEntry : entry.getValue().entrySet()) { // authId -> full json attributes object
            JsonArray singleAuthIdCredentials = credentialEntry.getValue(); // from one authId

            credentialsArray.add(singleAuthIdCredentials);
            idCount.incrementAndGet();
        }
        tenants.add(
                new JsonObject().put(FIELD_TENANT, entry.getKey()).put(ARRAY_CREDENTIALS, credentialsArray));
    }
    fs.writeFile(filename, Buffer.factory.buffer(tenants.encodePrettily()), writeAttempt -> {
        if (writeAttempt.succeeded()) {
            dirty = false;
            log.trace("successfully wrote {} credentials to file {}", idCount.get(), filename);
            writeResult.complete();
        } else {
            log.warn("could not write credentials to file {}", filename, writeAttempt.cause());
            writeResult.fail(writeAttempt.cause());
        }
    });
}

From source file:io.wcm.caravan.pipeline.impl.JsonPipelineMultipleSubscriptionsTest.java

@SuppressWarnings("unchecked")
@Test/*from   w w w .  j  a  v a  2  s.  c  o m*/
public void test3StepPipelineActionCallsMixedOrder() {

    final AtomicInteger subscribeCount = new AtomicInteger();
    initPipelines(subscribeCount);

    Observer<JsonPipelineOutput> firstObserver = Mockito.mock(Observer.class);
    Observer<JsonPipelineOutput> secondObserver = Mockito.mock(Observer.class);
    Observer<JsonPipelineOutput> thirdObserver = Mockito.mock(Observer.class);

    secondStep.getOutput().subscribe(secondObserver);
    firstStep.getOutput().subscribe(firstObserver);
    thirdStep.getOutput().subscribe(thirdObserver);

    assertEquals(1, subscribeCount.get());
}

From source file:io.pravega.controller.eventProcessor.impl.SerializedRequestHandlerTest.java

@Test(timeout = 10000)
public void testPostponeEvent() throws InterruptedException, ExecutionException {
    AtomicInteger postponeS1e1Count = new AtomicInteger();
    AtomicInteger postponeS1e2Count = new AtomicInteger();
    AtomicBoolean allowCompletion = new AtomicBoolean(false);

    SerializedRequestHandler<TestEvent> requestHandler = new SerializedRequestHandler<TestEvent>(
            executorService()) {/*from www . ja  va2s  .  co m*/
        @Override
        public CompletableFuture<Void> processEvent(TestEvent event) {
            if (!event.future.isDone()) {
                return Futures.failedFuture(new TestPostponeException());
            }
            return event.getFuture();
        }

        @Override
        public boolean toPostpone(TestEvent event, long pickupTime, Throwable exception) {

            boolean retval = true;

            if (allowCompletion.get()) {
                if (event.number == 1) {
                    postponeS1e1Count.incrementAndGet();
                    retval = exception instanceof TestPostponeException && postponeS1e1Count.get() < 2;
                }

                if (event.number == 2) {
                    postponeS1e2Count.incrementAndGet();
                    retval = exception instanceof TestPostponeException
                            && (System.currentTimeMillis() - pickupTime < Duration.ofMillis(100).toMillis());
                }
            }

            return retval;
        }
    };

    List<Pair<TestEvent, CompletableFuture<Void>>> stream1Queue = requestHandler
            .getEventQueueForKey(getKeyForStream("scope", "stream1"));
    assertNull(stream1Queue);
    // post 3 work for stream1
    TestEvent s1e1 = new TestEvent("scope", "stream1", 1);
    CompletableFuture<Void> s1p1 = requestHandler.process(s1e1);
    TestEvent s1e2 = new TestEvent("scope", "stream1", 2);
    CompletableFuture<Void> s1p2 = requestHandler.process(s1e2);
    TestEvent s1e3 = new TestEvent("scope", "stream1", 3);
    CompletableFuture<Void> s1p3 = requestHandler.process(s1e3);

    // post events for some more arbitrary streams in background
    AtomicBoolean stop = new AtomicBoolean(false);

    runBackgroundStreamProcessing("stream2", requestHandler, stop);
    runBackgroundStreamProcessing("stream3", requestHandler, stop);
    runBackgroundStreamProcessing("stream4", requestHandler, stop);

    s1e3.complete();
    // verify that s1p3 completes.
    assertTrue(Futures.await(s1p3));
    // verify that s1e1 and s1e2 are still not complete.
    assertTrue(!s1e1.getFuture().isDone());
    assertTrue(!s1p1.isDone());
    assertTrue(!s1e2.getFuture().isDone());
    assertTrue(!s1p2.isDone());

    // Allow completion
    allowCompletion.set(true);

    assertFalse(Futures.await(s1p1));
    assertFalse(Futures.await(s1p2));
    AssertExtensions.assertThrows("", s1p1::join, e -> Exceptions.unwrap(e) instanceof TestPostponeException);
    AssertExtensions.assertThrows("", s1p2::join, e -> Exceptions.unwrap(e) instanceof TestPostponeException);
    assertTrue(postponeS1e1Count.get() == 2);
    assertTrue(postponeS1e2Count.get() > 0);
    stop.set(true);
}

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;//from w w w.j ava  2 s .  c om
    if (outDir != null) {
        pool = getThreadPool(cmd);
    } 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:com.comphenix.protocol.error.DetailedErrorReporter.java

/**
 * Report a problem with a given method and plugin, ensuring that we don't exceed the maximum number of error reports.
 * @param sender - the component that observed this exception.
 * @param methodName - the method name./*from   w  w  w  .  j av a2  s.  c  o  m*/
 * @param error - the error itself.
 * @return TRUE if the error was printed, FALSE if it was suppressed.
 */
public boolean reportMinimalNoSpam(Plugin sender, String methodName, Throwable error) {
    String pluginName = PacketAdapter.getPluginName(sender);
    AtomicInteger counter = warningCount.get(pluginName);

    // Thread safe pattern
    if (counter == null) {
        AtomicInteger created = new AtomicInteger();
        counter = warningCount.putIfAbsent(pluginName, created);

        if (counter == null) {
            counter = created;
        }
    }

    final int errorCount = counter.incrementAndGet();

    // See if we should print the full error
    if (errorCount < getMaxErrorCount()) {
        logger.log(Level.SEVERE,
                "[" + pluginName + "] Unhandled exception occured in " + methodName + " for " + pluginName,
                error);
        return true;

    } else {
        // Nope - only print the error count occationally
        if (isPowerOfTwo(errorCount)) {
            logger.log(Level.SEVERE, "[" + pluginName + "] Unhandled exception number " + errorCount
                    + " occured in " + methodName + " for " + pluginName, error);
        }
        return false;
    }
}

From source file:dk.netarkivet.common.utils.arc.ARCUtils.java

/**
 * Return an ARCWriter suitable for the tools ArcMerge and ArcWrap.
 * @param stream the given PrintStream.//from  w w  w.j ava 2 s .co m
 * @param destinationArcfile the given destination ARC file.
 * @return ARCWriter to be used by tools ArcMerge and ArcWrap
 * @throws IOException redirect from ARCWriter constructure
 */
public static ARCWriter getToolsARCWriter(PrintStream stream, File destinationArcfile) throws IOException {
    return new ARCWriter(new AtomicInteger(), stream, destinationArcfile, false, //Don't compress
            // Use current time
            ArchiveUtils.get14DigitDate(System.currentTimeMillis()), null // //No particular file metadata to add
    );
}

From source file:io.uploader.drive.drive.DriveOperations.java

public static File uploadFile(OperationResult operationResult, Drive client, final File driveParent, Path path,
        boolean overwrite, InputStreamProgressFilter.StreamProgressCallback progressCallback) throws Throwable {

    File ret = null;/*from  ww  w  . ja v a2  s.  co  m*/
    AtomicInteger tryCounter = new AtomicInteger();
    while (true) {
        try {
            // check if file already exists, if yes, check the etag
            String mineType = findMineType(path);
            String title = path.getFileName().toString();

            //FileList fileList = DriveUtils.findFilesWithTitleAndMineType(client, title, 
            //      DriveUtils.newId(driveParent), DriveUtils.newMineType(mineType), null);
            FileList fileList = DriveUtils.findFilesWithTitleAndMineType(client, title,
                    DriveUtils.newId(driveParent), null, null);

            if (fileList.getItems() == null || fileList.getItems().isEmpty()) {
                // there exists no file with the name title, we create it
                ret = insertFile(client, path.getFileName().toString(), null, DriveUtils.newId(driveParent),
                        DriveUtils.newMineType(mineType), path.toString(), progressCallback);
            } else if (!overwrite) {
                // there already exists at least one file with the name title, we do nothing
                logger.info("File with the name '" + title + "' and type '" + mineType
                        + "' already exists in directory "
                        + ((driveParent == null) ? ("root") : (driveParent.getTitle())) + " (there are "
                        + fileList.getItems().size() + " copies), it will be ignored");
                ret = fileList.getItems().get(0);
            } else {
                // there exists at least one file with the name title
                if (fileList.getItems().size() > 1) {
                    // here there are more than one file with the name title.
                    // this is an unexpected situation! A warning message will be displayed
                    StringBuilder sb = new StringBuilder();
                    sb.append("The folder '");
                    sb.append(driveParent.getTitle());
                    sb.append("' contains ");
                    sb.append(fileList.getItems().size());
                    sb.append(" files with the same name '");
                    sb.append(path.getFileName().toString());
                    sb.append("'.");

                    // all the files with the name title are identical, we delete the unnecessary copies
                    String refMd5 = fileList.getItems().get(0).getMd5Checksum();
                    boolean allIdentical = true;
                    for (File file : fileList.getItems()) {
                        if (!refMd5.equals(file.getMd5Checksum())) {
                            allIdentical = false;
                            break;
                        }
                    }
                    if (allIdentical) {
                        // remove unnecessary copies
                        boolean toBeTrashed = false;
                        for (File file : fileList.getItems()) {
                            if (toBeTrashed) {
                                logger.info("Trashed duplicated file " + file.getTitle());
                                DriveUtils.trashFile(client, DriveUtils.newId(file.getId()));
                            }
                            toBeTrashed = true;
                        }
                        sb.append(
                                " The duplicated copies have been trashed and the remaining copy has been updated'");
                        operationResult.addWarning(path, OperationResult.newWarning(sb.toString()));

                        //  we update the now unique remaining file if required
                        String localEtag = io.uploader.drive.util.FileUtils.getMD5(path.toFile());
                        ret = updateFile(localEtag, client, fileList.getItems().get(0), null, null,
                                DriveUtils.newMineType(mineType), path.toString(), progressCallback);
                    } else {
                        // there are discrepancies between the files with the name title
                        // we add the new file without modifying the existing ones
                        sb.append(" The file '");
                        sb.append(path.toString());
                        sb.append("' was uploaded as a new file");
                        operationResult.addWarning(path, OperationResult.newWarning(sb.toString()));

                        ret = insertFile(client, path.getFileName().toString(), null,
                                DriveUtils.newId(driveParent), DriveUtils.newMineType(mineType),
                                path.toString(), progressCallback);
                    }
                } else {
                    // there already exists only one file with the name title, we update the file if required
                    String localEtag = io.uploader.drive.util.FileUtils.getMD5(path.toFile());
                    ret = updateFile(localEtag, client, fileList.getItems().get(0), null, null,
                            DriveUtils.newMineType(mineType), path.toString(), progressCallback);
                }
            }
            break;
        } catch (Throwable e) {
            dealWithException(e, tryCounter);
            logger.info("Is about to retry...");
        }
    }
    return ret;
}

From source file:org.eclipse.hono.deviceregistry.FileBasedRegistrationService.java

Future<Void> saveToFile() {

    if (!getConfig().isSaveToFile()) {
        return Future.succeededFuture();
    } else if (dirty) {
        return checkFileExists(true).compose(s -> {
            final AtomicInteger idCount = new AtomicInteger();
            final JsonArray tenants = new JsonArray();
            for (final Entry<String, Map<String, JsonObject>> entry : identities.entrySet()) {
                final JsonArray devices = new JsonArray();
                for (final Entry<String, JsonObject> deviceEntry : entry.getValue().entrySet()) {
                    devices.add(new JsonObject().put(FIELD_PAYLOAD_DEVICE_ID, deviceEntry.getKey())
                            .put(FIELD_DATA, deviceEntry.getValue()));
                    idCount.incrementAndGet();
                }//  w w w.  ja  v a  2  s. co m
                tenants.add(new JsonObject().put(FIELD_TENANT, entry.getKey()).put(ARRAY_DEVICES, devices));
            }

            final Future<Void> writeHandler = Future.future();
            vertx.fileSystem().writeFile(getConfig().getFilename(),
                    Buffer.factory.buffer(tenants.encodePrettily()), writeHandler.completer());
            return writeHandler.map(ok -> {
                dirty = false;
                log.trace("successfully wrote {} device identities to file {}", idCount.get(),
                        getConfig().getFilename());
                return (Void) null;
            }).otherwise(t -> {
                log.warn("could not write device identities to file {}", getConfig().getFilename(), t);
                return (Void) null;
            });
        });
    } else {
        log.trace("registry does not need to be persisted");
        return Future.succeededFuture();
    }
}

From source file:com.hybris.mobile.lib.commerce.sync.CatalogSyncAdapter.java

@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider,
        SyncResult syncResult) {/*from  www  .ja v a  2 s . c o  m*/

    Log.i(TAG, "Receiving a sync with bundle: " + extras.toString());

    // Get some optional parameters
    String categoryId = extras.getString(CatalogSyncConstants.SYNC_PARAM_GROUP_ID);
    String productId = extras.getString(CatalogSyncConstants.SYNC_PARAM_DATA_ID);
    boolean loadVariants = extras.getBoolean(CatalogSyncConstants.SYNC_PARAM_LOAD_VARIANTS);
    String contentServiceHelperUrl = extras
            .getString(CatalogSyncConstants.SYNC_PARAM_CONTENT_SERVICE_HELPER_URL);
    boolean cancelAllRequests = extras.getBoolean(CatalogSyncConstants.SYNC_PARAM_CANCEL_ALL_REQUESTS);

    // Update the content service helper url
    if (StringUtils.isNotBlank(contentServiceHelperUrl)) {
        String catalog = extras.getString(CatalogSyncConstants.SYNC_PARAM_CONTENT_SERVICE_HELPER_CATALOG);
        String catalogId = extras.getString(CatalogSyncConstants.SYNC_PARAM_CONTENT_SERVICE_HELPER_CATALOG_ID);
        String catalogVersionId = extras
                .getString(CatalogSyncConstants.SYNC_PARAM_CONTENT_SERVICE_HELPER_CATALOG_VERSION_ID);
        String catalogMainCategoryId = extras
                .getString(CatalogSyncConstants.SYNC_PARAM_CONTENT_SERVICE_HELPER_MAIN_CATEGORY_ID);
        updateContentServiceHelperUrlConfiguration(contentServiceHelperUrl, catalog, catalogId,
                catalogVersionId, catalogMainCategoryId);
    }
    // Cancelling all the requests
    else if (cancelAllRequests) {
        cancelAllRequests();
    }
    // Sync a category
    else if (StringUtils.isNotBlank(categoryId)) {
        Log.i(TAG, "Syncing the category " + categoryId);

        int currentPage = 0;
        int pageSize = 0;

        if (extras.containsKey(CatalogSyncConstants.SYNC_PARAM_CURRENT_PAGE)
                && extras.containsKey(CatalogSyncConstants.SYNC_PARAM_PAGE_SIZE)) {
            currentPage = extras.getInt(CatalogSyncConstants.SYNC_PARAM_CURRENT_PAGE);
            pageSize = extras.getInt(CatalogSyncConstants.SYNC_PARAM_PAGE_SIZE);
        }

        syncCategory(categoryId, currentPage, pageSize);

    }
    // Sync a product
    else if (StringUtils.isNotBlank(productId)) {
        Log.i(TAG, "Syncing the product " + productId);

        loadProduct(productId, categoryId, null, false, loadVariants);
    }
    // Sync all the catalog
    else {
        Log.i(TAG, "Syncing the catalog");

        // Init nb calls counter and blocker
        mNbCalls = new AtomicInteger();
        mBlockSync = new CountDownLatch(1);

        String categories = extras.getString(CatalogSyncConstants.SYNC_PARAM_GROUP_ID_LIST);

        try {
            String[] categoryList = null;

            if (StringUtils.isNotBlank(categories)) {
                categoryList = categories.split(CatalogSyncConstants.SYNC_PARAM_GROUP_ID_LIST_SEPARATOR);
            }

            syncCatalog(categoryList);

            // Save the date
            mContentServiceHelper.saveCatalogLastSyncDate(new Date().getTime());

            // Showing the notification
            showNotificationProgress(true);

            // Wait for the end of the sync
            mBlockSync.await(getContext().getResources().getInteger(R.integer.sync_timeout_in_min),
                    TimeUnit.MINUTES);

        } catch (InterruptedException e) {
            Log.e(TAG, "Error syncing the catalog");
        }
    }
}

From source file:org.eclipse.hono.deviceregistry.FileBasedCredentialsService.java

Future<Void> saveToFile() {

    if (!getConfig().isSaveToFile()) {
        return Future.succeededFuture();
    } else if (dirty) {
        return checkFileExists(true).compose(s -> {
            final AtomicInteger idCount = new AtomicInteger();
            final JsonArray tenants = new JsonArray();
            for (final Entry<String, Map<String, JsonArray>> entry : credentials.entrySet()) {
                final JsonArray credentialsArray = new JsonArray();
                for (final JsonArray singleAuthIdCredentials : entry.getValue().values()) {
                    credentialsArray.addAll(singleAuthIdCredentials.copy());
                    idCount.incrementAndGet();
                }/*from   w ww. ja  v  a 2 s  . c o  m*/
                tenants.add(new JsonObject().put(FIELD_TENANT, entry.getKey()).put(ARRAY_CREDENTIALS,
                        credentialsArray));
            }
            final Future<Void> writeHandler = Future.future();
            vertx.fileSystem().writeFile(getConfig().getFilename(),
                    Buffer.buffer(tenants.encodePrettily(), StandardCharsets.UTF_8.name()),
                    writeHandler.completer());
            return writeHandler.map(ok -> {
                dirty = false;
                log.trace("successfully wrote {} credentials to file {}", idCount.get(),
                        getConfig().getFilename());
                return (Void) null;
            }).otherwise(t -> {
                log.warn("could not write credentials to file {}", getConfig().getFilename(), t);
                return (Void) null;
            });
        });
    } else {
        log.trace("credentials registry does not need to be persisted");
        return Future.succeededFuture();
    }
}