1 16 17 package org.apache.catalina.cluster.mcast; 18 19 import org.apache.catalina.cluster.MembershipService; 20 import java.util.Properties ; 21 import org.apache.catalina.cluster.Member; 22 import org.apache.catalina.cluster.MembershipListener; 23 24 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 42 protected Properties properties = new Properties (); 43 46 protected McastServiceImpl impl; 47 50 protected MembershipListener listener; 51 54 protected McastMember localMember ; 55 private int mcastSoTimeout; 56 private int mcastTTL; 57 58 61 public McastService() { 62 } 63 64 65 77 public void setProperties(Properties 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 90 public Properties getProperties() { 91 return properties; 92 } 93 94 97 public Member getLocalMember() { 98 localMember.setMemberAliveTime(System.currentTimeMillis()-impl.getServiceStartTime()); 99 return localMember; 100 } 101 102 105 public void setLocalMemberProperties(String listenHost, int listenPort) { 106 properties.setProperty("tcpListenHost",listenHost); 107 properties.setProperty("tcpListenPort",String.valueOf(listenPort)); 108 } 109 110 public void setMcastAddr(String addr) { 111 properties.setProperty("mcastAddress", addr); 112 } 113 114 public String getMcastAddr() { 115 return properties.getProperty("mcastAddress"); 116 } 117 118 public void setMcastBindAddress(String bindaddr) { 119 properties.setProperty("mcastBindAddress", bindaddr); 120 } 121 122 public String 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 p = properties.getProperty("mcastPort"); 132 return new Integer (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 p = properties.getProperty("msgFrequency"); 141 return new Long (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 p = properties.getProperty("memberDropTime"); 150 return new Long (p).longValue(); 151 } 152 153 158 protected void hasProperty(Properties properties, String name){ 159 if ( properties.getProperty(name)==null) throw new IllegalArgumentException ("Required property \""+name+"\" is missing."); 160 } 161 162 166 public void start() throws java.lang.Exception { 167 start(1); 168 start(2); 169 } 170 171 public void start(int level) throws java.lang.Exception { 172 if ( impl != null ) { 173 impl.start(level); 174 return; 175 } 176 String host = getProperties().getProperty("tcpListenHost"); 177 int port = Integer.parseInt(getProperties().getProperty("tcpListenPort")); 178 String 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 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 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 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 227 public void stop() { 228 try { 229 if ( impl != null) impl.stop(); 230 } catch ( Exception x) { 231 log.error("Unable to stop the mcast service.",x); 232 } 233 impl = null; 234 } 235 236 239 public Member[] getMembers() { 240 if ( impl == null || impl.membership == null ) return null; 241 return impl.membership.getMembers(); 242 } 243 248 public void addMembershipListener(MembershipListener listener) { 249 this.listener = listener; 250 } 251 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 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 291 public static void main(String args[]) throws Exception { 292 if(log.isInfoEnabled()) 293 log.info("Usage McastService hostname tcpport"); 294 McastService service = new McastService(); 295 java.util.Properties p = new java.util.Properties (); 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 |