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

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

Introduction

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

Prototype

public final void set(int newValue) 

Source Link

Document

Sets the value to newValue , with memory effects as specified by VarHandle#setVolatile .

Usage

From source file:org.apache.nifi.processors.hive.ConvertAvroToORC.java

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();/*from w w  w .  ja  v  a2s.c  o  m*/
    if (flowFile == null) {
        return;
    }

    try {
        long startTime = System.currentTimeMillis();
        final long stripeSize = context.getProperty(STRIPE_SIZE).asDataSize(DataUnit.B).longValue();
        final int bufferSize = context.getProperty(BUFFER_SIZE).asDataSize(DataUnit.B).intValue();
        final CompressionKind compressionType = CompressionKind
                .valueOf(context.getProperty(COMPRESSION_TYPE).getValue());
        final AtomicReference<Schema> hiveAvroSchema = new AtomicReference<>(null);
        final AtomicInteger totalRecordCount = new AtomicInteger(0);
        final String fileName = flowFile.getAttribute(CoreAttributes.FILENAME.key());
        flowFile = session.write(flowFile, new StreamCallback() {
            @Override
            public void process(final InputStream rawIn, final OutputStream rawOut) throws IOException {
                try (final InputStream in = new BufferedInputStream(rawIn);
                        final OutputStream out = new BufferedOutputStream(rawOut);
                        final DataFileStream<GenericRecord> reader = new DataFileStream<>(in,
                                new GenericDatumReader<>())) {

                    // Create ORC schema from Avro schema
                    Schema avroSchema = reader.getSchema();
                    TypeDescription orcSchema = OrcUtils.getOrcField(avroSchema);

                    if (orcConfig == null) {
                        orcConfig = new Configuration();
                    }
                    OrcFile.WriterOptions options = OrcFile.writerOptions(orcConfig).setSchema(orcSchema)
                            .stripeSize(stripeSize).bufferSize(bufferSize).compress(compressionType)
                            .version(OrcFile.Version.CURRENT);

                    OrcFlowFileWriter orcWriter = new OrcFlowFileWriter(out, new Path(fileName), options);
                    try {
                        VectorizedRowBatch batch = orcSchema.createRowBatch();
                        int recordCount = 0;
                        int recordsInBatch = 0;
                        GenericRecord currRecord = null;
                        while (reader.hasNext()) {
                            currRecord = reader.next(currRecord);
                            List<Schema.Field> fields = currRecord.getSchema().getFields();
                            if (fields != null) {
                                MutableInt[] vectorOffsets = new MutableInt[fields.size()];
                                for (int i = 0; i < fields.size(); i++) {
                                    vectorOffsets[i] = new MutableInt(0);
                                    Schema.Field field = fields.get(i);
                                    Schema fieldSchema = field.schema();
                                    Object o = currRecord.get(field.name());
                                    try {
                                        OrcUtils.putToRowBatch(batch.cols[i], vectorOffsets[i], recordsInBatch,
                                                fieldSchema, o);
                                    } catch (ArrayIndexOutOfBoundsException aioobe) {
                                        getLogger().error(
                                                "Index out of bounds at record {} for column {}, type {}, and object {}",
                                                new Object[] { recordsInBatch, i,
                                                        fieldSchema.getType().getName(), o.toString() },
                                                aioobe);
                                        throw new IOException(aioobe);
                                    }
                                }
                            }
                            recordCount++;
                            recordsInBatch++;

                            if (recordsInBatch == batch.getMaxSize()) {
                                // add batch and start a new one
                                batch.size = recordsInBatch;
                                orcWriter.addRowBatch(batch);
                                batch = orcSchema.createRowBatch();
                                recordsInBatch = 0;
                            }
                        }

                        // If there are records in the batch, add the batch
                        if (recordsInBatch > 0) {
                            batch.size = recordsInBatch;
                            orcWriter.addRowBatch(batch);
                        }

                        hiveAvroSchema.set(avroSchema);
                        totalRecordCount.set(recordCount);
                    } finally {
                        // finished writing this record, close the writer (which will flush to the flow file)
                        orcWriter.close();
                    }
                }
            }
        });

        final String hiveTableName = context.getProperty(HIVE_TABLE_NAME).isSet()
                ? context.getProperty(HIVE_TABLE_NAME).evaluateAttributeExpressions(flowFile).getValue()
                : OrcUtils.normalizeHiveTableName(hiveAvroSchema.get().getFullName());
        String hiveDDL = OrcUtils.generateHiveDDL(hiveAvroSchema.get(), hiveTableName);
        // Add attributes and transfer to success
        flowFile = session.putAttribute(flowFile, RECORD_COUNT_ATTRIBUTE,
                Integer.toString(totalRecordCount.get()));
        flowFile = session.putAttribute(flowFile, HIVE_DDL_ATTRIBUTE, hiveDDL);
        StringBuilder newFilename = new StringBuilder();
        int extensionIndex = fileName.lastIndexOf(".");
        if (extensionIndex != -1) {
            newFilename.append(fileName.substring(0, extensionIndex));
        } else {
            newFilename.append(fileName);
        }
        newFilename.append(".orc");
        flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), ORC_MIME_TYPE);
        flowFile = session.putAttribute(flowFile, CoreAttributes.FILENAME.key(), newFilename.toString());
        session.transfer(flowFile, REL_SUCCESS);
        session.getProvenanceReporter().modifyContent(flowFile,
                "Converted " + totalRecordCount.get() + " records", System.currentTimeMillis() - startTime);
    } catch (final ProcessException pe) {
        getLogger().error("Failed to convert {} from Avro to ORC due to {}; transferring to failure",
                new Object[] { flowFile, pe });
        session.transfer(flowFile, REL_FAILURE);
    }
}

From source file:org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.java

private void initializeCursors(final ManagedLedgerInitializeLedgerCallback callback) {
    if (log.isDebugEnabled()) {
        log.debug("[{}] initializing cursors", name);
    }//w w w  .ja  v  a  2s  .  co  m
    store.getCursors(name, new MetaStoreCallback<List<String>>() {
        @Override
        public void operationComplete(List<String> consumers, Stat s) {
            // Load existing cursors
            final AtomicInteger cursorCount = new AtomicInteger(consumers.size());
            if (log.isDebugEnabled()) {
                log.debug("[{}] Found {} cursors", name, consumers.size());
            }

            if (consumers.isEmpty()) {
                callback.initializeComplete();
                return;
            }

            for (final String cursorName : consumers) {
                if (log.isDebugEnabled()) {
                    log.debug("[{}] Loading cursor {}", name, cursorName);
                }
                final ManagedCursorImpl cursor;
                cursor = new ManagedCursorImpl(bookKeeper, config, ManagedLedgerImpl.this, cursorName);

                cursor.recover(new VoidCallback() {
                    @Override
                    public void operationComplete() {
                        log.info("[{}] Recovery for cursor {} completed. pos={} -- todo={}", name, cursorName,
                                cursor.getMarkDeletedPosition(), cursorCount.get() - 1);
                        cursor.setActive();
                        cursors.add(cursor);

                        if (cursorCount.decrementAndGet() == 0) {
                            // The initialization is now completed, register the jmx mbean
                            callback.initializeComplete();
                        }
                    }

                    @Override
                    public void operationFailed(ManagedLedgerException exception) {
                        log.warn("[{}] Recovery for cursor {} failed", name, cursorName, exception);
                        cursorCount.set(-1);
                        callback.initializeFailed(exception);
                    }
                });
            }
        }

        @Override
        public void operationFailed(MetaStoreException e) {
            log.warn("[{}] Failed to get the cursors list", name, e);
            callback.initializeFailed(new ManagedLedgerException(e));
        }
    });
}

From source file:com.klinker.android.launcher.launcher3.Workspace.java

/**
 * Returns a new bitmap to show when the given View is being dragged around.
 * Responsibility for the bitmap is transferred to the caller.
 * @param expectedPadding padding to add to the drag view. If a different padding was used
 * its value will be changed//  www  .j av a  2  s.  co m
 */
public Bitmap createDragBitmap(View v, AtomicInteger expectedPadding) {
    Bitmap b;

    int padding = expectedPadding.get();
    if (v instanceof TextView) {
        Drawable d = getTextViewIcon((TextView) v);
        Rect bounds = getDrawableBounds(d);
        b = Bitmap.createBitmap(bounds.width() + padding, bounds.height() + padding, Bitmap.Config.ARGB_8888);
        expectedPadding.set(padding - bounds.left - bounds.top);
    } else {
        b = Bitmap.createBitmap(v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
    }

    mCanvas.setBitmap(b);
    drawDragView(v, mCanvas, padding);
    mCanvas.setBitmap(null);

    return b;
}

From source file:org.openspaces.grid.gsm.rebalancing.RebalancingUtils.java

static Collection<FutureStatelessProcessingUnitInstance> incrementNumberOfStatelessInstancesAsync(
        final ProcessingUnit pu, final GridServiceContainer[] containers, final Log logger, final long duration,
        final TimeUnit timeUnit) {

    if (pu.getMaxInstancesPerVM() != 1) {
        throw new IllegalArgumentException("Only one instance per VM is allowed");
    }/* w w  w. j av a2  s  . c o  m*/

    List<GridServiceContainer> unusedContainers = getUnusedContainers(pu, containers);

    final Admin admin = pu.getAdmin();
    final Map<GridServiceContainer, FutureStatelessProcessingUnitInstance> futureInstances = new HashMap<GridServiceContainer, FutureStatelessProcessingUnitInstance>();

    final AtomicInteger targetNumberOfInstances = new AtomicInteger(pu.getNumberOfInstances());

    final long start = System.currentTimeMillis();
    final long end = start + timeUnit.toMillis(duration);

    for (GridServiceContainer container : unusedContainers) {
        final GridServiceContainer targetContainer = container;
        futureInstances.put(container, new FutureStatelessProcessingUnitInstance() {

            AtomicReference<Throwable> throwable = new AtomicReference<Throwable>();
            ProcessingUnitInstance newInstance;

            public boolean isTimedOut() {
                return System.currentTimeMillis() > end;
            }

            public boolean isDone() {

                end();

                return isTimedOut() || throwable.get() != null || newInstance != null;
            }

            public ProcessingUnitInstance get()
                    throws ExecutionException, IllegalStateException, TimeoutException {

                end();

                if (getException() != null) {
                    throw getException();
                }

                if (newInstance == null) {
                    if (isTimedOut()) {
                        throw new TimeoutException("Relocation timeout");
                    }
                    throw new IllegalStateException("Async operation is not done yet.");
                }

                return newInstance;
            }

            public Date getTimestamp() {
                return new Date(start);
            }

            public ExecutionException getException() {

                end();
                Throwable t = throwable.get();
                if (t != null) {
                    return new ExecutionException(t.getMessage(), t);
                }
                return null;
            }

            public GridServiceContainer getTargetContainer() {
                return targetContainer;
            }

            public ProcessingUnit getProcessingUnit() {
                return pu;
            }

            public String getFailureMessage() throws IllegalStateException {
                if (isTimedOut()) {
                    return "deployment timeout of processing unit " + pu.getName() + " on "
                            + gscToString(targetContainer);
                }

                if (getException() != null) {
                    return getException().getMessage();
                }

                throw new IllegalStateException("Relocation has not encountered any failure.");
            }

            private void end() {

                if (!targetContainer.isDiscovered()) {
                    throwable.set(new RemovedContainerProcessingUnitDeploymentException(pu, targetContainer));
                }

                else if (throwable.get() != null || newInstance != null) {
                    //do nothing. idempotent method
                }

                else {
                    incrementInstance();

                    ProcessingUnitInstance[] instances = targetContainer
                            .getProcessingUnitInstances(pu.getName());

                    if (instances.length > 0) {
                        newInstance = instances[0];
                    }
                }
            }

            private void incrementInstance() {
                final String uuid = "[incrementUid:" + UUID.randomUUID().toString() + "] ";
                int numberOfInstances = pu.getNumberOfInstances();
                int maxNumberOfInstances = getContainersOnMachines(pu).length;
                if (numberOfInstances < maxNumberOfInstances) {
                    if (targetNumberOfInstances.get() == numberOfInstances + 1) {
                        if (logger.isInfoEnabled()) {
                            logger.info("Waiting for pu.numberOfInstances to increment from "
                                    + numberOfInstances + " to " + targetNumberOfInstances.get() + ". "
                                    + "Number of relevant containers " + maxNumberOfInstances);
                        }
                    } else if (admin.getGridServiceManagers().getSize() > 1
                            && !((InternalProcessingUnit) pu).isBackupGsmInSync()) {
                        if (logger.isInfoEnabled()) {
                            logger.info("Waiting for backup gsm to sync with active gsm");
                        }
                    } else {
                        targetNumberOfInstances.set(numberOfInstances + 1);
                        if (logger.isInfoEnabled()) {
                            logger.info(uuid + " Planning to increment pu.numberOfInstances from "
                                    + numberOfInstances + " to " + targetNumberOfInstances.get() + ". "
                                    + "Number of relevant containers " + maxNumberOfInstances);
                        }
                        ((InternalAdmin) admin).scheduleAdminOperation(new Runnable() {
                            public void run() {
                                try {
                                    // this is an async operation 
                                    // pu.getNumberOfInstances() still shows the old value.
                                    pu.incrementInstance();
                                    if (logger.isInfoEnabled()) {
                                        logger.info(uuid + " pu.incrementInstance() called");
                                    }
                                } catch (AdminException e) {
                                    throwable.set(e);
                                } catch (Throwable e) {
                                    logger.error(uuid + " Unexpected Exception: " + e.getMessage(), e);
                                    throwable.set(e);
                                }
                            }
                        });
                    }
                }
            }
        });

    }

    return futureInstances.values();

}

From source file:com.android.launcher3.Workspace.java

/**
 * Returns a new bitmap to show when the given View is being dragged around.
 * Responsibility for the bitmap is transferred to the caller.
 * @param expectedPadding padding to add to the drag view. If a different padding was used
 * its value will be changed//from  w  ww .j  a va  2 s .  co m
 */
public Bitmap createDragBitmap(View v, AtomicInteger expectedPadding) {
    Bitmap b;

    int padding = expectedPadding.get();
    if (v instanceof TextView) {
        Drawable d = ((TextView) v).getCompoundDrawables()[1];
        Rect bounds = getDrawableBounds(d);
        b = Bitmap.createBitmap(bounds.width() + padding, bounds.height() + padding, Bitmap.Config.ARGB_8888);
        expectedPadding.set(padding - bounds.left - bounds.top);
    } else {
        b = Bitmap.createBitmap(v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
    }

    mCanvas.setBitmap(b);
    drawDragView(v, mCanvas, padding);
    mCanvas.setBitmap(null);

    return b;
}

From source file:org.apache.nifi.processors.standard.ForkRecord.java

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {

    FlowFile flowFile = session.get();/* ww w. ja v a 2  s . co  m*/
    if (flowFile == null) {
        return;
    }

    final List<RecordPath> recordPaths = new ArrayList<>();
    Map<PropertyDescriptor, String> processorProperties = context.getProperties();
    for (final Map.Entry<PropertyDescriptor, String> entry : processorProperties.entrySet()) {
        PropertyDescriptor property = entry.getKey();
        if (property.isDynamic() && property.isExpressionLanguageSupported()) {
            String path = context.getProperty(property).evaluateAttributeExpressions(flowFile).getValue();
            if (StringUtils.isNotBlank(path)) {
                recordPaths.add(recordPathCache.getCompiled(
                        context.getProperty(property).evaluateAttributeExpressions(flowFile).getValue()));
            }
        }
    }

    final RecordReaderFactory readerFactory = context.getProperty(RECORD_READER)
            .asControllerService(RecordReaderFactory.class);
    final RecordSetWriterFactory writerFactory = context.getProperty(RECORD_WRITER)
            .asControllerService(RecordSetWriterFactory.class);
    final boolean addParentFields = context.getProperty(INCLUDE_PARENT_FIELDS).asBoolean();
    final boolean isSplitMode = context.getProperty(MODE).getValue().equals(MODE_SPLIT.getValue());

    final FlowFile original = flowFile;
    final Map<String, String> originalAttributes = original.getAttributes();

    final FlowFile outFlowFile = session.create(original);
    final AtomicInteger readCount = new AtomicInteger(0);
    final AtomicInteger writeCount = new AtomicInteger(0);

    try {

        session.read(flowFile, new InputStreamCallback() {
            @Override
            public void process(final InputStream in) throws IOException {
                try (final RecordReader reader = readerFactory.createRecordReader(originalAttributes, in,
                        getLogger())) {

                    final RecordSchema writeSchema = writerFactory.getSchema(originalAttributes,
                            reader.getSchema());
                    final OutputStream out = session.write(outFlowFile);

                    try (final RecordSetWriter recordSetWriter = writerFactory.createWriter(getLogger(),
                            writeSchema, out)) {

                        recordSetWriter.beginRecordSet();

                        // we read each record of the input flow file
                        Record record;
                        while ((record = reader.nextRecord()) != null) {

                            readCount.incrementAndGet();

                            for (RecordPath recordPath : recordPaths) {

                                // evaluate record path in each record of the flow file
                                Iterator<FieldValue> it = recordPath.evaluate(record).getSelectedFields()
                                        .iterator();

                                while (it.hasNext()) {
                                    FieldValue fieldValue = it.next();
                                    RecordFieldType fieldType = fieldValue.getField().getDataType()
                                            .getFieldType();

                                    // we want to have an array here, nothing else allowed
                                    if (fieldType != RecordFieldType.ARRAY) {
                                        getLogger().debug("The record path " + recordPath.getPath()
                                                + " is matching a field " + "of type " + fieldType
                                                + " when the type ARRAY is expected.");
                                        continue;
                                    }

                                    if (isSplitMode) {

                                        Object[] items = (Object[]) fieldValue.getValue();
                                        for (Object item : items) {
                                            fieldValue.updateValue(new Object[] { item });
                                            recordSetWriter.write(record);
                                        }

                                    } else {

                                        // we get the type of the elements of the array
                                        final ArrayDataType arrayDataType = (ArrayDataType) fieldValue
                                                .getField().getDataType();
                                        final DataType elementType = arrayDataType.getElementType();

                                        // we want to have records in the array
                                        if (elementType.getFieldType() != RecordFieldType.RECORD) {
                                            getLogger().debug("The record path " + recordPath.getPath()
                                                    + " is matching an array field with " + "values of type "
                                                    + elementType.getFieldType()
                                                    + " when the type RECORD is expected.");
                                            continue;
                                        }

                                        Object[] records = (Object[]) fieldValue.getValue();
                                        for (Object elementRecord : records) {

                                            if (elementRecord == null) {
                                                continue;
                                            }

                                            Record recordToWrite = (Record) elementRecord;

                                            if (addParentFields) {
                                                // in this case we want to recursively add the parent fields into the record to write
                                                // but we need to ensure that the Record has the appropriate schema for that
                                                recordToWrite.incorporateSchema(writeSchema);
                                                recursivelyAddParentFields(recordToWrite, fieldValue);
                                            }

                                            recordSetWriter.write(recordToWrite);
                                        }

                                    }

                                }

                            }
                        }

                        final WriteResult writeResult = recordSetWriter.finishRecordSet();

                        try {
                            recordSetWriter.close();
                        } catch (final IOException ioe) {
                            getLogger().warn("Failed to close Writer for {}", new Object[] { outFlowFile });
                        }

                        final Map<String, String> attributes = new HashMap<>();
                        writeCount.set(writeResult.getRecordCount());
                        attributes.put("record.count", String.valueOf(writeResult.getRecordCount()));
                        attributes.put(CoreAttributes.MIME_TYPE.key(), recordSetWriter.getMimeType());
                        attributes.putAll(writeResult.getAttributes());
                        session.transfer(session.putAllAttributes(outFlowFile, attributes), REL_FORK);
                    }

                } catch (final SchemaNotFoundException | MalformedRecordException e) {
                    throw new ProcessException("Could not parse incoming data: " + e.getLocalizedMessage(), e);
                }
            }

            private void recursivelyAddParentFields(Record recordToWrite, FieldValue fieldValue) {
                try {
                    // we get the parent data
                    FieldValue parentField = fieldValue.getParent().get();
                    Record parentRecord = fieldValue.getParentRecord().get();

                    // for each field of the parent
                    for (String field : parentRecord.getSchema().getFieldNames()) {
                        // if and only if there is not an already existing field with this name
                        // (we want to give priority to the deeper existing fields)
                        if (recordToWrite.getValue(field) == null) {
                            // Updates the value of the field with the given name to the given value.
                            // If the field specified is not present in the schema, will do nothing.
                            recordToWrite.setValue(field, parentRecord.getValue(field));
                        }
                    }

                    // recursive call
                    recursivelyAddParentFields(recordToWrite, parentField);
                } catch (NoSuchElementException e) {
                    return;
                }
            }
        });

    } catch (Exception e) {
        getLogger().error("Failed to fork {}", new Object[] { flowFile, e });
        session.remove(outFlowFile);
        session.transfer(original, REL_FAILURE);
        return;
    }

    session.adjustCounter("Records Processed", readCount.get(), false);
    session.adjustCounter("Records Generated", writeCount.get(), false);
    getLogger().debug("Successfully forked {} records into {} records in {}",
            new Object[] { readCount.get(), writeCount.get(), flowFile });
    session.transfer(original, REL_ORIGINAL);
}

From source file:org.apache.usergrid.services.notifications.QueueListener.java

private void execute(int threadNumber) {

    if (Thread.currentThread().isDaemon()) {
        Thread.currentThread().setDaemon(true);
    }// w  w w.  j  a  va 2 s. c o m

    Thread.currentThread().setName(getClass().getSimpleName() + "_Push-"
            + RandomStringUtils.randomAlphanumeric(4) + "-" + threadNumber);

    final AtomicInteger consecutiveExceptions = new AtomicInteger();

    if (logger.isTraceEnabled()) {
        logger.trace("QueueListener: Starting execute process.");
    }

    Meter meter = metricsService.getMeter(QueueListener.class, "execute.commit");
    com.codahale.metrics.Timer timer = metricsService.getTimer(QueueListener.class, "execute.dequeue");

    if (logger.isTraceEnabled()) {
        logger.trace("getting from queue {} ", queueName);
    }

    LegacyQueueScope queueScope = new LegacyQueueScopeImpl(queueName,
            LegacyQueueScope.RegionImplementation.LOCAL);
    LegacyQueueManager legacyQueueManager = queueManagerFactory.getQueueManager(queueScope);

    // run until there are no more active jobs
    final AtomicLong runCount = new AtomicLong(0);

    while (true) {

        if (sleepBetweenRuns > 0) {
            if (logger.isTraceEnabled()) {
                logger.trace("sleep between rounds...sleep...{}", sleepBetweenRuns);
            }
            try {
                Thread.sleep(sleepBetweenRuns);
            } catch (InterruptedException ignored) {
            }
        }

        Timer.Context timerContext = timer.time();
        rx.Observable.from(legacyQueueManager.getMessages(MAX_TAKE, ApplicationQueueMessage.class))
                .buffer(MAX_TAKE).doOnNext(messages -> {

                    try {
                        if (logger.isTraceEnabled()) {
                            logger.trace("retrieved batch of {} messages from queue {}", messages.size(),
                                    queueName);
                        }

                        if (messages.size() > 0) {
                            HashMap<UUID, List<LegacyQueueMessage>> messageMap = new HashMap<>(messages.size());

                            //group messages into hash map by app id
                            for (LegacyQueueMessage message : messages) {
                                //TODO: stop copying around this area as it gets notification specific.
                                ApplicationQueueMessage queueMessage = (ApplicationQueueMessage) message
                                        .getBody();
                                UUID applicationId = queueMessage.getApplicationId();

                                // Groups queue messages by application Id,
                                // (they are all probably going to the same place)
                                if (!messageMap.containsKey(applicationId)) {
                                    //For each app id it sends the set.
                                    List<LegacyQueueMessage> lqms = new ArrayList<LegacyQueueMessage>();
                                    lqms.add(message);
                                    messageMap.put(applicationId, lqms);
                                } else {
                                    messageMap.get(applicationId).add(message);
                                }
                            }

                            long now = System.currentTimeMillis();
                            Observable merge = null;

                            //send each set of app ids together
                            for (Map.Entry<UUID, List<LegacyQueueMessage>> entry : messageMap.entrySet()) {
                                UUID applicationId = entry.getKey();

                                ApplicationQueueManager manager = applicationQueueManagerCache
                                        .getApplicationQueueManager(emf.getEntityManager(applicationId),
                                                legacyQueueManager,
                                                new JobScheduler(smf.getServiceManager(applicationId),
                                                        emf.getEntityManager(applicationId)),
                                                metricsService, properties);

                                if (logger.isTraceEnabled()) {
                                    logger.trace("send batch for app {} of {} messages", entry.getKey(),
                                            entry.getValue().size());
                                }
                                Observable current = manager.sendBatchToProviders(entry.getValue(), queueName);

                                if (merge == null)
                                    merge = current;
                                else {
                                    merge = Observable.merge(merge, current);
                                }
                            }

                            if (merge != null) {
                                merge.toBlocking().lastOrDefault(null);
                            }
                            legacyQueueManager.commitMessages(messages);

                            meter.mark(messages.size());
                            if (logger.isTraceEnabled()) {
                                logger.trace("sent batch {} messages duration {} ms", messages.size(),
                                        System.currentTimeMillis() - now);
                            }

                            if (runCount.incrementAndGet() % consecutiveCallsToRemoveDevices == 0) {
                                for (ApplicationQueueManager aqm : applicationQueueManagerCache.asMap()
                                        .values()) {
                                    try {
                                        aqm.asyncCheckForInactiveDevices();
                                    } catch (Exception inactiveDeviceException) {
                                        logger.error("Inactive Device Get failed", inactiveDeviceException);
                                    }
                                }
                                //clear everything
                                runCount.set(0);
                            }
                        }

                else {
                            if (logger.isTraceEnabled()) {
                                logger.trace("no messages...sleep...{}", sleepWhenNoneFound);
                            }
                            try {
                                Thread.sleep(sleepWhenNoneFound);
                            } catch (InterruptedException e) {
                                // noop
                            }
                        }
                        timerContext.stop();
                        //send to the providers
                        consecutiveExceptions.set(0);
                    } catch (Exception ex) {
                        logger.error("failed to dequeue", ex);

                        // clear the queue name cache b/c tests might have wiped the keyspace
                        legacyQueueManager.clearQueueNameCache();
                        try {
                            long sleeptime = sleepWhenNoneFound * consecutiveExceptions.incrementAndGet();
                            long maxSleep = 15000;
                            sleeptime = sleeptime > maxSleep ? maxSleep : sleeptime;
                            logger.info("sleeping due to failures {} ms", sleeptime);
                            Thread.sleep(sleeptime);
                        } catch (InterruptedException ie) {
                            if (logger.isTraceEnabled()) {
                                logger.trace("sleep interrupted");
                            }
                        }
                    }
                }).toBlocking().lastOrDefault(null);

    }
}

From source file:org.tvbrowser.tvbrowser.TvBrowser.java

private void updateTvData() {
    if (!TvDataUpdateService.IS_RUNNING) {
        Cursor test = getContentResolver().query(TvBrowserContentProvider.CONTENT_URI_CHANNELS, null,
                TvBrowserContentProvider.CHANNEL_KEY_SELECTION + "=1", null, null);

        if (test.getCount() > 0) {
            AlertDialog.Builder builder = new AlertDialog.Builder(TvBrowser.this);

            RelativeLayout dataDownload = (RelativeLayout) getLayoutInflater()
                    .inflate(R.layout.dialog_data_update_selection, getParentViewGroup(), false);

            final Spinner days = (Spinner) dataDownload
                    .findViewById(R.id.dialog_data_update_selection_download_days);
            final CheckBox pictures = (CheckBox) dataDownload
                    .findViewById(R.id.dialog_data_update_selection_download_picture);

            final Spinner autoUpdate = (Spinner) dataDownload
                    .findViewById(R.id.dialog_data_update_preferences_auto_update_selection_type);
            final Spinner frequency = (Spinner) dataDownload
                    .findViewById(R.id.dialog_data_update_preferences_auto_update_selection_frequency);
            final CheckBox onlyWiFi = (CheckBox) dataDownload
                    .findViewById(R.id.dialog_data_update_preferences_auto_update_selection_type_connection);
            final TextView timeLabel = (TextView) dataDownload
                    .findViewById(R.id.dialog_data_update_preferences_auto_update_selection_time_label);
            final TextView time = (TextView) dataDownload
                    .findViewById(R.id.dialog_data_update_preferences_auto_update_selection_time);
            time.setTextColor(onlyWiFi.getTextColors());

            String currentDownloadDays = PrefUtils.getStringValue(R.string.DAYS_TO_DOWNLOAD,
                    R.string.days_to_download_default);

            final String[] possibleDownloadDays = getResources().getStringArray(R.array.download_days);

            for (int i = 0; i < possibleDownloadDays.length; i++) {
                if (currentDownloadDays.equals(possibleDownloadDays[i])) {
                    days.setSelection(i);
                    break;
                }// w  ww . j  a  v  a 2s  .c o  m
            }

            pictures.setChecked(
                    PrefUtils.getBooleanValue(R.string.LOAD_PICTURE_DATA, R.bool.load_picture_data_default));

            String currentAutoUpdateValue = PrefUtils.getStringValue(R.string.PREF_AUTO_UPDATE_TYPE,
                    R.string.pref_auto_update_type_default);
            String currentAutoUpdateFrequency = PrefUtils.getStringValue(R.string.PREF_AUTO_UPDATE_FREQUENCY,
                    R.string.pref_auto_update_frequency_default);

            if (currentAutoUpdateValue.equals("0")) {
                frequency.setEnabled(false);
                onlyWiFi.setEnabled(false);
                timeLabel.setEnabled(false);
                time.setEnabled(false);
                frequency.setVisibility(View.GONE);
                onlyWiFi.setVisibility(View.GONE);
                timeLabel.setVisibility(View.GONE);
                time.setVisibility(View.GONE);
            } else if (currentAutoUpdateValue.equals("1")) {
                autoUpdate.setSelection(1);
                timeLabel.setEnabled(false);
                time.setEnabled(false);
                timeLabel.setVisibility(View.GONE);
                time.setVisibility(View.GONE);
            } else if (currentAutoUpdateValue.equals("2")) {
                autoUpdate.setSelection(2);
            }

            final String[] autoFrequencyPossibleValues = getResources()
                    .getStringArray(R.array.pref_auto_update_frequency_values);

            for (int i = 0; i < autoFrequencyPossibleValues.length; i++) {
                if (autoFrequencyPossibleValues[i].equals(currentAutoUpdateFrequency)) {
                    frequency.setSelection(i);
                    break;
                }
            }

            onlyWiFi.setChecked(PrefUtils.getBooleanValue(R.string.PREF_AUTO_UPDATE_ONLY_WIFI,
                    R.bool.pref_auto_update_only_wifi_default));

            final AtomicInteger currentAutoUpdateTime = new AtomicInteger(PrefUtils.getIntValue(
                    R.string.PREF_AUTO_UPDATE_START_TIME, R.integer.pref_auto_update_start_time_default));

            Calendar now = Calendar.getInstance();

            now.set(Calendar.HOUR_OF_DAY, currentAutoUpdateTime.get() / 60);
            now.set(Calendar.MINUTE, currentAutoUpdateTime.get() % 60);
            now.set(Calendar.SECOND, 0);
            now.set(Calendar.MILLISECOND, 0);

            time.setText(DateFormat.getTimeFormat(TvBrowser.this).format(now.getTime()));

            autoUpdate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    frequency.setEnabled(position != 0);
                    onlyWiFi.setEnabled(position != 0);

                    if (position != 0) {
                        frequency.setVisibility(View.VISIBLE);
                        onlyWiFi.setVisibility(View.VISIBLE);
                    } else {
                        frequency.setVisibility(View.GONE);
                        onlyWiFi.setVisibility(View.GONE);
                    }

                    timeLabel.setEnabled(position == 2);
                    time.setEnabled(position == 2);

                    if (position == 2) {
                        timeLabel.setVisibility(View.VISIBLE);
                        time.setVisibility(View.VISIBLE);
                    } else {
                        timeLabel.setVisibility(View.GONE);
                        time.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

            View.OnClickListener onClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder b2 = new AlertDialog.Builder(TvBrowser.this);

                    LinearLayout timeSelection = (LinearLayout) getLayoutInflater().inflate(
                            R.layout.dialog_data_update_selection_auto_update_time, getParentViewGroup(),
                            false);

                    final TimePicker timePick = (TimePicker) timeSelection
                            .findViewById(R.id.dialog_data_update_selection_auto_update_selection_time);
                    timePick.setIs24HourView(DateFormat.is24HourFormat(TvBrowser.this));
                    timePick.setCurrentHour(currentAutoUpdateTime.get() / 60);
                    timePick.setCurrentMinute(currentAutoUpdateTime.get() % 60);

                    b2.setView(timeSelection);

                    b2.setPositiveButton(android.R.string.ok, new OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            currentAutoUpdateTime
                                    .set(timePick.getCurrentHour() * 60 + timePick.getCurrentMinute());

                            Calendar now = Calendar.getInstance();

                            now.set(Calendar.HOUR_OF_DAY, currentAutoUpdateTime.get() / 60);
                            now.set(Calendar.MINUTE, currentAutoUpdateTime.get() % 60);
                            now.set(Calendar.SECOND, 0);
                            now.set(Calendar.MILLISECOND, 0);

                            time.setText(DateFormat.getTimeFormat(TvBrowser.this).format(now.getTime()));
                        }
                    });
                    b2.setNegativeButton(android.R.string.cancel, null);

                    b2.show();
                }
            };

            time.setOnClickListener(onClickListener);
            timeLabel.setOnClickListener(onClickListener);

            builder.setTitle(R.string.download_data);
            builder.setView(dataDownload);

            builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String value = possibleDownloadDays[days.getSelectedItemPosition()];

                    Editor settings = PreferenceManager.getDefaultSharedPreferences(TvBrowser.this).edit();

                    if (PrefUtils.getStringValueAsInt(R.string.PREF_AUTO_UPDATE_RANGE,
                            R.string.pref_auto_update_range_default) < Integer.parseInt(value)) {
                        settings.putString(getString(R.string.PREF_AUTO_UPDATE_RANGE), value);
                    }

                    settings.putString(getString(R.string.DAYS_TO_DOWNLOAD), value);
                    settings.putBoolean(getString(R.string.LOAD_PICTURE_DATA), pictures.isChecked());
                    settings.putString(getString(R.string.PREF_AUTO_UPDATE_TYPE),
                            String.valueOf(autoUpdate.getSelectedItemPosition()));

                    if (autoUpdate.getSelectedItemPosition() == 1
                            || autoUpdate.getSelectedItemPosition() == 2) {
                        settings.putString(getString(R.string.PREF_AUTO_UPDATE_FREQUENCY),
                                autoFrequencyPossibleValues[frequency.getSelectedItemPosition()]);
                        settings.putBoolean(getString(R.string.PREF_AUTO_UPDATE_ONLY_WIFI),
                                onlyWiFi.isChecked());

                        if (autoUpdate.getSelectedItemPosition() == 2) {
                            settings.putInt(getString(R.string.PREF_AUTO_UPDATE_START_TIME),
                                    currentAutoUpdateTime.get());
                        }
                    }

                    settings.commit();

                    IOUtils.handleDataUpdatePreferences(TvBrowser.this);

                    Intent startDownload = new Intent(TvBrowser.this, TvDataUpdateService.class);
                    startDownload.putExtra(TvDataUpdateService.TYPE, TvDataUpdateService.TV_DATA_TYPE);
                    startDownload.putExtra(getResources().getString(R.string.DAYS_TO_DOWNLOAD),
                            Integer.parseInt(value));

                    startService(startDownload);

                    updateProgressIcon(true);
                }
            });
            builder.setNegativeButton(android.R.string.cancel, null);
            builder.show();
        } else {
            Cursor test2 = getContentResolver().query(TvBrowserContentProvider.CONTENT_URI_CHANNELS, null, null,
                    null, null);

            boolean loadAgain = test2.getCount() < 1;

            test2.close();

            selectChannels(loadAgain);
        }

        test.close();
    }
}