@@ -26,12 +26,10 @@ public final class HttpRequestService implements RequestService, Runnable {
2626 private final PeriodicTaskExecutor executor ;
2727 private final PeriodicDelay periodicRequestDelay ;
2828 private final PeriodicDelay periodicRetryDelay ;
29- private final Object runningLock = new Object ();
3029 private final AtomicBoolean retryModeEnabled = new AtomicBoolean (false );
30+ private final AtomicBoolean isRunning = new AtomicBoolean (false );
3131 @ Nullable private Callback callback ;
3232 @ Nullable private Supplier <Request > requestSupplier ;
33- private boolean isRunning = false ;
34- private boolean isStopped = false ;
3533 public static final PeriodicDelay DEFAULT_DELAY_BETWEEN_REQUESTS =
3634 PeriodicDelay .ofFixedDuration (Duration .ofSeconds (30 ));
3735
@@ -75,27 +73,18 @@ public static HttpRequestService create(
7573
7674 @ Override
7775 public void start (Callback callback , Supplier <Request > requestSupplier ) {
78- synchronized (runningLock ) {
79- if (isStopped ) {
80- throw new IllegalStateException ("RequestDispatcher has been stopped" );
81- }
82- if (isRunning ) {
83- throw new IllegalStateException ("RequestDispatcher is already running" );
84- }
76+ if (isRunning .compareAndSet (false , true )) {
8577 this .callback = callback ;
8678 this .requestSupplier = requestSupplier ;
8779 executor .start (this );
88- isRunning = true ;
80+ } else {
81+ throw new IllegalStateException ("RequestDispatcher is already running" );
8982 }
9083 }
9184
9285 @ Override
9386 public void stop () {
94- synchronized (runningLock ) {
95- if (!isRunning || isStopped ) {
96- return ;
97- }
98- isStopped = true ;
87+ if (isRunning .compareAndSet (true , false )) {
9988 executor .executeNow ();
10089 executor .stop ();
10190 }
@@ -165,7 +154,7 @@ private void handleHttpError(HttpSender.Response response) {
165154 String retryAfterHeader = response .getHeader ("Retry-After" );
166155 Duration retryAfter = null ;
167156 if (retryAfterHeader != null ) {
168- // retryAfter = TODO parse header to duration
157+ retryAfter = Duration . ofSeconds ( Long . parseLong ( retryAfterHeader ));
169158 }
170159 enableRetryMode (retryAfter );
171160 }
0 commit comments