1 18 package org.apache.activemq.transport.discovery.rendezvous; 19 20 import java.io.IOException ; 21 import java.net.InetAddress ; 22 import java.net.UnknownHostException ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 27 import javax.jmdns.JmDNS; 28 import javax.jmdns.ServiceEvent; 29 import javax.jmdns.ServiceInfo; 30 import javax.jmdns.ServiceListener; 31 32 import org.apache.activemq.command.DiscoveryEvent; 33 import org.apache.activemq.transport.discovery.DiscoveryAgent; 34 import org.apache.activemq.transport.discovery.DiscoveryListener; 35 import org.apache.activemq.util.JMSExceptionSupport; 36 import org.apache.activemq.util.MapHelper; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 import java.util.concurrent.CopyOnWriteArrayList ; 41 42 48 public class RendezvousDiscoveryAgent implements DiscoveryAgent, ServiceListener { 49 private static final Log log = LogFactory.getLog(RendezvousDiscoveryAgent.class); 50 51 private static final String TYPE_SUFFIX = "ActiveMQ-4."; 52 53 private JmDNS jmdns; 54 private InetAddress localAddress; 55 private String localhost; 56 private int weight = 0; 57 private int priority = 0; 58 59 private DiscoveryListener listener; 60 private String group = "default"; 61 private final CopyOnWriteArrayList serviceInfos = new CopyOnWriteArrayList (); 62 63 private String brokerName; 64 65 public void start() throws Exception { 68 if (group == null) { 69 throw new IOException ("You must specify a group to discover"); 70 } 71 String type = getType(); 72 if (!type.endsWith(".")) { 73 log.warn("The type '" + type + "' should end with '.' to be a valid Rendezvous type"); 74 type += "."; 75 } 76 try { 77 getJmdns(); 79 if (listener!=null) { 80 log.info("Discovering service of type: " +type); 81 jmdns.addServiceListener(type, this); 82 } 83 } 84 catch (IOException e) { 85 JMSExceptionSupport.create("Failed to start JmDNS service: " + e, e); 86 } 87 } 88 89 public void stop() { 90 if( jmdns!=null ) { 91 for (Iterator iter = serviceInfos.iterator(); iter.hasNext();) { 92 ServiceInfo si = (ServiceInfo) iter.next(); 93 jmdns.unregisterService(si); 94 } 95 96 final JmDNS closeTarget = jmdns; 98 Thread thread = new Thread () { 99 public void run() { 100 closeTarget.close(); 101 } 102 }; 103 104 thread.setDaemon(true); 105 thread.start(); 106 107 jmdns=null; 108 } 109 } 110 111 public void registerService(String name) throws IOException { 112 ServiceInfo si = createServiceInfo(name, new HashMap ()); 113 serviceInfos.add(si); 114 getJmdns().registerService(si); 115 } 116 117 118 public void addService(JmDNS jmDNS, String type, String name) { 121 if (log.isDebugEnabled()) { 122 log.debug("addService with type: " + type + " name: " + name); 123 } 124 if( listener!=null ) 125 listener.onServiceAdd(new DiscoveryEvent(name)); 126 jmDNS.requestServiceInfo(type, name); 127 } 128 129 public void removeService(JmDNS jmDNS, String type, String name) { 130 if (log.isDebugEnabled()) { 131 log.debug("removeService with type: " + type + " name: " + name); 132 } 133 if( listener!=null ) 134 listener.onServiceRemove(new DiscoveryEvent(name)); 135 } 136 137 public void serviceAdded(ServiceEvent event) { 138 addService(event.getDNS(), event.getType(), event.getName()); 139 } 140 public void serviceRemoved(ServiceEvent event) { 141 removeService(event.getDNS(), event.getType(), event.getName()); 142 } 143 public void serviceResolved(ServiceEvent event) { 144 } 145 public void resolveService(JmDNS jmDNS, String type, String name, ServiceInfo serviceInfo) { 146 } 147 148 public int getPriority() { 149 return priority; 150 } 151 152 public void setPriority(int priority) { 153 this.priority = priority; 154 } 155 156 public int getWeight() { 157 return weight; 158 } 159 160 public void setWeight(int weight) { 161 this.weight = weight; 162 } 163 164 public JmDNS getJmdns() throws IOException { 165 if (jmdns == null) { 166 jmdns = createJmDNS(); 167 } 168 return jmdns; 169 } 170 171 public void setJmdns(JmDNS jmdns) { 172 this.jmdns = jmdns; 173 } 174 175 176 public InetAddress getLocalAddress() throws UnknownHostException { 177 if (localAddress == null) { 178 localAddress = createLocalAddress(); 179 } 180 return localAddress; 181 } 182 183 public void setLocalAddress(InetAddress localAddress) { 184 this.localAddress = localAddress; 185 } 186 187 public String getLocalhost() { 188 return localhost; 189 } 190 191 public void setLocalhost(String localhost) { 192 this.localhost = localhost; 193 } 194 195 protected ServiceInfo createServiceInfo(String name, Map map) { 198 int port = MapHelper.getInt(map, "port", 0); 199 200 String type = getType(); 201 202 if (log.isDebugEnabled()) { 203 log.debug("Registering service type: " + type + " name: " + name + " details: " + map); 204 } 205 return new ServiceInfo(type, name+"."+type, port, weight, priority, ""); 206 } 207 208 protected JmDNS createJmDNS() throws IOException { 209 return JmDNSFactory.create(getLocalAddress()); 210 } 211 212 protected InetAddress createLocalAddress() throws UnknownHostException { 213 if (localhost != null) { 214 return InetAddress.getByName(localhost); 215 } 216 return InetAddress.getLocalHost(); 217 } 218 219 public void setDiscoveryListener(DiscoveryListener listener) { 220 this.listener = listener; 221 } 222 223 public String getGroup() { 224 return group; 225 } 226 227 public void setGroup(String group) { 228 this.group=group; 229 } 230 231 public String getType() { 232 return "_" + group+"."+TYPE_SUFFIX; 233 } 234 235 public void setBrokerName(String brokerName) { 236 this.brokerName = brokerName; 237 } 238 239 public void serviceFailed(DiscoveryEvent event) throws IOException { 240 } 242 } 243 | Popular Tags |