17
17
package org .apache .activemq .broker .jmx ;
18
18
19
19
import java .io .IOException ;
20
+ import java .lang .management .ManagementFactory ;
20
21
import java .lang .reflect .Method ;
22
+ import java .rmi .AccessException ;
23
+ import java .rmi .AlreadyBoundException ;
21
24
import java .rmi .NoSuchObjectException ;
22
- import java .rmi .registry .LocateRegistry ;
25
+ import java .rmi .NotBoundException ;
26
+ import java .rmi .Remote ;
27
+ import java .rmi .RemoteException ;
23
28
import java .rmi .registry .Registry ;
24
29
import java .rmi .server .UnicastRemoteObject ;
30
+ import java .util .HashMap ;
25
31
import java .util .LinkedList ;
26
32
import java .util .List ;
27
33
import java .util .Map ;
42
48
import javax .management .ObjectName ;
43
49
import javax .management .QueryExp ;
44
50
import javax .management .remote .JMXConnectorServer ;
45
- import javax .management .remote .JMXConnectorServerFactory ;
46
51
import javax .management .remote .JMXServiceURL ;
52
+ import javax .management .remote .rmi .RMIConnectorServer ;
53
+ import javax .management .remote .rmi .RMIJRMPServerImpl ;
47
54
48
55
import org .apache .activemq .Service ;
49
56
import org .slf4j .Logger ;
@@ -98,6 +105,8 @@ public class ManagementContext implements Service {
98
105
private String brokerName ;
99
106
private String suppressMBean ;
100
107
private List <ObjectName > suppressMBeanList ;
108
+ private Remote serverStub ;
109
+ private RMIJRMPServerImpl server ;
101
110
102
111
public ManagementContext () {
103
112
this (null );
@@ -140,20 +149,20 @@ public void run() {
140
149
MDC .put ("activemq.broker" , brokerName );
141
150
}
142
151
try {
143
- JMXConnectorServer server = connectorServer ;
144
152
if (started .get () && server != null ) {
145
153
LOG .debug ("Starting JMXConnectorServer..." );
146
154
try {
147
155
// need to remove MDC as we must not inherit MDC in child threads causing leaks
148
156
MDC .remove ("activemq.broker" );
149
- server .start ();
157
+ connectorServer .start ();
158
+ serverStub = server .toStub ();
150
159
} finally {
151
160
if (brokerName != null ) {
152
161
MDC .put ("activemq.broker" , brokerName );
153
162
}
154
163
connectorStarted .countDown ();
155
164
}
156
- LOG .info ("JMX consoles can connect to {}" , server .getAddress ());
165
+ LOG .info ("JMX consoles can connect to {}" , connectorServer .getAddress ());
157
166
}
158
167
} catch (IOException e ) {
159
168
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
546
555
try {
547
556
if (registry == null ) {
548
557
LOG .debug ("Creating RMIRegistry on port {}" , connectorPort );
549
- registry = LocateRegistry . createRegistry (connectorPort );
558
+ registry = new JmxRegistry (connectorPort );
550
559
}
560
+
551
561
namingServiceObjectName = ObjectName .getInstance ("naming:type=rmiregistry" );
552
562
553
563
// 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
570
580
// This is handy to use if you have a firewall and need to force JMX to use fixed ports.
571
581
rmiServer = "" +getConnectorHost ()+":" + rmiServerPort ;
572
582
}
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 );
576
583
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 ());
577
591
LOG .debug ("Created JMXConnectorServer {}" , connectorServer );
578
592
}
579
593
@@ -664,4 +678,39 @@ public void setSuppressMBean(String commaListOfAttributeKeyValuePairs) {
664
678
public String getSuppressMBean () {
665
679
return suppressMBean ;
666
680
}
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
+ }
667
716
}
0 commit comments