All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.microsoft.azure.eventhubs.impl.ActiveClientTokenManager Maven / Gradle / Ivy

Go to download

Please note, a newer package azure-messaging-eventhubs for Azure Event Hubs is available at https://search.maven.org/artifact/com.azure/azure-messaging-eventhubs as of February 2020. While this package will continue to receive critical bug fixes, we strongly encourage you to upgrade. Read the migration guide at https://aka.ms/azsdk/java/migrate/eh for more details.

The newest version!
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.azure.eventhubs.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;

final class ActiveClientTokenManager {

    private static final Logger TRACE_LOGGER = LoggerFactory.getLogger(ActiveClientTokenManager.class);
    private final Object timerLock;
    private final Runnable sendTokenTask;
    private final ClientEntity clientEntity;
    private final Duration tokenRefreshInterval;
    private final SchedulerProvider schedulerProvider;
    private final Timer timerScheduler;
    private CompletableFuture timer;

    ActiveClientTokenManager(
            final ClientEntity clientEntity,
            final Runnable sendTokenAsync,
            final Duration tokenRefreshInterval,
            final SchedulerProvider schedulerProvider) {

        this.sendTokenTask = sendTokenAsync;
        this.clientEntity = clientEntity;
        this.tokenRefreshInterval = tokenRefreshInterval;
        this.timerLock = new Object();
        this.schedulerProvider = schedulerProvider;
        this.timerScheduler = new Timer(schedulerProvider);

        synchronized (this.timerLock) {
            this.timer = this.timerScheduler.schedule(new TimerCallback(), tokenRefreshInterval);
        }
    }

    public void cancel() {
        if (TRACE_LOGGER.isInfoEnabled()) {
            TRACE_LOGGER.info(String.format(Locale.US, "clientEntity[%s] - canceling ActiveClientLinkManager",
                    clientEntity.getClientId()));
        }

        synchronized (this.timerLock) {
            this.timer.cancel(false);
        }
    }

    private class TimerCallback implements Runnable {

        @Override
        public void run() {

            if (!clientEntity.getIsClosingOrClosed()) {

                sendTokenTask.run();

                synchronized (ActiveClientTokenManager.this.timerLock) {
                    ActiveClientTokenManager.this.timer = ActiveClientTokenManager.this.timerScheduler.schedule(new TimerCallback(), tokenRefreshInterval);
                }
            } else {

                if (TRACE_LOGGER.isInfoEnabled()) {
                    TRACE_LOGGER.info(String.format(Locale.US, "clientEntity[%s] - closing ActiveClientLinkManager",
                            clientEntity.getClientId()));
                }
            }
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy