KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > cluster > mcast > McastService


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.catalina.cluster.mcast;
18
19 import org.apache.catalina.cluster.MembershipService;
20 import java.util.Properties JavaDoc;
21 import org.apache.catalina.cluster.Member;
22 import org.apache.catalina.cluster.MembershipListener;
23
24 /**
25  * A <b>membership</b> implementation using simple multicast.
26  * This is the representation of a multicast membership service.
27  * This class is responsible for maintaining a list of active cluster nodes in the cluster.
28  * If a node fails to send out a heartbeat, the node will be dismissed.
29  *
30  * @author Filip Hanik
31  * @version $Revision: 1.13 $, $Date: 2004/11/11 14:47:26 $
32  */

33
34
35 public class McastService implements MembershipService,MembershipListener {
36
37     private static org.apache.commons.logging.Log log =
38         org.apache.commons.logging.LogFactory.getLog( McastService.class );
39     /**
40      * The implementation specific properties
41      */

42     protected Properties JavaDoc properties = new Properties JavaDoc();
43     /**
44      * A handle to the actual low level implementation
45      */

46     protected McastServiceImpl impl;
47     /**
48      * A membership listener delegate (should be the cluster :)
49      */

50     protected MembershipListener listener;
51     /**
52      * The local member
53      */

54     protected McastMember localMember ;
55     private int mcastSoTimeout;
56     private int mcastTTL;
57
58     /**
59      * Create a membership service.
60      */

61     public McastService() {
62     }
63
64
65     /**
66      *
67      * @param properties<BR>All are required<BR>
68      * 1. mcastPort - the port to listen to<BR>
69      * 2. mcastAddress - the mcast group address<BR>
70      * 3. bindAddress - the bind address if any - only one that can be null<BR>
71      * 4. memberDropTime - the time a member is gone before it is considered gone.<BR>
72      * 5. msgFrequency - the frequency of sending messages<BR>
73      * 6. tcpListenPort - the port this member listens to<BR>
74      * 7. tcpListenHost - the bind address of this member<BR>
75      * @exception java.lang.IllegalArgumentException if a property is missing.
76      */

77     public void setProperties(Properties JavaDoc properties) {
78         hasProperty(properties,"mcastPort");
79         hasProperty(properties,"mcastAddress");
80         hasProperty(properties,"memberDropTime");
81         hasProperty(properties,"msgFrequency");
82         hasProperty(properties,"tcpListenPort");
83         hasProperty(properties,"tcpListenHost");
84         this.properties = properties;
85     }
86
87     /**
88      * Return the properties, see setProperties
89      */

90     public Properties JavaDoc getProperties() {
91         return properties;
92     }
93
94     /**
95      * Return the local member
96      */

97     public Member getLocalMember() {
98         localMember.setMemberAliveTime(System.currentTimeMillis()-impl.getServiceStartTime());
99         return localMember;
100     }
101     
102     /**
103      * Sets the local member properties for broadcasting
104      */

105     public void setLocalMemberProperties(String JavaDoc listenHost, int listenPort) {
106         properties.setProperty("tcpListenHost",listenHost);
107         properties.setProperty("tcpListenPort",String.valueOf(listenPort));
108     }
109     
110     public void setMcastAddr(String JavaDoc addr) {
111         properties.setProperty("mcastAddress", addr);
112     }
113
114     public String JavaDoc getMcastAddr() {
115         return properties.getProperty("mcastAddress");
116     }
117
118     public void setMcastBindAddress(String JavaDoc bindaddr) {
119         properties.setProperty("mcastBindAddress", bindaddr);
120     }
121
122     public String JavaDoc getMcastBindAddress() {
123         return properties.getProperty("mcastBindAddress");
124     }
125
126     public void setMcastPort(int port) {
127         properties.setProperty("mcastPort", String.valueOf(port));
128     }
129
130     public int getMcastPort() {
131         String JavaDoc p = properties.getProperty("mcastPort");
132         return new Integer JavaDoc(p).intValue();
133     }
134     
135     public void setMcastFrequency(long time) {
136         properties.setProperty("msgFrequency", String.valueOf(time));
137     }
138
139     public long getMcastFrequency() {
140         String JavaDoc p = properties.getProperty("msgFrequency");
141         return new Long JavaDoc(p).longValue();
142     }
143
144     public void setMcastDropTime(long time) {
145         properties.setProperty("memberDropTime", String.valueOf(time));
146     }
147
148     public long getMcastDropTime() {
149         String JavaDoc p = properties.getProperty("memberDropTime");
150         return new Long JavaDoc(p).longValue();
151     }
152
153     /**
154      * Check if a required property is available.
155      * @param properties The set of properties
156      * @param name The property to check for
157      */

158     protected void hasProperty(Properties JavaDoc properties, String JavaDoc name){
159         if ( properties.getProperty(name)==null) throw new IllegalArgumentException JavaDoc("Required property \""+name+"\" is missing.");
160     }
161
162     /**
163      * Start broadcasting and listening to membership pings
164      * @throws java.lang.Exception if a IO error occurs
165      */

166     public void start() throws java.lang.Exception JavaDoc {
167         start(1);
168         start(2);
169     }
170     
171     public void start(int level) throws java.lang.Exception JavaDoc {
172         if ( impl != null ) {
173             impl.start(level);
174             return;
175         }
176         String JavaDoc host = getProperties().getProperty("tcpListenHost");
177         int port = Integer.parseInt(getProperties().getProperty("tcpListenPort"));
178         String JavaDoc name = "tcp://"+host+":"+port;
179         if ( localMember == null ) {
180             localMember = new McastMember(name, host, port, 100);
181         } else {
182             localMember.setName(name);
183             localMember.setHost(host);
184             localMember.setPort(port);
185             localMember.setMemberAliveTime(100);
186         }
187         java.net.InetAddress JavaDoc bind = null;
188         if ( properties.getProperty("mcastBindAddress")!= null ) {
189             bind = java.net.InetAddress.getByName(properties.getProperty("mcastBindAddress"));
190         }
191         int ttl = -1;
192         int soTimeout = -1;
193         if ( properties.getProperty("mcastTTL") != null ) {
194             try {
195                 ttl = Integer.parseInt(properties.getProperty("mcastTTL"));
196             } catch ( Exception JavaDoc x ) {
197                 log.error("Unable to parse mcastTTL="+properties.getProperty("mcastTTL"),x);
198             }
199         }
200         if ( properties.getProperty("mcastSoTimeout") != null ) {
201             try {
202                 soTimeout = Integer.parseInt(properties.getProperty("mcastSoTimeout"));
203             } catch ( Exception JavaDoc x ) {
204                 log.error("Unable to parse mcastSoTimeout="+properties.getProperty("mcastSoTimeout"),x);
205             }
206         }
207
208         impl = new McastServiceImpl((McastMember)localMember,Long.parseLong(properties.getProperty("msgFrequency")),
209                                     Long.parseLong(properties.getProperty("memberDropTime")),
210                                     Integer.parseInt(properties.getProperty("mcastPort")),
211                                     bind,
212                                     java.net.InetAddress.getByName(properties.getProperty("mcastAddress")),
213                                     ttl,
214                                     soTimeout,
215                                     this);
216
217         impl.start(level);
218         if(log.isInfoEnabled())
219             log.info("Sleeping for "+(Long.parseLong(properties.getProperty("msgFrequency"))*4)+" secs to establish cluster membership");
220         Thread.sleep((Long.parseLong(properties.getProperty("msgFrequency"))*4));
221
222     }
223
224     /**
225      * Stop broadcasting and listening to membership pings
226      */

227     public void stop() {
228         try {
229             if ( impl != null) impl.stop();
230         } catch ( Exception JavaDoc x) {
231             log.error("Unable to stop the mcast service.",x);
232         }
233         impl = null;
234     }
235
236     /**
237      * Return all the members
238      */

239     public Member[] getMembers() {
240         if ( impl == null || impl.membership == null ) return null;
241         return impl.membership.getMembers();
242     }
243     /**
244      * Add a membership listener, this version only supports one listener per service,
245      * so calling this method twice will result in only the second listener being active.
246      * @param listener The listener
247      */

248     public void addMembershipListener(MembershipListener listener) {
249         this.listener = listener;
250     }
251     /**
252      * Remove the membership listener
253      */

254     public void removeMembershipListener(){
255         listener = null;
256     }
257
258     public void memberAdded(Member member) {
259         if ( listener!=null ) listener.memberAdded(member);
260     }
261
262     /**
263      * Callback from the impl when a new member has been received
264      * @param member The member
265      */

266     public void memberDisappeared(Member member)
267     {
268         if ( listener!=null ) listener.memberDisappeared(member);
269     }
270
271     public int getMcastSoTimeout() {
272         return mcastSoTimeout;
273     }
274     public void setMcastSoTimeout(int mcastSoTimeout) {
275         this.mcastSoTimeout = mcastSoTimeout;
276         properties.setProperty("mcastSoTimeout", String.valueOf(mcastSoTimeout));
277     }
278     public int getMcastTTL() {
279         return mcastTTL;
280     }
281     public void setMcastTTL(int mcastTTL) {
282         this.mcastTTL = mcastTTL;
283         properties.setProperty("mcastTTL", String.valueOf(mcastTTL));
284     }
285
286     /**
287      * Simple test program
288      * @param args Command-line arguments
289      * @throws Exception If an error occurs
290      */

291     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
292         if(log.isInfoEnabled())
293             log.info("Usage McastService hostname tcpport");
294         McastService service = new McastService();
295         java.util.Properties JavaDoc p = new java.util.Properties JavaDoc();
296         p.setProperty("mcastPort","5555");
297         p.setProperty("mcastAddress","224.10.10.10");
298         p.setProperty("bindAddress","localhost");
299         p.setProperty("memberDropTime","3000");
300         p.setProperty("msgFrequency","500");
301         p.setProperty("tcpListenPort",args[1]);
302         p.setProperty("tcpListenHost",args[0]);
303         service.setProperties(p);
304         service.start();
305         Thread.sleep(60*1000*60);
306     }
307 }
308
Popular Tags