1717package org .apache .activemq .broker .jmx ;
1818
1919import java .io .IOException ;
20+ import java .lang .management .ManagementFactory ;
2021import java .lang .reflect .Method ;
22+ import java .rmi .AccessException ;
23+ import java .rmi .AlreadyBoundException ;
2124import java .rmi .NoSuchObjectException ;
22- import java .rmi .registry .LocateRegistry ;
25+ import java .rmi .NotBoundException ;
26+ import java .rmi .Remote ;
27+ import java .rmi .RemoteException ;
2328import java .rmi .registry .Registry ;
2429import java .rmi .server .UnicastRemoteObject ;
30+ import java .util .HashMap ;
2531import java .util .LinkedList ;
2632import java .util .List ;
2733import java .util .Map ;
4248import javax .management .ObjectName ;
4349import javax .management .QueryExp ;
4450import javax .management .remote .JMXConnectorServer ;
45- import javax .management .remote .JMXConnectorServerFactory ;
4651import javax .management .remote .JMXServiceURL ;
52+ import javax .management .remote .rmi .RMIConnectorServer ;
53+ import javax .management .remote .rmi .RMIJRMPServerImpl ;
4754
4855import org .apache .activemq .Service ;
4956import org .slf4j .Logger ;
@@ -98,6 +105,8 @@ public class ManagementContext implements Service {
98105 private String brokerName ;
99106 private String suppressMBean ;
100107 private List <ObjectName > suppressMBeanList ;
108+ private Remote serverStub ;
109+ private RMIJRMPServerImpl server ;
101110
102111 public ManagementContext () {
103112 this (null );
@@ -140,20 +149,20 @@ public void run() {
140149 MDC .put ("activemq.broker" , brokerName );
141150 }
142151 try {
143- JMXConnectorServer server = connectorServer ;
144152 if (started .get () && server != null ) {
145153 LOG .debug ("Starting JMXConnectorServer..." );
146154 try {
147155 // need to remove MDC as we must not inherit MDC in child threads causing leaks
148156 MDC .remove ("activemq.broker" );
149- server .start ();
157+ connectorServer .start ();
158+ serverStub = server .toStub ();
150159 } finally {
151160 if (brokerName != null ) {
152161 MDC .put ("activemq.broker" , brokerName );
153162 }
154163 connectorStarted .countDown ();
155164 }
156- LOG .info ("JMX consoles can connect to {}" , server .getAddress ());
165+ LOG .info ("JMX consoles can connect to {}" , connectorServer .getAddress ());
157166 }
158167 } catch (IOException e ) {
159168 LOG .warn ("Failed to start JMX connector {}. Will restart management to re-create JMX connector, trying to remedy this issue." , e .getMessage ());
@@ -546,8 +555,9 @@ private void createConnector(MBeanServer mbeanServer) throws MalformedObjectName
546555 try {
547556 if (registry == null ) {
548557 LOG .debug ("Creating RMIRegistry on port {}" , connectorPort );
549- registry = LocateRegistry . createRegistry (connectorPort );
558+ registry = new JmxRegistry (connectorPort );
550559 }
560+
551561 namingServiceObjectName = ObjectName .getInstance ("naming:type=rmiregistry" );
552562
553563 // Do not use the createMBean as the mx4j jar may not be in the
@@ -570,10 +580,14 @@ private void createConnector(MBeanServer mbeanServer) throws MalformedObjectName
570580 // This is handy to use if you have a firewall and need to force JMX to use fixed ports.
571581 rmiServer = "" +getConnectorHost ()+":" + rmiServerPort ;
572582 }
573- String serviceURL = "service:jmx:rmi://" + rmiServer + "/jndi/rmi://" +getConnectorHost ()+":" + connectorPort + connectorPath ;
574- JMXServiceURL url = new JMXServiceURL (serviceURL );
575- connectorServer = JMXConnectorServerFactory .newJMXConnectorServer (url , environment , mbeanServer );
576583
584+ final Map <String ,Object > env = new HashMap <>();
585+ server = new RMIJRMPServerImpl (connectorPort , null , null , environment );
586+
587+ final String serviceURL = "service:jmx:rmi://" + rmiServer + "/jndi/rmi://" +getConnectorHost ()+":" + connectorPort + connectorPath ;
588+ final JMXServiceURL url = new JMXServiceURL (serviceURL );
589+
590+ connectorServer = new RMIConnectorServer (url , env , server , ManagementFactory .getPlatformMBeanServer ());
577591 LOG .debug ("Created JMXConnectorServer {}" , connectorServer );
578592 }
579593
@@ -664,4 +678,39 @@ public void setSuppressMBean(String commaListOfAttributeKeyValuePairs) {
664678 public String getSuppressMBean () {
665679 return suppressMBean ;
666680 }
681+
682+ /*
683+ * Better to use the internal API than re-invent the wheel.
684+ */
685+ @ SuppressWarnings ("restriction" )
686+ private class JmxRegistry extends sun .rmi .registry .RegistryImpl {
687+ public static final String LOOKUP_NAME = "jmxrmi" ;
688+
689+ public JmxRegistry (int port ) throws RemoteException {
690+ super (port );
691+ }
692+
693+ @ Override
694+
695+ public Remote lookup (String s ) throws RemoteException , NotBoundException {
696+ return LOOKUP_NAME .equals (s ) ? serverStub : null ;
697+ }
698+
699+ @ Override
700+ public void bind (String s , Remote remote ) throws RemoteException , AlreadyBoundException , AccessException {
701+ }
702+
703+ @ Override
704+ public void unbind (String s ) throws RemoteException , NotBoundException , AccessException {
705+ }
706+
707+ @ Override
708+ public void rebind (String s , Remote remote ) throws RemoteException , AccessException {
709+ }
710+
711+ @ Override
712+ public String [] list () throws RemoteException {
713+ return new String [] {LOOKUP_NAME };
714+ }
715+ }
667716}
0 commit comments