Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » transport » discovery » rendezvous » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.transport.discovery.rendezvous;
   18   
   19   import java.io.IOException;
   20   import java.net.InetAddress;
   21   import java.net.UnknownHostException;
   22   import java.util.HashMap;
   23   import java.util.Iterator;
   24   import java.util.Map;
   25   import java.util.concurrent.CopyOnWriteArrayList;
   26   
   27   import org.apache.activemq.jmdns.JmDNS;
   28   import org.apache.activemq.jmdns.ServiceEvent;
   29   import org.apache.activemq.jmdns.ServiceInfo;
   30   import org.apache.activemq.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   /**
   41    * A {@link DiscoveryAgent} using <a href="http://www.zeroconf.org/">Zeroconf</a>
   42    * via the <a href="http://jmdns.sf.net/">jmDNS</a> library
   43    * 
   44    * @version $Revision$
   45    */
   46   public class RendezvousDiscoveryAgent implements DiscoveryAgent, ServiceListener {
   47       private static final Log LOG = LogFactory.getLog(RendezvousDiscoveryAgent.class);
   48   
   49       private static final String TYPE_SUFFIX = "ActiveMQ-4.";
   50   
   51       private JmDNS jmdns;
   52       private InetAddress localAddress;
   53       private String localhost;
   54       private int weight;
   55       private int priority;
   56   
   57       private DiscoveryListener listener;
   58       private String group = "default";
   59       private final CopyOnWriteArrayList<ServiceInfo> serviceInfos = new CopyOnWriteArrayList<ServiceInfo>();
   60   
   61       // DiscoveryAgent interface
   62       // -------------------------------------------------------------------------
   63       public void start() throws Exception {
   64           if (group == null) {
   65               throw new IOException("You must specify a group to discover");
   66           }
   67           String type = getType();
   68           if (!type.endsWith(".")) {
   69               LOG.warn("The type '" + type + "' should end with '.' to be a valid Rendezvous type");
   70               type += ".";
   71           }
   72           try {
   73               // force lazy construction
   74               getJmdns();
   75               if (listener != null) {
   76                   LOG.info("Discovering service of type: " + type);
   77                   jmdns.addServiceListener(type, this);
   78               }
   79           } catch (IOException e) {
   80               JMSExceptionSupport.create("Failed to start JmDNS service: " + e, e);
   81           }
   82       }
   83   
   84       public void stop() {
   85           if (jmdns != null) {
   86               for (Iterator<ServiceInfo> iter = serviceInfos.iterator(); iter.hasNext();) {
   87                   ServiceInfo si = iter.next();
   88                   jmdns.unregisterService(si);
   89               }
   90   
   91               // Close it down async since this could block for a while.
   92               final JmDNS closeTarget = jmdns;
   93               Thread thread = new Thread() {
   94                   public void run() {
   95                       closeTarget.close();
   96                   }
   97               };
   98   
   99               thread.setDaemon(true);
  100               thread.start();
  101   
  102               jmdns = null;
  103           }
  104       }
  105   
  106       public void registerService(String name) throws IOException {
  107           ServiceInfo si = createServiceInfo(name, new HashMap());
  108           serviceInfos.add(si);
  109           getJmdns().registerService(si);
  110       }
  111   
  112       // ServiceListener interface
  113       // -------------------------------------------------------------------------
  114       public void addService(JmDNS jmDNS, String type, String name) {
  115           if (LOG.isDebugEnabled()) {
  116               LOG.debug("addService with type: " + type + " name: " + name);
  117           }
  118           if (listener != null) {
  119               listener.onServiceAdd(new DiscoveryEvent(name));
  120           }
  121           jmDNS.requestServiceInfo(type, name);
  122       }
  123   
  124       public void removeService(JmDNS jmDNS, String type, String name) {
  125           if (LOG.isDebugEnabled()) {
  126               LOG.debug("removeService with type: " + type + " name: " + name);
  127           }
  128           if (listener != null) {
  129               listener.onServiceRemove(new DiscoveryEvent(name));
  130           }
  131       }
  132   
  133       public void serviceAdded(ServiceEvent event) {
  134           addService(event.getDNS(), event.getType(), event.getName());
  135       }
  136   
  137       public void serviceRemoved(ServiceEvent event) {
  138           removeService(event.getDNS(), event.getType(), event.getName());
  139       }
  140   
  141       public void serviceResolved(ServiceEvent event) {
  142       }
  143   
  144       public void resolveService(JmDNS jmDNS, String type, String name, ServiceInfo serviceInfo) {
  145       }
  146   
  147       public int getPriority() {
  148           return priority;
  149       }
  150   
  151       public void setPriority(int priority) {
  152           this.priority = priority;
  153       }
  154   
  155       public int getWeight() {
  156           return weight;
  157       }
  158   
  159       public void setWeight(int weight) {
  160           this.weight = weight;
  161       }
  162   
  163       public JmDNS getJmdns() throws IOException {
  164           if (jmdns == null) {
  165               jmdns = createJmDNS();
  166           }
  167           return jmdns;
  168       }
  169   
  170       public void setJmdns(JmDNS jmdns) {
  171           this.jmdns = jmdns;
  172       }
  173   
  174       public InetAddress getLocalAddress() throws UnknownHostException {
  175           if (localAddress == null) {
  176               localAddress = createLocalAddress();
  177           }
  178           return localAddress;
  179       }
  180   
  181       public void setLocalAddress(InetAddress localAddress) {
  182           this.localAddress = localAddress;
  183       }
  184   
  185       public String getLocalhost() {
  186           return localhost;
  187       }
  188   
  189       public void setLocalhost(String localhost) {
  190           this.localhost = localhost;
  191       }
  192   
  193       // Implementation methods
  194       // -------------------------------------------------------------------------
  195       protected ServiceInfo createServiceInfo(String name, Map map) {
  196           int port = MapHelper.getInt(map, "port", 0);
  197   
  198           String type = getType();
  199   
  200           if (LOG.isDebugEnabled()) {
  201               LOG.debug("Registering service type: " + type + " name: " + name + " details: " + map);
  202           }
  203           return new ServiceInfo(type, name + "." + type, port, weight, priority, "");
  204       }
  205   
  206       protected JmDNS createJmDNS() throws IOException {
  207           return JmDNSFactory.create(getLocalAddress());
  208       }
  209   
  210       protected InetAddress createLocalAddress() throws UnknownHostException {
  211           if (localhost != null) {
  212               return InetAddress.getByName(localhost);
  213           }
  214           return InetAddress.getLocalHost();
  215       }
  216   
  217       public void setDiscoveryListener(DiscoveryListener listener) {
  218           this.listener = listener;
  219       }
  220   
  221       public String getGroup() {
  222           return group;
  223       }
  224   
  225       public void setGroup(String group) {
  226           this.group = group;
  227       }
  228   
  229       public String getType() {
  230           return "_" + group + "." + TYPE_SUFFIX;
  231       }
  232   
  233       public void serviceFailed(DiscoveryEvent event) throws IOException {
  234           // TODO: is there a way to notify the JmDNS that the service failed?
  235       }
  236   
  237   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » transport » discovery » rendezvous » [javadoc | source]