18
18
19
19
import java .util .concurrent .Callable ;
20
20
import java .util .concurrent .ExecutorService ;
21
- import java .util .concurrent .Executors ;
22
21
import java .util .concurrent .Future ;
23
22
import java .util .concurrent .TimeUnit ;
24
23
import java .util .concurrent .locks .Lock ;
25
24
26
- import org .apache .commons .logging .Log ;
27
- import org .apache .commons .logging .LogFactory ;
28
-
29
25
import org .springframework .beans .factory .DisposableBean ;
30
26
import org .springframework .context .ApplicationEventPublisher ;
31
27
import org .springframework .context .ApplicationEventPublisherAware ;
32
28
import org .springframework .context .SmartLifecycle ;
29
+ import org .springframework .core .log .LogAccessor ;
30
+ import org .springframework .core .task .AsyncTaskExecutor ;
31
+ import org .springframework .core .task .SimpleAsyncTaskExecutor ;
32
+ import org .springframework .core .task .support .TaskExecutorAdapter ;
33
33
import org .springframework .integration .leader .Candidate ;
34
34
import org .springframework .integration .leader .Context ;
35
35
import org .springframework .integration .leader .DefaultCandidate ;
36
36
import org .springframework .integration .leader .event .DefaultLeaderEventPublisher ;
37
37
import org .springframework .integration .leader .event .LeaderEventPublisher ;
38
38
import org .springframework .integration .support .locks .LockRegistry ;
39
- import org .springframework .scheduling .concurrent .CustomizableThreadFactory ;
40
39
import org .springframework .util .Assert ;
41
40
42
41
/**
@@ -67,9 +66,7 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe
67
66
68
67
public static final long DEFAULT_BUSY_WAIT_TIME = 50L ;
69
68
70
- private static final Log LOGGER = LogFactory .getLog (LockRegistryLeaderInitiator .class );
71
-
72
- private final Object lifecycleMonitor = new Object ();
69
+ private static final LogAccessor LOGGER = new LogAccessor (LockRegistryLeaderInitiator .class );
73
70
74
71
/**
75
72
* A lock registry. The locks it manages should be global (whatever that means for the
@@ -103,14 +100,7 @@ public String getRole() {
103
100
/**
104
101
* Executor service for running leadership daemon.
105
102
*/
106
- private ExecutorService executorService =
107
- Executors .newSingleThreadExecutor (new CustomizableThreadFactory ("lock-leadership-" ));
108
-
109
- /**
110
- * Flag to denote whether the {@link ExecutorService} was provided via the setter and
111
- * thus should not be shutdown when {@link #destroy()} is called.
112
- */
113
- private boolean executorServiceExplicitlySet ;
103
+ private AsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor ("lock-leadership-" );
114
104
115
105
/**
116
106
* Time in milliseconds to wait in between attempts to re-acquire the lock, once it is
@@ -161,7 +151,7 @@ public String getRole() {
161
151
162
152
/**
163
153
* Future returned by submitting an {@link LeaderSelector} to
164
- * {@link #executorService }. This is used to cancel leadership.
154
+ * {@link #taskExecutor }. This is used to cancel leadership.
165
155
*/
166
156
private volatile Future <?> future ;
167
157
@@ -192,10 +182,21 @@ public LockRegistryLeaderInitiator(LockRegistry locks, Candidate candidate) {
192
182
* single thread Executor will be used.
193
183
* @param executorService the executor service
194
184
* @since 5.0.2
185
+ * @deprecated since 6.2 in favor of {@link #setTaskExecutor(AsyncTaskExecutor)}
195
186
*/
187
+ @ Deprecated (since = "6.2" , forRemoval = true )
196
188
public void setExecutorService (ExecutorService executorService ) {
197
- this .executorService = executorService ;
198
- this .executorServiceExplicitlySet = true ;
189
+ setTaskExecutor (new TaskExecutorAdapter (executorService ));
190
+ }
191
+
192
+ /**
193
+ * Set a {@link AsyncTaskExecutor} for running leadership daemon.
194
+ * @param taskExecutor the {@link AsyncTaskExecutor} to use.
195
+ * @since 6.2
196
+ */
197
+ public void setTaskExecutor (AsyncTaskExecutor taskExecutor ) {
198
+ Assert .notNull (taskExecutor , "A 'taskExecutor' must not be null." );
199
+ this .taskExecutor = taskExecutor ;
199
200
}
200
201
201
202
public void setHeartBeatMillis (long heartBeatMillis ) {
@@ -224,9 +225,7 @@ public void setApplicationEventPublisher(ApplicationEventPublisher applicationEv
224
225
*/
225
226
@ Override
226
227
public boolean isRunning () {
227
- synchronized (this .lifecycleMonitor ) {
228
- return this .running ;
229
- }
228
+ return this .running ;
230
229
}
231
230
232
231
@ Override
@@ -287,43 +286,36 @@ public void setPublishFailedEvents(boolean publishFailedEvents) {
287
286
* Start the registration of the {@link #candidate} for leader election.
288
287
*/
289
288
@ Override
290
- public void start () {
289
+ public synchronized void start () {
291
290
if (this .leaderEventPublisher == null && this .applicationEventPublisher != null ) {
292
291
this .leaderEventPublisher = new DefaultLeaderEventPublisher (this .applicationEventPublisher );
293
292
}
294
- synchronized (this .lifecycleMonitor ) {
295
- if (!this .running ) {
296
- this .leaderSelector = new LeaderSelector (buildLeaderPath ());
297
- this .running = true ;
298
- this .future = this .executorService .submit (this .leaderSelector );
299
- LOGGER .debug ("Started LeaderInitiator" );
300
- }
293
+ if (!this .running ) {
294
+ this .leaderSelector = new LeaderSelector (buildLeaderPath ());
295
+ this .running = true ;
296
+ this .future = this .taskExecutor .submit (this .leaderSelector );
297
+ LOGGER .debug ("Started LeaderInitiator" );
301
298
}
302
299
}
303
300
304
301
@ Override
305
302
public void destroy () {
306
303
stop ();
307
- if (!this .executorServiceExplicitlySet ) {
308
- this .executorService .shutdown ();
309
- }
310
304
}
311
305
312
306
/**
313
307
* Stop the registration of the {@link #candidate} for leader election. If the
314
308
* candidate is currently leader, its leadership will be revoked.
315
309
*/
316
310
@ Override
317
- public void stop () {
318
- synchronized (this .lifecycleMonitor ) {
319
- if (this .running ) {
320
- this .running = false ;
321
- if (this .future != null ) {
322
- this .future .cancel (true );
323
- }
324
- this .future = null ;
325
- LOGGER .debug ("Stopped LeaderInitiator for " + getContext ());
311
+ public synchronized void stop () {
312
+ if (this .running ) {
313
+ this .running = false ;
314
+ if (this .future != null ) {
315
+ this .future .cancel (true );
326
316
}
317
+ this .future = null ;
318
+ LOGGER .debug (() -> "Stopped LeaderInitiator for " + getContext ());
327
319
}
328
320
}
329
321
@@ -382,9 +374,9 @@ public Void call() {
382
374
try {
383
375
this .lock .unlock ();
384
376
}
385
- catch (Exception e ) {
386
- LOGGER .debug ("Could not unlock during stop for " + this . context
387
- + " - treat as broken. Revoking..." , e );
377
+ catch (Exception ex ) {
378
+ LOGGER .debug (ex , () ->
379
+ "Could not unlock during stop for " + this . context + " - treat as broken. Revoking..." );
388
380
}
389
381
// We are stopping, therefore not leading anymore
390
382
handleRevoked ();
@@ -394,9 +386,7 @@ public Void call() {
394
386
}
395
387
396
388
private void tryAcquireLock () throws InterruptedException {
397
- if (LOGGER .isDebugEnabled ()) {
398
- LOGGER .debug ("Acquiring the lock for " + this .context );
399
- }
389
+ LOGGER .debug (() -> "Acquiring the lock for " + this .context );
400
390
// We always try to acquire the lock, in case it expired
401
391
boolean acquired =
402
392
this .lock .tryLock (LockRegistryLeaderInitiator .this .heartBeatMillis , TimeUnit .MILLISECONDS );
@@ -436,8 +426,8 @@ private boolean unlockAndHandleException(Exception ex) { // NOSONAR
436
426
this .lock .unlock ();
437
427
}
438
428
catch (Exception e1 ) {
439
- LOGGER .debug ("Could not unlock - treat as broken " + this .context +
440
- ". Revoking " + (isRunning () ? " and retrying..." : "..." ), e1 );
429
+ LOGGER .debug (e1 , () -> "Could not unlock - treat as broken " + this .context +
430
+ ". Revoking " + (isRunning () ? " and retrying..." : "..." ));
441
431
442
432
}
443
433
// The lock was broken and we are no longer leader
@@ -462,18 +452,16 @@ private boolean unlockAndHandleException(Exception ex) { // NOSONAR
462
452
Thread .currentThread ().interrupt ();
463
453
}
464
454
}
465
- if (LOGGER .isDebugEnabled ()) {
466
- LOGGER .debug ("Error acquiring the lock for " + this .context +
467
- ". " + (isRunning () ? "Retrying..." : "" ), ex );
468
- }
455
+ LOGGER .debug (ex , () ->
456
+ "Error acquiring the lock for " + this .context + ". " + (isRunning () ? "Retrying..." : "" ));
469
457
}
470
458
return false ;
471
459
}
472
460
473
461
private void restartSelectorBecauseOfError (Exception ex ) {
474
- LOGGER .warn ("Restarting LeaderSelector for " + this .context + " because of error." , ex );
462
+ LOGGER .warn (ex , () -> "Restarting LeaderSelector for " + this .context + " because of error." );
475
463
LockRegistryLeaderInitiator .this .future =
476
- LockRegistryLeaderInitiator .this .executorService .submit (
464
+ LockRegistryLeaderInitiator .this .taskExecutor .submit (
477
465
() -> {
478
466
// Give it a chance to elect some other leader.
479
467
Thread .sleep (LockRegistryLeaderInitiator .this .busyWaitMillis );
@@ -492,8 +480,8 @@ private void handleGranted() throws InterruptedException {
492
480
LockRegistryLeaderInitiator .this .leaderEventPublisher .publishOnGranted (
493
481
LockRegistryLeaderInitiator .this , this .context , this .lockKey );
494
482
}
495
- catch (Exception e ) {
496
- LOGGER .warn ("Error publishing OnGranted event." , e );
483
+ catch (Exception ex ) {
484
+ LOGGER .warn (ex , "Error publishing OnGranted event." );
497
485
}
498
486
}
499
487
}
@@ -506,8 +494,8 @@ private void handleRevoked() {
506
494
LockRegistryLeaderInitiator .this , this .context ,
507
495
LockRegistryLeaderInitiator .this .candidate .getRole ());
508
496
}
509
- catch (Exception e ) {
510
- LOGGER .warn ("Error publishing OnRevoked event." , e );
497
+ catch (Exception ex ) {
498
+ LOGGER .warn (ex , "Error publishing OnRevoked event." );
511
499
}
512
500
}
513
501
}
@@ -520,8 +508,8 @@ private void publishFailedToAcquire() {
520
508
this .context ,
521
509
LockRegistryLeaderInitiator .this .candidate .getRole ());
522
510
}
523
- catch (Exception e ) {
524
- LOGGER .warn ("Error publishing OnFailedToAcquire event." , e );
511
+ catch (Exception ex ) {
512
+ LOGGER .warn (ex , "Error publishing OnFailedToAcquire event." );
525
513
}
526
514
}
527
515
}
@@ -543,9 +531,7 @@ public boolean isLeader() {
543
531
544
532
@ Override
545
533
public void yield () {
546
- if (LOGGER .isDebugEnabled ()) {
547
- LOGGER .debug ("Yielding leadership from " + this );
548
- }
534
+ LOGGER .debug (() -> "Yielding leadership from " + this );
549
535
LockRegistryLeaderInitiator .this .leaderSelector .yielding = true ;
550
536
}
551
537
0 commit comments