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(int initialValue) 

Source Link

Document

Creates a new AtomicInteger with the given initial value.

Usage

From source file:com.streamsets.pipeline.stage.origin.spooldir.TestWholeFileSpoolDirSource.java

@Test
public void testWholeFileRecordsForFile() throws Exception {
    Path sourcePath = Paths.get(testDir + "/source.txt");
    Files.write(sourcePath, "Sample Text 1".getBytes());
    Files.setAttribute(sourcePath, "posix:permissions",
            ImmutableSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE,
                    PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ));

    SpoolDirSource source = createSource();
    PushSourceRunner runner = new PushSourceRunner.Builder(SpoolDirDSource.class, source).addOutputLane("lane")
            .setOnRecordError(OnRecordError.TO_ERROR).build();

    final List<Record> records = Collections.synchronizedList(new ArrayList<>(10));
    AtomicInteger batchCount = new AtomicInteger(0);

    runner.runInit();//from www . j  a  v a 2 s .  c  o  m
    try {
        runner.runProduce(new HashMap<>(), 10, output2 -> {
            synchronized (records) {
                records.addAll(output2.getRecords().get("lane"));
            }
            batchCount.incrementAndGet();
            runner.setStop();
        });

        runner.waitOnProduce();

        Assert.assertNotNull(records);
        Assert.assertEquals(1, records.size());
        Record record = records.get(0);

        Assert.assertTrue(record.has(FileRefUtil.FILE_INFO_FIELD_PATH));
        Assert.assertTrue(record.has(FileRefUtil.FILE_REF_FIELD_PATH));

        Assert.assertEquals(Field.Type.FILE_REF, record.get(FileRefUtil.FILE_REF_FIELD_PATH).getType());
        Assert.assertEquals(Field.Type.MAP, record.get(FileRefUtil.FILE_INFO_FIELD_PATH).getType());

        Map<String, Object> metadata = Files.readAttributes(sourcePath, "posix:*");
        Assert.assertTrue(record.get(FileRefUtil.FILE_INFO_FIELD_PATH).getValueAsMap().keySet()
                .containsAll(metadata.keySet()));

        //Check permissions
        Assert.assertTrue(record.has(FileRefUtil.FILE_INFO_FIELD_PATH + "/" + SpoolDirRunnable.PERMISSIONS));
        Assert.assertEquals("rwxr-----", record
                .get(FileRefUtil.FILE_INFO_FIELD_PATH + "/" + SpoolDirRunnable.PERMISSIONS).getValueAsString());

        Assert.assertEquals(Field.Type.FILE_REF, record.get(FileRefUtil.FILE_REF_FIELD_PATH).getType());
        Assert.assertEquals(Field.Type.MAP, record.get(FileRefUtil.FILE_INFO_FIELD_PATH).getType());

    } finally {
        runner.runDestroy();
    }
}

From source file:com.microsoft.azure.servicebus.samples.jmstopicquickstart.JmsTopicQuickstart.java

private void receiveFromSubscription(ConnectionStringBuilder csb, Context context, ConnectionFactory cf,
        String name) throws NamingException, JMSException, InterruptedException {
    AtomicInteger totalReceived = new AtomicInteger(0);
    System.out.printf("Subscription %s: \n", name);

    Destination subscription = (Destination) context.lookup(name);
    // Create Connection
    Connection connection = cf.createConnection(csb.getSasKeyName(), csb.getSasKey());
    connection.start();//  w w  w  .  j  a  v  a 2s. c o  m
    // Create Session, no transaction, client ack
    Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    // Create consumer
    MessageConsumer consumer = session.createConsumer(subscription);
    // Set callback listener. Gets called for each received message.
    consumer.setMessageListener(message -> {
        try {
            System.out.printf("Received message %d with sq#: %s\n", totalReceived.incrementAndGet(), // increments the counter
                    message.getJMSMessageID());
            message.acknowledge();
        } catch (Exception e) {
            System.out.printf("%s", e.toString());
        }
    });

    // wait on the main thread until all sent messages have been received
    while (totalReceived.get() < totalSend) {
        Thread.sleep(1000);
    }
    consumer.close();
    session.close();
    connection.stop();
    connection.close();
}

From source file:ai.grakn.client.LoaderClient.java

public LoaderClient(String keyspace, String uri, Consumer<Json> onCompletionOfTask) {
    this.uri = uri;
    this.keyspace = keyspace;
    this.queries = new HashSet<>();
    this.futures = new ConcurrentHashMap<>();
    this.onCompletionOfTask = onCompletionOfTask;
    this.batchNumber = new AtomicInteger(0);

    setBatchSize(25);/*from  w ww . j  a  v a2 s  .  c  o  m*/
    setNumberActiveTasks(25);
}

From source file:com.adobe.acs.commons.workflow.process.impl.SyntheticWrapperWorkflowProcess.java

@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
        throws WorkflowException {
    ResourceResolver resourceResolver = null;
    final SyntheticWorkflowRunner syntheticWorkflowRunner = syntheticWorkflowRunnerAccessor
            .getSyntheticWorkflowRunner();

    final String payload = (String) workItem.getWorkflowData().getPayload();
    final ProcessArgs processArgs = new ProcessArgs(metaDataMap);

    try {/*from   w  w  w.  ja v a  2 s  .  co m*/
        resourceResolver = workflowHelper.getResourceResolver(workflowSession);
        final SyntheticWorkflowModel syntheticWorkflowModel = syntheticWorkflowRunner
                .getSyntheticWorkflowModel(resourceResolver, processArgs.getWorkflowModelId(), true);

        final AtomicInteger count = new AtomicInteger(0);

        // Anonymous inner class to facilitate counting of processed payloads
        final ResourceRunnable syntheticRunnable = new ResourceRunnable() {
            @Override
            public void run(final Resource resource) throws java.lang.Exception {
                if (processArgs.isThrottle()) {
                    throttledTaskRunner.waitForLowCpuAndLowMemory();
                }

                syntheticWorkflowRunner.execute(resource.getResourceResolver(), resource.getPath(),
                        syntheticWorkflowModel, false, false);

                // Commit as needed
                if (processArgs.getSaveInterval() > 0
                        && count.incrementAndGet() % processArgs.getSaveInterval() == 0
                        && resource.getResourceResolver().hasChanges()) {
                    resource.getResourceResolver().commit();
                }
            }
        };

        final ContentVisitor visitor = new ContentVisitor(syntheticRunnable);
        final Resource resource = resourceResolver.getResource(payload);

        if (processArgs.isTraverseTree()) {
            visitor.accept(resource);
        } else {
            syntheticRunnable.run(resource);
        }

        if (processArgs.getSaveInterval() > 0 && resourceResolver.hasChanges()) {
            // Commit any stranglers
            resourceResolver.commit();
        }

        log.info("Synthetic Workflow Wrapper processed [ {} ] total payloads", count.get());
    } catch (Exception e) {
        throw new WorkflowException(e);
    }
}

From source file:com.github.naoghuman.testdata.abclist.service.ExerciseTermService.java

@Override
protected Task<Void> createTask() {
    return new Task<Void>() {
        {//from  w w w  .  j av a2 s .c  om
            updateProgress(0, saveMaxEntities);
        }

        @Override
        protected Void call() throws Exception {
            LoggerFacade.getDefault().deactivate(Boolean.TRUE);

            final StopWatch stopWatch = new StopWatch();
            stopWatch.start();

            final ObservableList<Topic> topics = SqlProvider.getDefault().findAllTopics();
            final ObservableList<Term> terms = SqlProvider.getDefault().findAllTerms();
            final int sizeTerms = terms.size();
            final AtomicInteger index = new AtomicInteger(0);

            final CrudService crudService = DatabaseFacade.getDefault().getCrudService(entityName);
            final AtomicLong id = new AtomicLong(
                    -1_000_000_000L + DatabaseFacade.getDefault().getCrudService().count(entityName));
            topics.stream().forEach(topic -> {
                final ObservableList<Exercise> exercises = SqlProvider.getDefault()
                        .findAllExercisesWithTopicId(topic.getId());
                exercises.stream().filter(exercise -> exercise.isReady()).forEach(exercise -> {
                    final int maxExerciseTerms = TestdataGenerator.RANDOM.nextInt(70) + 10;
                    for (int i = 0; i < maxExerciseTerms; i++) {
                        final Term term = terms.get(TestdataGenerator.RANDOM.nextInt(sizeTerms));
                        final ExerciseTerm exerciseTerm = ModelProvider.getDefault().getExerciseTerm();
                        exerciseTerm.setExerciseId(exercise.getId());
                        exerciseTerm.setId(id.getAndIncrement());
                        exerciseTerm.setTermId(term.getId());

                        crudService.create(exerciseTerm);
                    }
                });

                updateProgress(index.getAndIncrement(), saveMaxEntities);
            });

            LoggerFacade.getDefault().deactivate(Boolean.FALSE);
            stopWatch.split();
            LoggerFacade.getDefault().debug(this.getClass(),
                    "  + " + stopWatch.toSplitString() + " for " + saveMaxEntities + " ExerciseTerms."); // NOI18N
            stopWatch.stop();

            return null;
        }
    };
}

From source file:io.realm.TypeBasedNotificationsTests.java

@Before
public void setUp() {
    context = InstrumentationRegistry.getTargetContext();
    globalCommitInvocations = new AtomicInteger(0);
    typebasedCommitInvocations = new AtomicInteger(0);
}

From source file:com.appleframework.monitor.service.AlertService.java

protected boolean isNeedNotify(Alert alert) {
    if (alert == null)
        return false;
    String key = alert.getProjectName() + "_" + alert.getTitle();

    AtomicInteger init = new AtomicInteger(0);
    AtomicInteger times = notifyTimes.putIfAbsent(key, init);
    if (times == null)
        times = init;/*w w w.  j  a  v  a 2 s .  com*/
    logger.debug("{} notify times ={}", key, times.get());
    return times.getAndIncrement() < limitTimes;
}

From source file:com.indeed.lsmtree.recordlog.TestRecordLogDirectory.java

public void testRandom() throws Exception {
    final RecordLogDirectory<String> fileCache = createRecordLogDirectory();
    final AtomicInteger done = new AtomicInteger(8);
    for (int i = 0; i < 8; i++) {
        final int index = i;
        new Thread(new Runnable() {
            @Override/* ww w . j  ava  2s  .  com*/
            public void run() {
                try {
                    final Random r = new Random(index);
                    for (int i = 0; i < 10000; i++) {
                        int rand = r.nextInt(positions.size());
                        assertTrue(fileCache.get(positions.get(rand)).equals(strings.get(rand)));
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } finally {
                    done.decrementAndGet();
                }
            }
        }).start();
    }
    while (done.get() > 0) {
        Thread.yield();
    }
    fileCache.close();
}

From source file:com.zimbra.cs.servlet.ContextPathBasedThreadPoolBalancerFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    // Determine whether to allow or suspend request
    boolean suspend = shouldSuspend(request);

    // Suspend request
    if (suspend) {
        Continuation continuation = ContinuationSupport.getContinuation(request);
        HttpServletRequest hreq = (HttpServletRequest) request;
        ZimbraServlet.addRemoteIpToLoggingContext(hreq);
        ZimbraServlet.addUAToLoggingContext(hreq);
        ZimbraLog.clearContext();//from  w  w w . j  ava  2s . c  om
        continuation.setTimeout(suspendMs);
        continuation.suspend();
        return;
    }

    // Allow request
    String contextPath = getContextPath(request);
    try {
        // Start tracking request
        AtomicInteger i = activeRequestsByContextPath.get(contextPath);
        if (i == null) {
            i = new AtomicInteger(1);
            activeRequestsByContextPath.put(contextPath, i);
        } else {
            i.incrementAndGet();
        }
        //            ZimbraLog.misc.debug("%s concurrency=%d", contextPath, i.get());

        // Perform default operation
        chain.doFilter(request, response);

    } finally {
        // Stop tracking request
        AtomicInteger i = activeRequestsByContextPath.get(contextPath);
        i.decrementAndGet();
        //            ZimbraLog.misc.debug("%s concurrency=%d", contextPath, i.get());
    }
}

From source file:com.rapplogic.aru.uploader.wifi.WifiSketchUploader.java

@Override
protected void open(final Map<String, Object> context) throws Exception {

    String host = (String) context.get("host");
    Integer port = (Integer) context.get("port");
    Integer connectionTimeoutSecs = (Integer) context.get("connectionTimeoutSecs");
    Integer readTimeoutSecs = (Integer) context.get("readTimeoutSecs");

    // open socket
    socket = new Socket();
    socket.connect(new InetSocketAddress(host, port), connectionTimeoutSecs * 1000);
    socket.setSoTimeout(readTimeoutSecs * 1000);

    connected = true;/*from ww w .j  a v  a 2s  .  c o m*/

    final int[] reply = new int[5];
    final AtomicInteger replyIndex = new AtomicInteger(0);

    // TODO unlike other wireless protocols, wifi is stateful so we need to handle situations where we lose the socket and reconnect

    t = new Thread(new Runnable() {
        @Override
        public void run() {
            int ch = 0;

            // reply always terminated with 13,10
            try {
                while ((ch = socket.getInputStream().read()) > -1) {
                    if (replyIndex.get() < reply.length) {
                        reply[replyIndex.getAndIncrement()] = ch;
                    } else if (replyIndex.get() == 5 && ch == 13) {
                        replyIndex.getAndIncrement();
                        // discard
                    } else if (replyIndex.get() == 6 && ch == 10) {
                        //System.out.println("reply is " + stringBuilder.toString());
                        //                  stringBuilder = new StringBuilder();
                        handleReply(reply);
                        replyIndex.set(0);
                    } else {
                        //error
                        throw new RuntimeException("Expected CR/LF -- invalid reply at position "
                                + replyIndex.get() + ", array: " + intArrayToString(reply));
                    }
                }
            } catch (IOException e) {
                if (!connected && e instanceof SocketException) {
                    // expected.. ignore
                } else {
                    System.out.println("IO error in socket reader");
                    e.printStackTrace();
                }
            } catch (Exception e) {
                System.out.println("Unexpected error in socket reader");
                e.printStackTrace();
            }

            connected = false;
        }
    });

    t.setDaemon(true);
    t.start();
}