66import java .util .Locale ;
77import java .util .Map ;
88import java .util .Set ;
9- import java .util .Timer ;
109import java .util .TimerTask ;
1110import java .util .concurrent .atomic .AtomicBoolean ;
1211
4645import io .ably .lib .types .PublishResult ;
4746import io .ably .lib .types .Summary ;
4847import io .ably .lib .types .UpdateDeleteResult ;
48+ import io .ably .lib .util .Clock ;
4949import io .ably .lib .util .CollectionUtils ;
5050import io .ably .lib .util .EventEmitter ;
5151import io .ably .lib .util .Listeners ;
5252import io .ably .lib .util .Log ;
53+ import io .ably .lib .util .NamedTimer ;
5354import io .ably .lib .util .ReconnectionStrategy ;
5455import io .ably .lib .util .StringUtils ;
56+ import io .ably .lib .util .SystemClock ;
5557import org .jetbrains .annotations .Blocking ;
5658import org .jetbrains .annotations .NonBlocking ;
5759import org .jetbrains .annotations .Nullable ;
@@ -508,21 +510,20 @@ private void setFailed(ErrorInfo reason) {
508510 }
509511
510512 /* Timer for attach operation */
511- private Timer attachTimer ;
513+ private NamedTimer attachTimer ;
512514
513515 /* Timer for reattaching if attach failed */
514- private Timer reattachTimer ;
516+ private NamedTimer reattachTimer ;
515517
516518 /**
517519 * Cancel attach/reattach timers
518520 */
519521 synchronized private void clearAttachTimers () {
520- Timer [] timers = new Timer []{attachTimer , reattachTimer };
522+ NamedTimer [] timers = new NamedTimer []{attachTimer , reattachTimer };
521523 attachTimer = reattachTimer = null ;
522- for (Timer t : timers ) {
524+ for (NamedTimer t : timers ) {
523525 if (t != null ) {
524526 t .cancel ();
525- t .purge ();
526527 }
527528 }
528529 }
@@ -537,9 +538,9 @@ private void attachWithTimeout(final CompletionListener listener) throws AblyExc
537538 */
538539 synchronized private void attachWithTimeout (final boolean forceReattach , final CompletionListener listener , ErrorInfo reattachmentReason ) {
539540 checkChannelIsNotReleased ();
540- Timer currentAttachTimer ;
541+ NamedTimer currentAttachTimer ;
541542 try {
542- currentAttachTimer = new Timer ( );
543+ currentAttachTimer = clock . newTimer ( "attach-timer" );
543544 } catch (Throwable t ) {
544545 /* an exception instancing the timer can arise because the runtime is exiting */
545546 callCompletionListenerError (listener , ErrorInfo .fromThrowable (t ));
@@ -571,7 +572,7 @@ public void onError(ErrorInfo reason) {
571572 return ;
572573 }
573574
574- final Timer inProgressTimer = currentAttachTimer ;
575+ final NamedTimer inProgressTimer = currentAttachTimer ;
575576 attachTimer .schedule (
576577 new TimerTask () {
577578 @ Override
@@ -601,9 +602,9 @@ private void checkChannelIsNotReleased() {
601602 * try to attach the channel
602603 */
603604 synchronized private void reattachAfterTimeout () {
604- Timer currentReattachTimer ;
605+ NamedTimer currentReattachTimer ;
605606 try {
606- currentReattachTimer = new Timer ( );
607+ currentReattachTimer = clock . newTimer ( "reattach-timer" );
607608 } catch (Throwable t ) {
608609 /* an exception instancing the timer can arise because the runtime is exiting */
609610 return ;
@@ -613,7 +614,7 @@ synchronized private void reattachAfterTimeout() {
613614 this .retryAttempt ++;
614615 int retryDelay = ReconnectionStrategy .getRetryTime (ably .options .channelRetryTimeout , retryAttempt );
615616
616- final Timer inProgressTimer = currentReattachTimer ;
617+ final NamedTimer inProgressTimer = currentReattachTimer ;
617618 reattachTimer .schedule (new TimerTask () {
618619 @ Override
619620 public void run () {
@@ -640,9 +641,9 @@ public void run() {
640641 */
641642 synchronized private void detachWithTimeout (final CompletionListener listener ) {
642643 final ChannelState originalState = state ;
643- Timer currentDetachTimer ;
644+ NamedTimer currentDetachTimer ;
644645 try {
645- currentDetachTimer = released .get () ? null : new Timer ( );
646+ currentDetachTimer = released .get () ? null : clock . newTimer ( "detach-timer" );
646647 } catch (Throwable t ) {
647648 /* an exception instancing the timer can arise because the runtime is exiting */
648649 callCompletionListenerError (listener , ErrorInfo .fromThrowable (t ));
@@ -676,7 +677,7 @@ public void onError(ErrorInfo reason) {
676677 return ;
677678 }
678679
679- final Timer inProgressTimer = currentDetachTimer ;
680+ final NamedTimer inProgressTimer = currentDetachTimer ;
680681 attachTimer .schedule (new TimerTask () {
681682 @ Override
682683 public void run () {
@@ -1684,6 +1685,7 @@ else if(stateChange.current.equals(failureState)) {
16841685 ChannelBase (AblyRealtime ably , String name , ChannelOptions options , @ Nullable LiveObjectsPlugin liveObjectsPlugin ) throws AblyException {
16851686 Log .v (TAG , "RealtimeChannel(); channel = " + name );
16861687 this .ably = ably ;
1688+ this .clock = SystemClock .clockFrom (ably .options );
16871689 this .name = name ;
16881690 this .basePath = "/channels/" + HttpUtils .encodeURIComponent (name );
16891691 this .setOptions (options );
@@ -1808,6 +1810,7 @@ public void sendProtocolMessage(ProtocolMessage protocolMessage, CompletionListe
18081810
18091811 private static final String TAG = Channel .class .getName ();
18101812 final AblyRealtime ably ;
1813+ final Clock clock ;
18111814 final String basePath ;
18121815 ChannelOptions options ;
18131816 /**
0 commit comments