Skip to content

Commit

Permalink
[AMQ-7400] This closes #444
Browse files Browse the repository at this point in the history
  • Loading branch information
jbonofre committed Feb 11, 2020
2 parents 5f818d0 + 359ae4b commit 48cd61d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@
package org.apache.activemq.broker.jmx;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;
import java.rmi.AccessException;
import java.rmi.AlreadyBoundException;
import java.rmi.NoSuchObjectException;
import java.rmi.registry.LocateRegistry;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand All @@ -42,8 +48,9 @@
import javax.management.ObjectName;
import javax.management.QueryExp;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import javax.management.remote.rmi.RMIConnectorServer;
import javax.management.remote.rmi.RMIJRMPServerImpl;

import org.apache.activemq.Service;
import org.slf4j.Logger;
Expand Down Expand Up @@ -98,6 +105,8 @@ public class ManagementContext implements Service {
private String brokerName;
private String suppressMBean;
private List<ObjectName> suppressMBeanList;
private Remote serverStub;
private RMIJRMPServerImpl server;

public ManagementContext() {
this(null);
Expand Down Expand Up @@ -140,20 +149,20 @@ public void run() {
MDC.put("activemq.broker", brokerName);
}
try {
JMXConnectorServer server = connectorServer;
if (started.get() && server != null) {
LOG.debug("Starting JMXConnectorServer...");
try {
// need to remove MDC as we must not inherit MDC in child threads causing leaks
MDC.remove("activemq.broker");
server.start();
connectorServer.start();
serverStub = server.toStub();
} finally {
if (brokerName != null) {
MDC.put("activemq.broker", brokerName);
}
connectorStarted.countDown();
}
LOG.info("JMX consoles can connect to {}", server.getAddress());
LOG.info("JMX consoles can connect to {}", connectorServer.getAddress());
}
} catch (IOException e) {
LOG.warn("Failed to start JMX connector {}. Will restart management to re-create JMX connector, trying to remedy this issue.", e.getMessage());
Expand Down Expand Up @@ -546,8 +555,9 @@ private void createConnector(MBeanServer mbeanServer) throws MalformedObjectName
try {
if (registry == null) {
LOG.debug("Creating RMIRegistry on port {}", connectorPort);
registry = LocateRegistry.createRegistry(connectorPort);
registry = new JmxRegistry(connectorPort);
}

namingServiceObjectName = ObjectName.getInstance("naming:type=rmiregistry");

// Do not use the createMBean as the mx4j jar may not be in the
Expand All @@ -570,10 +580,14 @@ private void createConnector(MBeanServer mbeanServer) throws MalformedObjectName
// This is handy to use if you have a firewall and need to force JMX to use fixed ports.
rmiServer = ""+getConnectorHost()+":" + rmiServerPort;
}
String serviceURL = "service:jmx:rmi://" + rmiServer + "/jndi/rmi://" +getConnectorHost()+":" + connectorPort + connectorPath;
JMXServiceURL url = new JMXServiceURL(serviceURL);
connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, environment, mbeanServer);

final Map<String,Object> env = new HashMap<>();
server = new RMIJRMPServerImpl(connectorPort, null, null, environment);

final String serviceURL = "service:jmx:rmi://" + rmiServer + "/jndi/rmi://" +getConnectorHost()+":" + connectorPort + connectorPath;
final JMXServiceURL url = new JMXServiceURL(serviceURL);

connectorServer = new RMIConnectorServer(url, env, server, ManagementFactory.getPlatformMBeanServer());
LOG.debug("Created JMXConnectorServer {}", connectorServer);
}

Expand Down Expand Up @@ -664,4 +678,39 @@ public void setSuppressMBean(String commaListOfAttributeKeyValuePairs) {
public String getSuppressMBean() {
return suppressMBean;
}

/*
* Better to use the internal API than re-invent the wheel.
*/
@SuppressWarnings("restriction")
private class JmxRegistry extends sun.rmi.registry.RegistryImpl {
public static final String LOOKUP_NAME = "jmxrmi";

public JmxRegistry(int port) throws RemoteException {
super(port);
}

@Override

public Remote lookup(String s) throws RemoteException, NotBoundException {
return LOOKUP_NAME.equals(s) ? serverStub : null;
}

@Override
public void bind(String s, Remote remote) throws RemoteException, AlreadyBoundException, AccessException {
}

@Override
public void unbind(String s) throws RemoteException, NotBoundException, AccessException {
}

@Override
public void rebind(String s, Remote remote) throws RemoteException, AccessException {
}

@Override
public String[] list() throws RemoteException {
return new String[] {LOOKUP_NAME};
}
}
}
1 change: 1 addition & 0 deletions activemq-osgi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
!com.google.j2objc.annotations,
sun.misc*;resolution:=optional,
sun.nio*;resolution:=optional,
sun.rmi*;resolution:=optional,
javax.jmdns*;resolution:=optional,
javax.resource*;resolution:=optional,
javax.servlet*;resolution:=optional,
Expand Down

0 comments on commit 48cd61d

Please sign in to comment.