@Override public void addTokenChangeListener(TokenChangeListener listener) { CredentialsChangedListener listenerWrapper = new TokenChangeListenerWrapper( listener, executor, authVariable); credentials.addChangeListener(listenerWrapper); }
/** * Starts the TokenRefresher if not already started. Starts listening to credentials changed * events, and schedules refresh events every time the OAuth2 token changes. If no active * token is present, or if the available token is set to expire soon, this will also schedule * a refresh event to be executed immediately. * * <p>This operation is idempotent. Calling it multiple times, or calling it after the * refresher has been stopped has no effect. */ final synchronized void start() { // Allow starting only from the ready state. if (!state.compareAndSet(State.READY, State.STARTED)) { return; } logger.debug("Starting the proactive token refresher"); credentials.addChangeListener(this); AccessToken accessToken = credentials.getAccessToken(); long refreshDelay; if (accessToken != null) { // If the token is about to expire (i.e. expires in less than 5 minutes), schedule a // refresh event with 0 delay. Otherwise schedule a refresh event at the regular token // expiry time, minus 5 minutes. refreshDelay = Math.max(getRefreshDelay(accessToken), 0L); } else { // If there is no token fetched so far, fetch one immediately. refreshDelay = 0L; } scheduleRefresh(refreshDelay); }