1 package org.apache.helix.examples;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.helix.NotificationContext;
23 import org.apache.helix.model.Message;
24 import org.apache.helix.participant.statemachine.StateModel;
25 import org.apache.helix.participant.statemachine.StateModelFactory;
26
27 public class LeaderStandbyStateModelFactory extends
28 StateModelFactory<StateModel> {
29 int _delay;
30
31 public LeaderStandbyStateModelFactory(int delay) {
32 _delay = delay;
33 }
34
35 @Override
36 public StateModel createNewStateModel(String stateUnitKey) {
37 LeaderStandbyStateModel stateModel = new LeaderStandbyStateModel();
38 stateModel.setDelay(_delay);
39 return stateModel;
40 }
41
42 public static class LeaderStandbyStateModel extends StateModel {
43 int _transDelay = 0;
44
45 public void setDelay(int delay) {
46 _transDelay = delay > 0 ? delay : 0;
47 }
48
49 public void onBecomeLeaderFromStandby(Message message,
50 NotificationContext context) {
51 System.out
52 .println("LeaderStandbyStateModel.onBecomeLeaderFromStandby()");
53 sleep();
54 }
55
56 public void onBecomeStandbyFromLeader(Message message,
57 NotificationContext context) {
58 System.out
59 .println("LeaderStandbyStateModel.onBecomeStandbyFromLeader()");
60 sleep();
61 }
62
63 private void sleep() {
64 try {
65 Thread.sleep(_transDelay);
66 } catch (Exception e) {
67 e.printStackTrace();
68 }
69 }
70 }
71 }