1 18 package org.apache.activemq.transport.discovery; 19 20 import java.io.IOException ; 21 import java.net.URI ; 22 import java.net.URISyntaxException ; 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.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import java.util.concurrent.ConcurrentHashMap ; 32 33 39 public class DiscoveryTransport extends TransportFilter implements DiscoveryListener { 40 41 private static final Log log = LogFactory.getLog(DiscoveryTransport.class); 42 43 private final CompositeTransport next; 44 private DiscoveryAgent discoveryAgent; 45 private final ConcurrentHashMap serviceURIs = new ConcurrentHashMap (); 46 47 public DiscoveryTransport(CompositeTransport next) { 48 super(next); 49 this.next = next; 50 } 51 52 public void start() throws Exception { 53 if (discoveryAgent == null) { 54 throw new IllegalStateException ("discoveryAgent not configured"); 55 } 56 57 discoveryAgent.setDiscoveryListener(this); 59 discoveryAgent.start(); 60 next.start(); 61 } 62 63 public void stop() throws Exception { 64 ServiceStopper ss = new ServiceStopper(); 65 ss.stop(discoveryAgent); 66 ss.stop(next); 67 ss.throwFirstException(); 68 } 69 70 public void onServiceAdd(DiscoveryEvent event) { 71 String url = event.getServiceName(); 72 if (url != null) { 73 try { 74 URI uri = new URI (url); 75 serviceURIs.put(event.getServiceName(), uri); 76 log.info("Adding new broker connection URL: " + uri ); 77 next.add(new URI []{uri}); 78 } catch (URISyntaxException e) { 79 log.warn("Could not connect to remote URI: " + url + " due to bad URI syntax: " + e, e); 80 } 81 } 82 } 83 84 public void onServiceRemove(DiscoveryEvent event) { 85 URI uri = (URI ) serviceURIs.get(event.getServiceName()); 86 if (uri != null) { 87 next.remove(new URI []{uri}); 88 } 89 } 90 91 public DiscoveryAgent getDiscoveryAgent() { 92 return discoveryAgent; 93 } 94 95 public void setDiscoveryAgent(DiscoveryAgent discoveryAgent) { 96 this.discoveryAgent = discoveryAgent; 97 } 98 99 } 100 | Popular Tags |