https://issues.apache.org/jira/browse/AMQ-3918 - transport connector urls exposed using JMX

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1359194 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Bosanac Dejan 2012-07-09 14:43:30 +00:00
parent b7dc057abd
commit cb895f01b7
8 changed files with 142 additions and 26 deletions

View File

@ -2328,11 +2328,11 @@ public class BrokerService implements Service {
if (policy != null) { if (policy != null) {
connector.setMessageAuthorizationPolicy(policy); connector.setMessageAuthorizationPolicy(policy);
} }
connector.getStatistics().setEnabled(enableStatistics);
connector.start();
if (isUseJmx()) { if (isUseJmx()) {
connector = registerConnectorMBean(connector); connector = registerConnectorMBean(connector);
} }
connector.getStatistics().setEnabled(enableStatistics);
connector.start();
return connector; return connector;
} }

View File

@ -23,6 +23,8 @@ import java.lang.reflect.Method;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import javax.management.ObjectName; import javax.management.ObjectName;
@ -364,21 +366,54 @@ public class BrokerView implements BrokerViewMBean {
} }
} }
public Map<String, String> getTransportConnectors() {
Map<String, String> answer = new HashMap<String, String>();
try {
for (TransportConnector connector : brokerService.getTransportConnectors()) {
answer.put(connector.getName(), connector.getConnectUri().toString());
}
} catch (Exception e) {
LOG.debug("Failed to read URI to build transport connectors map", e);
}
return answer;
}
@Override
public String getTransportConnectorByType(String type) {
return brokerService.getTransportConnectorURIsAsMap().get(type);
}
@Deprecated
/**
* @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)}
*/
public String getOpenWireURL() { public String getOpenWireURL() {
String answer = brokerService.getTransportConnectorURIsAsMap().get("tcp"); String answer = brokerService.getTransportConnectorURIsAsMap().get("tcp");
return answer != null ? answer : ""; return answer != null ? answer : "";
} }
@Deprecated
/**
* @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)}
*/
public String getStompURL() { public String getStompURL() {
String answer = brokerService.getTransportConnectorURIsAsMap().get("stomp"); String answer = brokerService.getTransportConnectorURIsAsMap().get("stomp");
return answer != null ? answer : ""; return answer != null ? answer : "";
} }
@Deprecated
/**
* @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)}
*/
public String getSslURL() { public String getSslURL() {
String answer = brokerService.getTransportConnectorURIsAsMap().get("ssl"); String answer = brokerService.getTransportConnectorURIsAsMap().get("ssl");
return answer != null ? answer : ""; return answer != null ? answer : "";
} }
@Deprecated
/**
* @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)}
*/
public String getStompSslURL() { public String getStompSslURL() {
String answer = brokerService.getTransportConnectorURIsAsMap().get("stomp+ssl"); String answer = brokerService.getTransportConnectorURIsAsMap().get("stomp+ssl");
return answer != null ? answer : ""; return answer != null ? answer : "";

View File

@ -19,6 +19,8 @@ package org.apache.activemq.broker.jmx;
import javax.management.ObjectName; import javax.management.ObjectName;
import org.apache.activemq.Service; import org.apache.activemq.Service;
import java.util.Map;
/** /**
* @author David Martin Clavo david(dot)martin(dot)clavo(at)gmail.com (for the reloadLog4jProperties method) * @author David Martin Clavo david(dot)martin(dot)clavo(at)gmail.com (for the reloadLog4jProperties method)
@ -249,21 +251,43 @@ public interface BrokerViewMBean extends Service {
@MBeanInfo(value="Reloads log4j.properties from the classpath.") @MBeanInfo(value="Reloads log4j.properties from the classpath.")
public void reloadLog4jProperties() throws Throwable; public void reloadLog4jProperties() throws Throwable;
@MBeanInfo("The url of the openwire connector") /**
* @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)}
*/
@Deprecated
@MBeanInfo("The url of the openwire connector - deprecated, use getTransportConnectors or getTransportConnectorByType instead")
String getOpenWireURL(); String getOpenWireURL();
@MBeanInfo("The url of the stomp connector") /**
* @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)}
*/
@Deprecated
@MBeanInfo("The url of the stomp connector - deprecated, use getTransportConnectors or getTransportConnectorByType instead")
String getStompURL(); String getStompURL();
@MBeanInfo("The url of the SSL connector") /**
* @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)}
*/
@Deprecated
@MBeanInfo("The url of the SSL connector - deprecated, use getTransportConnectors or getTransportConnectorByType instead")
String getSslURL(); String getSslURL();
@MBeanInfo("The url of the Stomp SSL connector") /**
* @deprecated use {@link #getTransportConnectors()} or {@link #getTransportConnectorByType(String)}
*/
@Deprecated
@MBeanInfo("The url of the Stomp SSL connector - deprecated, use getTransportConnectors or getTransportConnectorByType instead")
String getStompSslURL(); String getStompSslURL();
@MBeanInfo("The url of the VM connector") @MBeanInfo("The url of the VM connector")
String getVMURL(); String getVMURL();
@MBeanInfo("The map of all defined transport connectors, with transport name as a key")
Map<String, String> getTransportConnectors();
@MBeanInfo("The url of transport connector by it's type; e.g. tcp, stomp, ssl, etc.")
String getTransportConnectorByType(String type);
@MBeanInfo("The location of the data directory") @MBeanInfo("The location of the data directory")
public String getDataDirectory(); public String getDataDirectory();

View File

@ -88,4 +88,6 @@ public abstract class TransportServerSupport extends ServiceSupport implements T
public void setTransportOption(Map<String, Object> transportOptions) { public void setTransportOption(Map<String, Object> transportOptions) {
this.transportOptions = transportOptions; this.transportOptions = transportOptions;
} }
} }

View File

@ -90,8 +90,9 @@ public class MBeanTest extends EmbeddedBrokerTestSupport {
public void testConnectors() throws Exception{ public void testConnectors() throws Exception{
ObjectName brokerName = assertRegisteredObjectName(domain + ":Type=Broker,BrokerName=localhost"); ObjectName brokerName = assertRegisteredObjectName(domain + ":Type=Broker,BrokerName=localhost");
BrokerViewMBean broker = (BrokerViewMBean)MBeanServerInvocationHandler.newProxyInstance(mbeanServer, brokerName, BrokerViewMBean.class, true); BrokerViewMBean broker = (BrokerViewMBean)MBeanServerInvocationHandler.newProxyInstance(mbeanServer, brokerName, BrokerViewMBean.class, true);
assertEquals("openwire URL port doesn't equal bind Address", assertEquals("openwire URL port doesn't equal bind Address",
new URI(broker.getOpenWireURL()).getPort(), new URI(broker.getTransportConnectorByType("tcp")).getPort(),
new URI(this.broker.getTransportConnectors().get(0).getPublishableConnectString()).getPort()); new URI(this.broker.getTransportConnectors().get(0).getPublishableConnectString()).getPort());
} }

View File

@ -0,0 +1,56 @@
/**
* 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.transport;
import org.apache.activemq.util.InetAddressUtil;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import java.net.InetAddress;
import java.net.URI;
abstract public class WebTransportServerSupport extends TransportServerSupport {
protected URI bindAddress;
protected Server server;
protected Connector connector;
protected SocketConnectorFactory socketConnectorFactory;
protected String host;
public WebTransportServerSupport(URI location) {
super(location);
}
public void bind() throws Exception {
URI bind = getBindLocation();
String bindHost = bind.getHost();
bindHost = (bindHost == null || bindHost.length() == 0) ? "localhost" : bindHost;
InetAddress addr = InetAddress.getByName(bindHost);
host = addr.getCanonicalHostName();
if (addr.isAnyLocalAddress()) {
host = InetAddressUtil.getLocalHostName();
}
connector.setHost(host);
connector.setPort(bindAddress.getPort());
connector.setServer(server);
server.addConnector(connector);
}
}

View File

@ -19,6 +19,7 @@ package org.apache.activemq.transport.http;
import org.apache.activemq.command.BrokerInfo; import org.apache.activemq.command.BrokerInfo;
import org.apache.activemq.transport.SocketConnectorFactory; import org.apache.activemq.transport.SocketConnectorFactory;
import org.apache.activemq.transport.TransportServerSupport; import org.apache.activemq.transport.TransportServerSupport;
import org.apache.activemq.transport.WebTransportServerSupport;
import org.apache.activemq.transport.util.TextWireFormat; import org.apache.activemq.transport.util.TextWireFormat;
import org.apache.activemq.transport.xstream.XStreamWireFormat; import org.apache.activemq.transport.xstream.XStreamWireFormat;
import org.apache.activemq.util.ServiceStopper; import org.apache.activemq.util.ServiceStopper;
@ -28,18 +29,15 @@ import org.eclipse.jetty.server.handler.GzipHandler;
import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.URI; import java.net.URI;
import java.util.Map; import java.util.Map;
public class HttpTransportServer extends TransportServerSupport { public class HttpTransportServer extends WebTransportServerSupport {
private URI bindAddress;
private TextWireFormat wireFormat; private TextWireFormat wireFormat;
private Server server;
private Connector connector;
private HttpTransportFactory transportFactory; private HttpTransportFactory transportFactory;
protected SocketConnectorFactory socketConnectorFactory;
public HttpTransportServer(URI uri, HttpTransportFactory factory) { public HttpTransportServer(URI uri, HttpTransportFactory factory) {
super(uri); super(uri);
@ -79,10 +77,9 @@ public class HttpTransportServer extends TransportServerSupport {
if (connector == null) { if (connector == null) {
connector = socketConnectorFactory.createConnector(); connector = socketConnectorFactory.createConnector();
} }
connector.setHost(bindAddress.getHost());
connector.setPort(bindAddress.getPort()); URI bind = getBindLocation();
connector.setServer(server); bind();
server.addConnector(connector);
ServletContextHandler contextHandler = ServletContextHandler contextHandler =
new ServletContextHandler(server, "/", ServletContextHandler.NO_SECURITY); new ServletContextHandler(server, "/", ServletContextHandler.NO_SECURITY);
@ -100,6 +97,7 @@ public class HttpTransportServer extends TransportServerSupport {
contextHandler.setHandler(gzipHandler); contextHandler.setHandler(gzipHandler);
server.start(); server.start();
setConnectURI(new URI(bind.getScheme(), bind.getUserInfo(), host, connector.getLocalPort(), bind.getPath(), bind.getQuery(), bind.getFragment()));
} }
protected void doStop(ServiceStopper stopper) throws Exception { protected void doStop(ServiceStopper stopper) throws Exception {

View File

@ -20,6 +20,8 @@ package org.apache.activemq.transport.ws;
import org.apache.activemq.command.BrokerInfo; import org.apache.activemq.command.BrokerInfo;
import org.apache.activemq.transport.SocketConnectorFactory; import org.apache.activemq.transport.SocketConnectorFactory;
import org.apache.activemq.transport.TransportServerSupport; import org.apache.activemq.transport.TransportServerSupport;
import org.apache.activemq.transport.WebTransportServerSupport;
import org.apache.activemq.util.InetAddressUtil;
import org.apache.activemq.util.ServiceStopper; import org.apache.activemq.util.ServiceStopper;
import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
@ -27,6 +29,7 @@ import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.URI; import java.net.URI;
import java.util.Map; import java.util.Map;
@ -35,12 +38,7 @@ import java.util.Map;
* Creates a web server and registers web socket server * Creates a web server and registers web socket server
* *
*/ */
public class WSTransportServer extends TransportServerSupport { public class WSTransportServer extends WebTransportServerSupport {
private URI bindAddress;
private Server server;
private Connector connector;
protected SocketConnectorFactory socketConnectorFactory;
public WSTransportServer(URI location) { public WSTransportServer(URI location) {
super(location); super(location);
@ -50,13 +48,14 @@ public class WSTransportServer extends TransportServerSupport {
protected void doStart() throws Exception { protected void doStart() throws Exception {
server = new Server(); server = new Server();
if (connector == null) { if (connector == null) {
connector = socketConnectorFactory.createConnector(); connector = socketConnectorFactory.createConnector();
} }
connector.setHost(bindAddress.getHost());
connector.setPort(bindAddress.getPort()); URI bind = getBindLocation();
connector.setServer(server);
server.addConnector(connector); bind();
ServletContextHandler contextHandler = ServletContextHandler contextHandler =
new ServletContextHandler(server, "/", ServletContextHandler.NO_SECURITY); new ServletContextHandler(server, "/", ServletContextHandler.NO_SECURITY);
@ -68,6 +67,7 @@ public class WSTransportServer extends TransportServerSupport {
contextHandler.setAttribute("acceptListener", getAcceptListener()); contextHandler.setAttribute("acceptListener", getAcceptListener());
server.start(); server.start();
setConnectURI(new URI(bind.getScheme(), bind.getUserInfo(), host, connector.getLocalPort(), bind.getPath(), bind.getQuery(), bind.getFragment()));
} }
protected void doStop(ServiceStopper stopper) throws Exception { protected void doStop(ServiceStopper stopper) throws Exception {