Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache » activemq » transport » discovery » [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;
   18   
   19   import java.net.URI;
   20   import java.net.URISyntaxException;
   21   import java.util.Map;
   22   import java.util.concurrent.ConcurrentHashMap;
   23   
   24   import org.apache.activemq.command.DiscoveryEvent;
   25   import org.apache.activemq.transport.CompositeTransport;
   26   import org.apache.activemq.transport.TransportFilter;
   27   import org.apache.activemq.util.ServiceStopper;
   28   import org.apache.activemq.util.URISupport;
   29   import org.apache.commons.logging.Log;
   30   import org.apache.commons.logging.LogFactory;
   31   
   32   /**
   33    * A {@link ReliableTransportChannel} which uses a {@link DiscoveryAgent} to
   34    * discover remote broker instances and dynamically connect to them.
   35    * 
   36    * @version $Revision$
   37    */
   38   public class DiscoveryTransport extends TransportFilter implements DiscoveryListener {
   39   
   40       private static final Log LOG = LogFactory.getLog(DiscoveryTransport.class);
   41   
   42       private final CompositeTransport next;
   43       private DiscoveryAgent discoveryAgent;
   44       private final ConcurrentHashMap<String, URI> serviceURIs = new ConcurrentHashMap<String, URI>();
   45   
   46       private Map<String, String> parameters;
   47   
   48       public DiscoveryTransport(CompositeTransport next) {
   49           super(next);
   50           this.next = next;
   51       }
   52   
   53       public void start() throws Exception {
   54           if (discoveryAgent == null) {
   55               throw new IllegalStateException("discoveryAgent not configured");
   56           }
   57   
   58           // lets pass into the agent the broker name and connection details
   59           discoveryAgent.setDiscoveryListener(this);
   60           discoveryAgent.start();
   61           next.start();
   62       }
   63   
   64       public void stop() throws Exception {
   65           ServiceStopper ss = new ServiceStopper();
   66           ss.stop(discoveryAgent);
   67           ss.stop(next);
   68           ss.throwFirstException();
   69       }
   70   
   71       public void onServiceAdd(DiscoveryEvent event) {
   72           String url = event.getServiceName();
   73           if (url != null) {
   74               try {
   75                   URI uri = new URI(url);
   76                   serviceURIs.put(event.getServiceName(), uri);
   77                   LOG.info("Adding new broker connection URL: " + uri);
   78                   next.add(new URI[] {URISupport.applyParameters(uri, parameters)});
   79               } catch (URISyntaxException e) {
   80                   LOG.warn("Could not connect to remote URI: " + url + " due to bad URI syntax: " + e, e);
   81               }
   82           }
   83       }
   84   
   85       public void onServiceRemove(DiscoveryEvent event) {
   86           URI uri = serviceURIs.get(event.getServiceName());
   87           if (uri != null) {
   88               next.remove(new URI[] {uri});
   89           }
   90       }
   91   
   92       public DiscoveryAgent getDiscoveryAgent() {
   93           return discoveryAgent;
   94       }
   95   
   96       public void setDiscoveryAgent(DiscoveryAgent discoveryAgent) {
   97           this.discoveryAgent = discoveryAgent;
   98       }
   99   
  100       public void setParameters(Map<String, String> parameters) {
  101          this.parameters = parameters;      
  102       }
  103   
  104   }

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