Search in sources :

Example 1 with Duration

use of org.threeten.bp.Duration in project Douya by DreaminginCodeZH.

the class TimeUtils method formatDateTime.

public static String formatDateTime(ZonedDateTime dateTime, Context context) {
    ZonedDateTime now = ZonedDateTime.now().withZoneSameInstant(dateTime.getZone());
    LocalDate date = dateTime.toLocalDate();
    LocalDate nowDate = now.toLocalDate();
    if (date.equals(nowDate)) {
        Duration duration = Duration.between(dateTime, now);
        if (duration.compareTo(Duration.ZERO) > 0) {
            if (duration.compareTo(JUST_NOW_DURATION) < 0) {
                return context.getString(R.string.just_now);
            } else if (duration.compareTo(MINUTE_PATTERN_DURATION) < 0) {
                return context.getString(R.string.minute_format, Math.round((float) duration.getSeconds() / SECONDS_PER_MINUTE));
            } else if (duration.compareTo(HOUR_PATTERN_DURATION) < 0) {
                return context.getString(R.string.hour_format, Math.round((float) duration.getSeconds() / SECONDS_PER_HOUR));
            }
        }
        return DateTimeFormatter.ofPattern(context.getString(R.string.today_hour_minute_pattern)).format(dateTime);
    }
    if (date.plusDays(1).equals(nowDate)) {
        return DateTimeFormatter.ofPattern(context.getString(R.string.yesterday_hour_minute_pattern)).format(dateTime);
    } else if (date.getYear() == nowDate.getYear()) {
        return DateTimeFormatter.ofPattern(context.getString(R.string.month_day_hour_minute_pattern)).format(dateTime);
    } else {
        return DateTimeFormatter.ofPattern(context.getString(R.string.date_hour_minute_pattern)).format(dateTime);
    }
}
Also used : ZonedDateTime(org.threeten.bp.ZonedDateTime) Duration(org.threeten.bp.Duration) LocalDate(org.threeten.bp.LocalDate)

Example 2 with Duration

use of org.threeten.bp.Duration in project google-cloud-java by GoogleCloudPlatform.

the class PollingSubscriberConnection method pullMessages.

private void pullMessages(final Duration backoff) {
    ListenableFuture<PullResponse> pullResult = stub.withDeadlineAfter(DEFAULT_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS).pull(PullRequest.newBuilder().setSubscription(subscription).setMaxMessages(maxDesiredPulledMessages).setReturnImmediately(true).build());
    Futures.addCallback(pullResult, new FutureCallback<PullResponse>() {

        @Override
        public void onSuccess(PullResponse pullResponse) {
            if (pullResponse.getReceivedMessagesCount() == 0) {
                // No messages in response, possibly caught up in backlog, we backoff to avoid
                // slamming the server.
                pollingExecutor.schedule(new Runnable() {

                    @Override
                    public void run() {
                        Duration newBackoff = backoff.multipliedBy(2);
                        if (newBackoff.compareTo(MAX_BACKOFF) > 0) {
                            newBackoff = MAX_BACKOFF;
                        }
                        pullMessages(newBackoff);
                    }
                }, backoff.toMillis(), TimeUnit.MILLISECONDS);
                return;
            }
            messageDispatcher.processReceivedMessages(pullResponse.getReceivedMessagesList(), new Runnable() {

                @Override
                public void run() {
                    pullMessages(INITIAL_BACKOFF);
                }
            });
        }

        @Override
        public void onFailure(Throwable cause) {
            if (!isAlive()) {
                // we don't care about subscription failures when we're no longer running.
                logger.log(Level.FINE, "pull failure after service no longer running", cause);
                return;
            }
            if (StatusUtil.isRetryable(cause)) {
                logger.log(Level.WARNING, "Failed to pull messages (recoverable): ", cause);
                pollingExecutor.schedule(new Runnable() {

                    @Override
                    public void run() {
                        Duration newBackoff = backoff.multipliedBy(2);
                        if (newBackoff.compareTo(MAX_BACKOFF) > 0) {
                            newBackoff = MAX_BACKOFF;
                        }
                        pullMessages(newBackoff);
                    }
                }, backoff.toMillis(), TimeUnit.MILLISECONDS);
            } else {
                messageDispatcher.stop();
                notifyFailed(cause);
            }
        }
    }, pollingExecutor);
}
Also used : PullResponse(com.google.pubsub.v1.PullResponse) Duration(org.threeten.bp.Duration)

Example 3 with Duration

use of org.threeten.bp.Duration in project google-cloud-java by GoogleCloudPlatform.

the class PublisherSnippets method getPublisherWithCustomBatchSettings.

public Publisher getPublisherWithCustomBatchSettings(TopicName topicName) throws Exception {
    // [START pubsub_publisher_batch_settings]
    // Batch settings control how the publisher batches messages
    // default : 1kb
    long requestBytesThreshold = 5000L;
    // default : 100
    long messageCountBatchSize = 10L;
    // default : 1 ms
    Duration publishDelayThreshold = Duration.ofMillis(100);
    // Publish request get triggered based on request size, messages count & time since last publish
    BatchingSettings batchingSettings = BatchingSettings.newBuilder().setElementCountThreshold(messageCountBatchSize).setRequestByteThreshold(requestBytesThreshold).setDelayThreshold(publishDelayThreshold).build();
    Publisher publisher = Publisher.defaultBuilder(topicName).setBatchingSettings(batchingSettings).build();
    // [END pubsub_publisher_batch_settings]
    return publisher;
}
Also used : Duration(org.threeten.bp.Duration) Publisher(com.google.cloud.pubsub.spi.v1.Publisher) BatchingSettings(com.google.api.gax.batching.BatchingSettings)

Example 4 with Duration

use of org.threeten.bp.Duration in project google-cloud-java by GoogleCloudPlatform.

the class PublisherSnippets method getPublisherWithCustomRetrySettings.

public Publisher getPublisherWithCustomRetrySettings(TopicName topicName) throws Exception {
    // [START pubsub_publisher_retry_settings]
    // Retry settings control how the publisher handles retryable failures
    // default : 1 ms
    Duration retryDelay = Duration.ofMillis(100);
    // back off for repeated failures
    double retryDelayMultiplier = 2.0;
    // default : 10 seconds
    Duration maxRetryDelay = Duration.ofSeconds(5);
    RetrySettings retrySettings = RetrySettings.newBuilder().setInitialRetryDelay(retryDelay).setRetryDelayMultiplier(retryDelayMultiplier).setMaxRetryDelay(maxRetryDelay).build();
    Publisher publisher = Publisher.defaultBuilder(topicName).setRetrySettings(retrySettings).build();
    // [END pubsub_publisher_retry_settings]
    return publisher;
}
Also used : RetrySettings(com.google.api.gax.retrying.RetrySettings) Duration(org.threeten.bp.Duration) Publisher(com.google.cloud.pubsub.spi.v1.Publisher)

Aggregations

Duration (org.threeten.bp.Duration)4 Publisher (com.google.cloud.pubsub.spi.v1.Publisher)2 BatchingSettings (com.google.api.gax.batching.BatchingSettings)1 RetrySettings (com.google.api.gax.retrying.RetrySettings)1 PullResponse (com.google.pubsub.v1.PullResponse)1 LocalDate (org.threeten.bp.LocalDate)1 ZonedDateTime (org.threeten.bp.ZonedDateTime)1