KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ha > singleton > HASingletonElectionPolicySimple


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.ha.singleton;
23
24
25 import org.jboss.ha.framework.interfaces.ClusterNode;
26 import org.jboss.ha.framework.interfaces.HAPartition;
27
28 /**
29  * A simple concrete policy service that decides which node in the cluster should be
30  * the master node to run certain HASingleton service based on attribute "Position".
31  * The value will be divided by partition size and only remainder will be used.
32  *
33  * Let's say partition size is n:
34  * 0 means the first oldest node.
35  * 1 means the 2nd oldest node.
36  * ...
37  * n-1 means the nth oldest node.
38  *
39  * -1 means the youngest node.
40  * -2 means the 2nd youngest node.
41  * ...
42  * -n means the nth youngest node.
43  *
44  * E.g. the following attribute says the singleton will be running on the 3rd oldest node of
45  * the current partition:
46  * <attribute name="Position">2</attribute>
47  *
48  * @author <a HREF="mailto:Alex.Fu@novell.com">Alex Fu</a>
49  * @version $Revision: 46010 $
50  */

51 public class HASingletonElectionPolicySimple
52    extends HASingletonElectionPolicyBase
53    implements HASingletonElectionPolicySimpleMBean
54 {
55    // Attributes
56
private int mPosition = 0; // Default
57

58    /**
59     * @see HASingletonElectionPolicySimpleMBean#setPosition(int)
60     */

61    public void setPosition(int pos)
62    {
63       this.mPosition = pos;
64    }
65
66    /**
67     * @see HASingletonElectionPolicySimpleMBean#getPosition()
68     */

69    public int getPosition()
70    {
71       return this.mPosition;
72    }
73
74    public ClusterNode pickSingleton()
75    {
76       return pickSingleton(getHAPartition());
77    }
78    
79    public ClusterNode pickSingleton(HAPartition partition)
80    {
81       ClusterNode[] nodes = partition.getClusterNodes();
82       
83       int size = nodes.length;
84       int remainder = ((this.mPosition % size) + size) % size;
85       
86       return nodes[remainder];
87    }
88 }
89
Popular Tags