无法启动 JMXConnectorServer
Can't start JMXConnectorServer
我想通过 JMX 远程连接到我的应用程序,所以我在 main 方法中创建了以下配置:
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:7890/jmxrmi");
Map<String, Object> envConf = new HashMap<>();
//My custom authenticator
envConf.put(JMXConnectorServer.AUTHENTICATOR, new MyAuthenticator(jmxUsername, jmxPassword));
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, envConf, mbs);
cs.start();
以下是我开始申请的方式:
java -Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=7890
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
Main
但似乎缺少某些东西,我得到以下异常:
Cannot bind to URL [rmi://localhost:7890/jmxrmi]: javax.naming.NoPermissionException [Root exception is java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.AccessException: Cannot modify this registry]
java.io.IOException: Cannot bind to URL [rmi://localhost:7890/jmxrmi]: javax.naming.NoPermissionException [Root exception is java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.AccessException: Cannot modify this registry]
at javax.management.remote.rmi.RMIConnectorServer.newIOException(RMIConnectorServer.java:827)
at javax.management.remote.rmi.RMIConnectorServer.start(RMIConnectorServer.java:432)
at test.jms.Main.start(JmxModule.java:35)
Caused by: javax.naming.NoPermissionException [Root exception is java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.AccessException: Cannot modify this registry]
at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:147)
at com.sun.jndi.toolkit.url.GenericURLContext.bind(GenericURLContext.java:228)
at javax.naming.InitialContext.bind(InitialContext.java:425)
at javax.management.remote.rmi.RMIConnectorServer.bind(RMIConnectorServer.java:644)
at javax.management.remote.rmi.RMIConnectorServer.start(RMIConnectorServer.java:427)
... 4 more
Caused by: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.AccessException: Cannot modify this registry
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:420)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
at sun.rmi.transport.Transport.run(Transport.java:200)
at sun.rmi.transport.Transport.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run4(TCPTransport.java:683)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda/8098086.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:276)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:253)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:379)
at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:141)
... 8 more
Caused by: java.rmi.AccessException: Cannot modify this registry
at sun.management.jmxremote.SingleEntryRegistry.bind(SingleEntryRegistry.java:76)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:410)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
at sun.rmi.transport.Transport.run(Transport.java:200)
at sun.rmi.transport.Transport.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run4(TCPTransport.java:683)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda/8098086.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
你能指出我哪里错了吗?
通过使用启动应用程序
java -Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.端口=7890
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=假
主要
您正在端口 7890 上启动默认的 RMI JMX 连接器。您正在尝试在同一端口上启动自定义连接器,但它失败了。
如果您打算使用自己的连接器实例,您可以安全地省略所有 -Dcom.sun.management.jmxremote.* 属性。
问题是我没有 RMI 注册表 运行,所以只需 运行 在控制台中执行此命令:
rmiregistry
或者在服务器启动之前添加这行代码:
LocateRegistry.createRegistry(1099);
解决了一个问题。
下面是从 https://docs.oracle.com/cd/E19698-01/816-7609/6mdjrf86t/index.html#security-ex-37 and https://docs.oracle.com/javase/8/docs/technotes/guides/jmx/tutorial/security.html#wp997065
扩展而来的 JDK 1.8 的最小工作示例
链接中的代码和接受的答案中缺少的关键步骤是:
- LocateReigstry.createRegistry(端口)
- Thread.sleep() 循环以确保应用程序不会关闭。
- 答案没有描述 MyAuthenticator。我的回答使用默认的身份验证器,它需要访问和密码文件的路径。
工作代码Main.java:
import javax.management.MBeanServer;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
if(args.length != 2) {
throw new IllegalArgumentException("Main <port> <path suffix>"
+ "\nservice:jmx:rmi:///jndi/rmi://localhost:<port>/<pathsuffix>"
);
}
int port = Integer.parseInt(args[0]);
String pathSuffix = args[1];
LocateRegistry.createRegistry(port);
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://0.0.0.0:" + port + "/" + pathSuffix);
Map<String, Object> envConf = new HashMap<>();
envConf.put("jmx.remote.x.password.file", "password.properties");
envConf.put("jmx.remote.x.access.file", "access.properties");
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, envConf, mbs);
cs.start();
while(true) {
Thread.sleep(1000);
}
}
}
jmx.remote.x.access.file 的内容:
admin readwrite
jmx.remote.x.password.file 的内容:
admin password
我使用(假设 8474 可用)从 maven 执行代码:
javac Main.java
java Main 8474 jmxrmi
我想通过 JMX 远程连接到我的应用程序,所以我在 main 方法中创建了以下配置:
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:7890/jmxrmi");
Map<String, Object> envConf = new HashMap<>();
//My custom authenticator
envConf.put(JMXConnectorServer.AUTHENTICATOR, new MyAuthenticator(jmxUsername, jmxPassword));
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, envConf, mbs);
cs.start();
以下是我开始申请的方式:
java -Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=7890
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
Main
但似乎缺少某些东西,我得到以下异常:
Cannot bind to URL [rmi://localhost:7890/jmxrmi]: javax.naming.NoPermissionException [Root exception is java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.AccessException: Cannot modify this registry]
java.io.IOException: Cannot bind to URL [rmi://localhost:7890/jmxrmi]: javax.naming.NoPermissionException [Root exception is java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.AccessException: Cannot modify this registry]
at javax.management.remote.rmi.RMIConnectorServer.newIOException(RMIConnectorServer.java:827)
at javax.management.remote.rmi.RMIConnectorServer.start(RMIConnectorServer.java:432)
at test.jms.Main.start(JmxModule.java:35)
Caused by: javax.naming.NoPermissionException [Root exception is java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.AccessException: Cannot modify this registry]
at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:147)
at com.sun.jndi.toolkit.url.GenericURLContext.bind(GenericURLContext.java:228)
at javax.naming.InitialContext.bind(InitialContext.java:425)
at javax.management.remote.rmi.RMIConnectorServer.bind(RMIConnectorServer.java:644)
at javax.management.remote.rmi.RMIConnectorServer.start(RMIConnectorServer.java:427)
... 4 more
Caused by: java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.AccessException: Cannot modify this registry
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:420)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
at sun.rmi.transport.Transport.run(Transport.java:200)
at sun.rmi.transport.Transport.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run4(TCPTransport.java:683)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda/8098086.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:276)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:253)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:379)
at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:141)
... 8 more
Caused by: java.rmi.AccessException: Cannot modify this registry
at sun.management.jmxremote.SingleEntryRegistry.bind(SingleEntryRegistry.java:76)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:410)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
at sun.rmi.transport.Transport.run(Transport.java:200)
at sun.rmi.transport.Transport.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run4(TCPTransport.java:683)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda/8098086.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
你能指出我哪里错了吗?
通过使用启动应用程序 java -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.端口=7890 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=假 主要
您正在端口 7890 上启动默认的 RMI JMX 连接器。您正在尝试在同一端口上启动自定义连接器,但它失败了。
如果您打算使用自己的连接器实例,您可以安全地省略所有 -Dcom.sun.management.jmxremote.* 属性。
问题是我没有 RMI 注册表 运行,所以只需 运行 在控制台中执行此命令:
rmiregistry
或者在服务器启动之前添加这行代码:
LocateRegistry.createRegistry(1099);
解决了一个问题。
下面是从 https://docs.oracle.com/cd/E19698-01/816-7609/6mdjrf86t/index.html#security-ex-37 and https://docs.oracle.com/javase/8/docs/technotes/guides/jmx/tutorial/security.html#wp997065
扩展而来的 JDK 1.8 的最小工作示例链接中的代码和接受的答案中缺少的关键步骤是:
- LocateReigstry.createRegistry(端口)
- Thread.sleep() 循环以确保应用程序不会关闭。
- 答案没有描述 MyAuthenticator。我的回答使用默认的身份验证器,它需要访问和密码文件的路径。
工作代码Main.java:
import javax.management.MBeanServer;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
if(args.length != 2) {
throw new IllegalArgumentException("Main <port> <path suffix>"
+ "\nservice:jmx:rmi:///jndi/rmi://localhost:<port>/<pathsuffix>"
);
}
int port = Integer.parseInt(args[0]);
String pathSuffix = args[1];
LocateRegistry.createRegistry(port);
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://0.0.0.0:" + port + "/" + pathSuffix);
Map<String, Object> envConf = new HashMap<>();
envConf.put("jmx.remote.x.password.file", "password.properties");
envConf.put("jmx.remote.x.access.file", "access.properties");
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, envConf, mbs);
cs.start();
while(true) {
Thread.sleep(1000);
}
}
}
jmx.remote.x.access.file 的内容:
admin readwrite
jmx.remote.x.password.file 的内容:
admin password
我使用(假设 8474 可用)从 maven 执行代码:
javac Main.java
java Main 8474 jmxrmi