Skip to content

Commit 8fd71cd

Browse files
committed
vjtop 用命令行指定JMX端口 #82
1 parent 5356a94 commit 8fd71cd

File tree

5 files changed

+47
-18
lines changed

5 files changed

+47
-18
lines changed

vjtop/README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,27 @@ ERROR: Could not attach to process.
222222
```
223223

224224

225-
1. 执行vjtop的用户,对/tmp/.java_pid$PID 文件有读写权限,该文件权限为srw------- 1,所以需要相同用户或root权限用户 sudo执行。
225+
1. 执行vjtop的用户,对/tmp/.java_pid$PID 文件有读写权限,该文件权限为srw------- 1,所以需要相同用户
226226

227-
2. /tmp/.java_pid$PID 文件再首次连接时会生成,但如果生成之后被/tmp 目录的清理程序错误删除,JVM将不再能连入,只能重启应用。
227+
2. /tmp/.java_pid$PID 文件在首次连接时会生成,但如果生成之后被/tmp 目录的清理程序错误删除,JVM将不再能连入,只能重启应用。
228228

229229
3. 目标JVM使用启动参数-Djava.io.tmpdir,重定向了tmp目录路径
230230

231231
4. 目标JVM使用启动参数-XX:+DisableAttachMechanism禁止了attach
232232

233+
如果实在没有办法attach,可以考虑在原目标进程中配置JMX启动参数,设定JMX的地址与端口,然后在vjtop中指定
234+
235+
目标进程的JVM参数:
236+
```
237+
-Djava.rmi.server.hostname=127.0.0.1 -Dcom.sun.management.jmxremote.port=7001 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.X=false -Dcom.sun.management.jmxremote.ssl=false
238+
```
239+
240+
vjtop的命令(since 1.0.3):
241+
242+
```
243+
./vjtop.sh -j 127.0.0.1:7001 <PID>
244+
```
245+
233246

234247
# 4. 改进点
235248

vjtop/src/main/java/com/vip/vjtools/vjtop/InteractiveTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private void changeThreadLimit() {
157157
if (threadLimit != app.view.threadLimit) {
158158
app.view.threadLimit = threadLimit;
159159
if (app.nextFlushTime() > 1) {
160-
tty.println(" Number of threads to display changed to " + threadLimit + "for next flush ("
160+
tty.println(" Number of threads to display changed to " + threadLimit + " for next flush ("
161161
+ app.nextFlushTime() + "s later)");
162162
}
163163
} else {

vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ private static OptionParser createOptionParser() {
4545
parser.acceptsAll(Arrays.asList(new String[] { "l", "limit" }),
4646
"Number of threads to display ( default to 10 threads)").withRequiredArg().ofType(Integer.class);
4747

48+
parser.acceptsAll(Arrays.asList(new String[] { "j", "jmxurl" }),
49+
"JMX url like 127.0.0.1:7001 when VM attach is not work").withRequiredArg().ofType(String.class);
50+
4851
// detail mode
4952
parser.accepts("cpu",
5053
"default mode in detail view, display thread cpu usage and sort by thread delta cpu time ");
@@ -59,7 +62,6 @@ private static OptionParser createOptionParser() {
5962

6063
public static void main(String[] args) {
6164
try {
62-
6365
// 1. create option parser
6466
OptionParser parser = createOptionParser();
6567
OptionSet optionSet = parser.parse(args);
@@ -72,9 +74,14 @@ public static void main(String[] args) {
7274
// 2. create vminfo
7375
String pid = parsePid(parser, optionSet);
7476

75-
VMInfo vminfo = VMInfo.processNewVM(pid);
77+
String jmxHostAndPort = null;
78+
if (optionSet.hasArgument("jmxurl")) {
79+
jmxHostAndPort = (String) optionSet.valueOf("jmxurl");
80+
}
81+
82+
VMInfo vminfo = VMInfo.processNewVM(pid, jmxHostAndPort);
7683
if (vminfo.state != VMInfoState.ATTACHED) {
77-
System.out.println("\nERROR: Could not attach to process, please find reason in README\n");
84+
System.out.println("\nERROR: Could not attach to process, see the solution in README\n");
7885
return;
7986
}
8087

vjtop/src/main/java/com/vip/vjtools/vjtop/VMInfo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ private VMInfo() {
104104
/**
105105
* 创建JMX连接并构造VMInfo实例
106106
*/
107-
public static VMInfo processNewVM(String pid) {
107+
public static VMInfo processNewVM(String pid, String jmxHostAndPort) {
108108
try {
109-
final JmxClient jmxClient = new JmxClient(pid);
110-
jmxClient.connect();
109+
final JmxClient jmxClient = new JmxClient();
110+
jmxClient.connect(pid, jmxHostAndPort);
111111

112112
// 注册JMXClient注销的钩子
113113
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {

vjtop/src/main/java/com/vip/vjtools/vjtop/data/jmx/JmxClient.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public class JmxClient {
6666

6767
private String pid;
6868

69-
private JMXServiceURL jmxUrl = null;
7069
private MBeanServerConnection mbsc = null;
7170
private SnapshotMBeanServerConnection server = null;
7271
private JMXConnector jmxc = null;
@@ -81,8 +80,7 @@ public class JmxClient {
8180
private JmxMemoryPoolManager memoryPoolManager = null;
8281
private JmxBufferPoolManager bufferPoolManager = null;
8382

84-
public JmxClient(String pid) throws IOException {
85-
this.pid = pid;
83+
public JmxClient() throws IOException {
8684
}
8785

8886
public void flush() {
@@ -91,13 +89,24 @@ public void flush() {
9189
}
9290
}
9391

94-
public void connect() throws Exception {
92+
public void connect(String pid, String jmxHostAndPort) throws Exception {
93+
this.pid = pid;
94+
95+
if (jmxHostAndPort != null) {
96+
JMXServiceURL jmxUrl = new JMXServiceURL(
97+
"service:jmx:rmi://" + jmxHostAndPort + "/jndi/rmi://" + jmxHostAndPort + "/jmxrmi");
98+
Map credentials = new HashMap(1);
99+
String[] creds = new String[] { null, null };
100+
credentials.put(JMXConnector.CREDENTIALS, creds);
95101

96-
// 如果jmx agent未启动,主动attach进JVM后加载
97-
String address = attachToGetConnectorAddress();
102+
this.jmxc = JMXConnectorFactory.connect(jmxUrl, credentials);
103+
} else {
104+
// 如果jmx agent未启动,主动attach进JVM后加载
105+
String address = attachToGetConnectorAddress();
98106

99-
this.jmxUrl = new JMXServiceURL(address);
100-
this.jmxc = JMXConnectorFactory.connect(jmxUrl);// NOSONAR
107+
JMXServiceURL jmxUrl = new JMXServiceURL(address);
108+
this.jmxc = JMXConnectorFactory.connect(jmxUrl);// NOSONAR
109+
}
101110

102111
this.mbsc = jmxc.getMBeanServerConnection();
103112
this.server = Snapshot.newSnapshot(mbsc);
@@ -230,7 +239,7 @@ private ObjectName createBeanName(String beanName) {
230239

231240
@Override
232241
public String toString() {
233-
return pid;
242+
return "JMX Client for PID:" + pid;
234243
}
235244

236245
/**

0 commit comments

Comments
 (0)