1 18 package org.apache.activemq.broker.jmx; 19 20 import org.apache.activemq.broker.Broker; 21 import org.apache.activemq.broker.TransportConnection; 22 import org.apache.activemq.broker.TransportConnector; 23 import org.apache.activemq.command.ConnectionInfo; 24 import org.apache.activemq.command.Response; 25 import org.apache.activemq.thread.TaskRunnerFactory; 26 import org.apache.activemq.transport.Transport; 27 import org.apache.activemq.util.IOExceptionSupport; 28 import org.apache.activemq.util.JMXSupport; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 import javax.management.MBeanServer ; 33 import javax.management.ObjectName ; 34 35 import java.io.IOException ; 36 import java.util.Hashtable ; 37 38 43 public class ManagedTransportConnection extends TransportConnection { 44 private static final Log log = LogFactory.getLog(ManagedTransportConnection.class); 45 46 private final MBeanServer server; 47 private final ObjectName connectorName; 48 private ConnectionViewMBean mbean; 49 50 private ObjectName byClientIdName; 51 private ObjectName byAddressName; 52 53 public ManagedTransportConnection(TransportConnector connector, Transport transport, Broker broker, TaskRunnerFactory factory, MBeanServer server, 54 ObjectName connectorName) throws IOException { 55 super(connector, transport, broker, factory); 56 this.server = server; 57 this.connectorName = connectorName; 58 this.mbean = new ConnectionView(this); 59 byAddressName = createByAddressObjectName("address", transport.getRemoteAddress()); 60 registerMBean(byAddressName); 61 } 62 63 public synchronized void stop() throws Exception { 64 if (isStarting()) { 65 setPendingStop(true); 66 return; 67 } 68 synchronized(this) { 69 unregisterMBean(byClientIdName); 70 unregisterMBean(byAddressName); 71 byClientIdName=null; 72 byAddressName=null; 73 } 74 super.stop(); 75 } 76 77 82 public void setConnectionId(String connectionId) throws IOException { 83 } 84 85 public Response processAddConnection(ConnectionInfo info) throws Exception { 86 Response answer = super.processAddConnection(info); 87 String clientId = info.getClientId(); 88 if (clientId != null) { 89 if(byClientIdName==null) { 90 byClientIdName = createByClientIdObjectName(clientId); 91 registerMBean(byClientIdName); 92 } 93 } 94 return answer; 95 } 96 97 protected void registerMBean(ObjectName name) { 100 if( name!=null ) { 101 try { 102 server.registerMBean(mbean, name); 103 } catch (Throwable e) { 104 log.warn("Failed to register MBean: "+name); 105 log.debug("Failure reason: "+e,e); 106 } 107 } 108 } 109 110 protected void unregisterMBean(ObjectName name) { 111 if (name != null) { 112 try { 113 server.unregisterMBean(name); 114 } 115 catch (Throwable e) { 116 log.warn("Failed to unregister mbean: " + name); 117 log.debug("Failure reason: "+e,e); 118 } 119 } 120 } 121 122 protected ObjectName createByAddressObjectName(String type, String value) throws IOException { 123 Hashtable map = connectorName.getKeyPropertyList(); 125 try { 126 return new ObjectName ( 127 connectorName.getDomain()+":"+ 128 "BrokerName="+JMXSupport.encodeObjectNamePart((String ) map.get("BrokerName"))+","+ 129 "Type=Connection,"+ 130 "ConnectorName="+JMXSupport.encodeObjectNamePart((String ) map.get("ConnectorName"))+","+ 131 "ViewType="+JMXSupport.encodeObjectNamePart(type)+","+ 132 "Name="+JMXSupport.encodeObjectNamePart(value) 133 ); 134 } 135 catch (Throwable e) { 136 throw IOExceptionSupport.create(e); 137 } 138 } 139 140 protected ObjectName createByClientIdObjectName(String value) throws IOException { 141 Hashtable map = connectorName.getKeyPropertyList(); 143 try { 144 return new ObjectName ( 145 connectorName.getDomain()+":"+ 146 "BrokerName="+JMXSupport.encodeObjectNamePart((String ) map.get("BrokerName"))+","+ 147 "Type=Connection,"+ 148 "ConnectorName="+JMXSupport.encodeObjectNamePart((String ) map.get("ConnectorName"))+","+ 149 "Connection="+JMXSupport.encodeObjectNamePart(value) 150 ); 151 } 152 catch (Throwable e) { 153 throw IOExceptionSupport.create(e); 154 } 155 } 156 157 158 } 159 | Popular Tags |