1- // Copyright (c) 2024 Broadcom. All Rights Reserved.
1+ // Copyright (c) 2024-2025 Broadcom. All Rights Reserved.
22// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33//
4- // Licensed under the Apache License, Version 2.0 (the "License");
5- // you may not use this file except in compliance with the License.
6- // You may obtain a copy of the License at
4+ // This software, the RabbitMQ Stream Java client library, is dual-licensed under the
5+ // Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL").
6+ // For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL,
7+ // please see LICENSE-APACHE2.
78//
8- // http://www.apache.org/licenses/LICENSE-2.0
9- //
10- // Unless required by applicable law or agreed to in writing, software
11- // distributed under the License is distributed on an "AS IS" BASIS,
12- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- // See the License for the specific language governing permissions and
14- // limitations under the License.
9+ // This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
10+ // either express or implied. See the LICENSE file for specific language governing
11+ // rights and limitations of this software.
1512//
1613// If you have any questions regarding licensing, please contact us at
1714@@ -80,16 +77,16 @@ private boolean expiresSoon(Token ignores) {
8077 }
8178
8279 private Token getToken () {
83- if (LOGGER . isDebugEnabled ()) {
80+ if (debug ()) {
8481 LOGGER .debug (
8582 "Requesting new token ({})..." , registrationSummary (this .registrations .values ()));
8683 }
8784 long start = 0L ;
88- if (LOGGER . isDebugEnabled ()) {
85+ if (debug ()) {
8986 start = System .nanoTime ();
9087 }
9188 Token token = requester .request ();
92- if (LOGGER . isDebugEnabled ()) {
89+ if (debug ()) {
9390 LOGGER .debug (
9491 "Got new token in {} ms, token expires on {} ({})" ,
9592 Duration .ofNanos (System .nanoTime () - start ),
@@ -128,14 +125,14 @@ private void updateRegistrations(Token t) {
128125 registration .registrationToken = this .token ;
129126 refreshedCount ++;
130127 } else {
131- if (LOGGER . isDebugEnabled ()) {
128+ if (debug ()) {
132129 LOGGER .debug (
133130 "Not updating registration {} (closed or already has the new token)" ,
134131 registration .name ());
135132 }
136133 }
137134 } else {
138- if (LOGGER . isDebugEnabled ()) {
135+ if (debug ()) {
139136 LOGGER .debug (
140137 "Not updating registration {} (the token has changed)" , registration .name ());
141138 }
@@ -160,33 +157,46 @@ private void token(Token t) {
160157 private void scheduleTokenRefresh (Token t ) {
161158 if (this .schedulingRefresh .compareAndSet (false , true )) {
162159 if (this .refreshTask != null ) {
160+ if (debug ()) {
161+ LOGGER .debug ("Cancelling refresh task (scheduling a new one)" );
162+ }
163163 this .refreshTask .cancel (false );
164164 }
165165 Duration delay = this .refreshDelayStrategy .apply (t .expirationTime ());
166166 if (!this .registrations .isEmpty ()) {
167- if (LOGGER . isDebugEnabled ()) {
167+ if (debug ()) {
168168 LOGGER .debug (
169- "Scheduling token retrieval in {} ({})" ,
169+ "Scheduling token update in {} ({})" ,
170170 delay ,
171171 registrationSummary (this .registrations .values ()));
172172 }
173173 this .refreshTask =
174174 this .scheduledExecutorService .schedule (
175175 () -> {
176+ if (debug ()) {
177+ LOGGER .debug ("Starting token update task" );
178+ }
176179 Token previousToken = this .token ;
177180 this .lock ();
178181 try {
179182 if (this .token .equals (previousToken )) {
180183 Token newToken = getToken ();
181184 token (newToken );
182185 updateRegistrations (newToken );
186+ } else {
187+ if (debug ()) {
188+ LOGGER .debug ("Token has already been updated" );
189+ }
183190 }
184191 } finally {
185192 unlock ();
186193 }
187194 },
188195 delay .toMillis (),
189196 TimeUnit .MILLISECONDS );
197+ if (debug ()) {
198+ LOGGER .debug ("Task scheduled" );
199+ }
190200 } else {
191201 this .refreshTask = null ;
192202 }
@@ -214,6 +224,9 @@ private RegistrationImpl(Long id, String name, AuthenticationCallback updateCall
214224
215225 @ Override
216226 public void connect (AuthenticationCallback callback ) {
227+ if (debug ()) {
228+ LOGGER .debug ("Connecting registration {}" , this .name );
229+ }
217230 boolean shouldRefresh = false ;
218231 Token tokenToUse ;
219232 lock ();
@@ -235,6 +248,11 @@ public void connect(AuthenticationCallback callback) {
235248 } finally {
236249 unlock ();
237250 }
251+ if (debug ()) {
252+ if (debug ()) {
253+ LOGGER .debug ("Authenticating registration {}" , this .name );
254+ }
255+ }
238256 callback .authenticate ("" , tokenToUse .value ());
239257 if (shouldRefresh ) {
240258 updateRegistrations (tokenToUse );
@@ -244,6 +262,7 @@ public void connect(AuthenticationCallback callback) {
244262 @ Override
245263 public void close () {
246264 if (this .closed .compareAndSet (false , true )) {
265+ LOGGER .debug ("Closing credentials registration {}" , this .name );
247266 registrations .remove (this .id );
248267 ScheduledFuture <?> task = refreshTask ;
249268 if (registrations .isEmpty () && task != null ) {
@@ -325,4 +344,8 @@ public Duration apply(Instant expirationTime) {
325344 private static String registrationSummary (Collection <? extends Registration > registrations ) {
326345 return registrations .stream ().map (Registration ::toString ).collect (Collectors .joining (", " ));
327346 }
347+
348+ private static boolean debug () {
349+ return LOGGER .isDebugEnabled ();
350+ }
328351}
0 commit comments