16
16
17
17
package org .springframework .integration .zookeeper .leader ;
18
18
19
+ import java .util .Collection ;
20
+ import java .util .List ;
21
+
19
22
import org .apache .commons .logging .Log ;
20
23
import org .apache .commons .logging .LogFactory ;
21
24
import org .apache .curator .framework .CuratorFramework ;
22
25
import org .apache .curator .framework .imps .CuratorFrameworkState ;
23
26
import org .apache .curator .framework .recipes .leader .LeaderSelector ;
24
27
import org .apache .curator .framework .recipes .leader .LeaderSelectorListenerAdapter ;
28
+ import org .apache .curator .framework .recipes .leader .Participant ;
25
29
26
30
import org .springframework .context .SmartLifecycle ;
27
31
import org .springframework .integration .leader .Candidate ;
38
42
* @author Janne Valkealahti
39
43
* @author Gary Russell
40
44
* @author Artem Bilan
45
+ * @author Ivan Zaitsev
41
46
*
42
47
* @since 4.2
43
48
*/
@@ -49,6 +54,8 @@ public class LeaderInitiator implements SmartLifecycle {
49
54
50
55
private final CuratorContext context = new CuratorContext ();
51
56
57
+ private final CuratorContext nullContext = new NullCuratorContext ();
58
+
52
59
/**
53
60
* Curator client.
54
61
*/
@@ -59,20 +66,6 @@ public class LeaderInitiator implements SmartLifecycle {
59
66
*/
60
67
private final Candidate candidate ;
61
68
62
- private final Context nullContext = new Context () {
63
-
64
- @ Override
65
- public boolean isLeader () {
66
- return false ;
67
- }
68
-
69
- @ Override
70
- public String getRole () {
71
- return LeaderInitiator .this .candidate .getRole ();
72
- }
73
-
74
- };
75
-
76
69
private final Object lifecycleMonitor = new Object ();
77
70
78
71
/**
@@ -214,7 +207,7 @@ public void setLeaderEventPublisher(LeaderEventPublisher leaderEventPublisher) {
214
207
* @return the context.
215
208
* @since 5.0
216
209
*/
217
- public Context getContext () {
210
+ public CuratorContext getContext () {
218
211
if (this .leaderSelector == null ) {
219
212
return this .nullContext ;
220
213
}
@@ -283,7 +276,7 @@ public void takeLeadership(CuratorFramework framework) {
283
276
/**
284
277
* Implementation of leadership context backed by Curator.
285
278
*/
286
- private class CuratorContext implements Context {
279
+ public class CuratorContext implements Context {
287
280
288
281
CuratorContext () {
289
282
}
@@ -303,6 +296,24 @@ public String getRole() {
303
296
return LeaderInitiator .this .candidate .getRole ();
304
297
}
305
298
299
+ /**
300
+ * Get the leader
301
+ * @return the leader.
302
+ * @since 6.0.3
303
+ */
304
+ public Participant getLeader () throws Exception {
305
+ return LeaderInitiator .this .leaderSelector .getLeader ();
306
+ }
307
+
308
+ /**
309
+ * Get the list of participants
310
+ * @return list of participants.
311
+ * @since 6.0.3
312
+ */
313
+ public Collection <Participant > getParticipants () throws Exception {
314
+ return LeaderInitiator .this .leaderSelector .getParticipants ();
315
+ }
316
+
306
317
@ Override
307
318
public String toString () {
308
319
return "CuratorContext{role=" + LeaderInitiator .this .candidate .getRole () +
@@ -312,4 +323,28 @@ public String toString() {
312
323
313
324
}
314
325
326
+ private class NullCuratorContext extends CuratorContext {
327
+
328
+ @ Override
329
+ public boolean isLeader () {
330
+ return false ;
331
+ }
332
+
333
+ @ Override
334
+ public String getRole () {
335
+ return LeaderInitiator .this .candidate .getRole ();
336
+ }
337
+
338
+ @ Override
339
+ public Participant getLeader () throws Exception {
340
+ return null ;
341
+ }
342
+
343
+ @ Override
344
+ public Collection <Participant > getParticipants () throws Exception {
345
+ return List .of ();
346
+ }
347
+
348
+ }
349
+
315
350
}
0 commit comments