View Javadoc

1   package org.apache.helix.examples;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  }