https://issues.apache.org/jira/browse/AMQ-3438: Make use of remote port in Connection MBeanNames optional, useful when ephemeral range cycles quickly. added boolean attribute org.apache.activemq.broker.jmx.ManagementContext#allowRemoteAddressInMBeanNames, accessible via xml. when false, a transport connector will only be registered by its clientId (whic defaults to connectionid)

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1154162 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary Tully 2011-08-05 11:24:45 +00:00
parent 516558698a
commit c0513811a9
3 changed files with 113 additions and 11 deletions

View File

@ -53,9 +53,11 @@ public class ManagedTransportConnection extends TransportConnection {
this.managementContext = context; this.managementContext = context;
this.connectorName = connectorName; this.connectorName = connectorName;
this.mbean = new ConnectionView(this); this.mbean = new ConnectionView(this);
if (managementContext.isAllowRemoteAddressInMBeanNames()) {
byAddressName = createByAddressObjectName("address", transport.getRemoteAddress()); byAddressName = createByAddressObjectName("address", transport.getRemoteAddress());
registerMBean(byAddressName); registerMBean(byAddressName);
} }
}
public void doStop() throws Exception { public void doStop() throws Exception {
if (isStarting()) { if (isStarting()) {
@ -71,14 +73,6 @@ public class ManagedTransportConnection extends TransportConnection {
super.doStop(); super.doStop();
} }
/**
* Sets the connection ID of this connection. On startup this connection ID
* is set to an incrementing counter; once the client registers it is set to
* the clientID of the JMS client.
*/
public void setConnectionId(String connectionId) throws IOException {
}
public Response processAddConnection(ConnectionInfo info) throws Exception { public Response processAddConnection(ConnectionInfo info) throws Exception {
Response answer = super.processAddConnection(info); Response answer = super.processAddConnection(info);
String clientId = info.getClientId(); String clientId = info.getClientId();

View File

@ -65,6 +65,7 @@ public class ManagementContext implements Service {
private ObjectName namingServiceObjectName; private ObjectName namingServiceObjectName;
private Registry registry; private Registry registry;
private final List<ObjectName> registeredMBeanNames = new CopyOnWriteArrayList<ObjectName>(); private final List<ObjectName> registeredMBeanNames = new CopyOnWriteArrayList<ObjectName>();
private boolean allowRemoteAddressInMBeanNames = true;
public ManagementContext() { public ManagementContext() {
this(null); this(null);
@ -301,7 +302,7 @@ public class ManagementContext implements Service {
return result; return result;
} }
public Set queryNames(ObjectName name, QueryExp query) throws Exception{ public Set<ObjectName> queryNames(ObjectName name, QueryExp query) throws Exception{
return getMBeanServer().queryNames(name, query); return getMBeanServer().queryNames(name, query);
} }
@ -513,4 +514,12 @@ public class ManagementContext implements Service {
public void setEnvironment(Map environment) { public void setEnvironment(Map environment) {
this.environment = environment; this.environment = environment;
} }
public boolean isAllowRemoteAddressInMBeanNames() {
return allowRemoteAddressInMBeanNames;
}
public void setAllowRemoteAddressInMBeanNames(boolean allowRemoteAddressInMBeanNames) {
this.allowRemoteAddressInMBeanNames = allowRemoteAddressInMBeanNames;
}
} }

View File

@ -0,0 +1,99 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.broker.jmx;
import java.net.Socket;
import java.util.Set;
import javax.management.ObjectName;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.util.JMXSupport;
import org.junit.After;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class TransportConnectorMBeanTest {
private static final Logger LOG = LoggerFactory.getLogger(TransportConnectorMBeanTest.class);
BrokerService broker;
@Test
public void verifyRemoteAddressInMbeanName() throws Exception {
doVerifyRemoteAddressInMbeanName(true);
}
@Test
public void verifyRemoteAddressNotInMbeanName() throws Exception {
doVerifyRemoteAddressInMbeanName(false);
}
private void doVerifyRemoteAddressInMbeanName(boolean allowRemoteAddress) throws Exception {
createBroker(allowRemoteAddress);
ActiveMQConnection connection = createConnection();
Set<ObjectName> registeredMbeans = getRegisteredMbeans();
assertTrue("found mbean with clientId", match(connection.getClientID(), registeredMbeans));
assertEquals("presence of mbean with local port", allowRemoteAddress, match(extractLocalPort(connection), registeredMbeans));
}
@After
public void stopBroker() throws Exception {
if (broker != null) {
broker.stop();
}
}
private boolean match(String s, Set<ObjectName> registeredMbeans) {
String encodedName = JMXSupport.encodeObjectNamePart(s);
for (ObjectName name : registeredMbeans) {
LOG.info("checking for match:" + encodedName + ", with: " + name.toString());
if (name.toString().contains(encodedName)) {
return true;
}
}
return false;
}
private String extractLocalPort(ActiveMQConnection connection) throws Exception {
Socket socket = (Socket) connection.getTransport().narrow(Socket.class);
return String.valueOf(socket.getLocalPort());
}
private Set<ObjectName> getRegisteredMbeans() throws Exception {
return broker.getManagementContext().queryNames(null, null);
}
private ActiveMQConnection createConnection() throws Exception {
ActiveMQConnection connection = (ActiveMQConnection)
new ActiveMQConnectionFactory(broker.getTransportConnectors().get(0).getConnectUri()).createConnection();
connection.start();
return connection;
}
private void createBroker(boolean allowRemoteAddressInMbeanNames) throws Exception {
broker = new BrokerService();
broker.setPersistent(false);
broker.addConnector("tcp://localhost:0");
broker.getManagementContext().setAllowRemoteAddressInMBeanNames(allowRemoteAddressInMbeanNames);
broker.start();
}
}