提交 764913d7 编写于 作者: D dsamersoff

8004213: JDP packet needs pid, broadcast interval and rmi server hostname fields

Summary: Add some extra fileds to jdp packet
Reviewed-by: allwin, sla, hirt
上级 2505747c
...@@ -29,6 +29,12 @@ import java.net.InetAddress; ...@@ -29,6 +29,12 @@ import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.UUID; import java.util.UUID;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import sun.management.VMManagement;
/** /**
* JdpController is responsible to create and manage a broadcast loop * JdpController is responsible to create and manage a broadcast loop
* *
...@@ -126,6 +132,25 @@ public final class JdpController { ...@@ -126,6 +132,25 @@ public final class JdpController {
} }
} }
// Get the process id of the current running Java process
private static Integer getProcessId() {
try {
// Get the current process id using a reflection hack
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
Field jvm = runtime.getClass().getDeclaredField("jvm");
jvm.setAccessible(true);
VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime);
Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId");
pid_method.setAccessible(true);
Integer pid = (Integer) pid_method.invoke(mgmt);
return pid;
} catch(Exception ex) {
return null;
}
}
/** /**
* Starts discovery service * Starts discovery service
* *
...@@ -173,6 +198,20 @@ public final class JdpController { ...@@ -173,6 +198,20 @@ public final class JdpController {
// it the key is skipped. PacketWriter is responsible to skip keys having null value. // it the key is skipped. PacketWriter is responsible to skip keys having null value.
packet.setInstanceName(instanceName); packet.setInstanceName(instanceName);
// Set rmi server hostname if it explicitly specified by user with
// java.rmi.server.hostname
String rmiHostname = System.getProperty("java.rmi.server.hostname");
packet.setRmiHostname(rmiHostname);
// Set broadcast interval
packet.setBroadcastInterval(new Integer(pause).toString());
// Set process id
Integer pid = getProcessId();
if (pid != null) {
packet.setProcessId(pid.toString());
}
JdpBroadcaster bcast = new JdpBroadcaster(address, sourceAddress, port, ttl); JdpBroadcaster bcast = new JdpBroadcaster(address, sourceAddress, port, ttl);
// Stop discovery service if it's already running // Stop discovery service if it's already running
......
...@@ -64,11 +64,27 @@ public final class JdpJmxPacket ...@@ -64,11 +64,27 @@ public final class JdpJmxPacket
* Name of Java instance * Name of Java instance
*/ */
public final static String INSTANCE_NAME_KEY = "INSTANCE_NAME"; public final static String INSTANCE_NAME_KEY = "INSTANCE_NAME";
/**
* PID of java process, optional presented if it could be obtained
*/
public final static String PROCESS_ID_KEY = "PROCESS_ID";
/**
* Hostname of rmi server, optional presented if user overrides rmi server
* hostname by java.rmi.server.hostname property
*/
public final static String RMI_HOSTNAME_KEY = "RMI_HOSTNAME";
/**
* Configured broadcast interval, optional
*/
public final static String BROADCAST_INTERVAL_KEY = "BROADCAST_INTERVAL";
private UUID id; private UUID id;
private String mainClass; private String mainClass;
private String jmxServiceUrl; private String jmxServiceUrl;
private String instanceName; private String instanceName;
private String processId;
private String rmiHostname;
private String broadcastInterval;
/** /**
* Create new instance from user provided data. Set mandatory fields * Create new instance from user provided data. Set mandatory fields
...@@ -99,6 +115,9 @@ public final class JdpJmxPacket ...@@ -99,6 +115,9 @@ public final class JdpJmxPacket
this.jmxServiceUrl = p.get(JMX_SERVICE_URL_KEY); this.jmxServiceUrl = p.get(JMX_SERVICE_URL_KEY);
this.mainClass = p.get(MAIN_CLASS_KEY); this.mainClass = p.get(MAIN_CLASS_KEY);
this.instanceName = p.get(INSTANCE_NAME_KEY); this.instanceName = p.get(INSTANCE_NAME_KEY);
this.processId = p.get(PROCESS_ID_KEY);
this.rmiHostname = p.get(RMI_HOSTNAME_KEY);
this.broadcastInterval = p.get(BROADCAST_INTERVAL_KEY);
} }
/** /**
...@@ -150,6 +169,30 @@ public final class JdpJmxPacket ...@@ -150,6 +169,30 @@ public final class JdpJmxPacket
return instanceName; return instanceName;
} }
public String getProcessId() {
return processId;
}
public void setProcessId(String processId) {
this.processId = processId;
}
public String getRmiHostname() {
return rmiHostname;
}
public void setRmiHostname(String rmiHostname) {
this.rmiHostname = rmiHostname;
}
public String getBroadcastInterval() {
return broadcastInterval;
}
public void setBroadcastInterval(String broadcastInterval) {
this.broadcastInterval = broadcastInterval;
}
/** /**
* *
* @return assembled packet ready to be sent across a Net * @return assembled packet ready to be sent across a Net
...@@ -164,6 +207,10 @@ public final class JdpJmxPacket ...@@ -164,6 +207,10 @@ public final class JdpJmxPacket
writer.addEntry(MAIN_CLASS_KEY, mainClass); writer.addEntry(MAIN_CLASS_KEY, mainClass);
writer.addEntry(JMX_SERVICE_URL_KEY, jmxServiceUrl); writer.addEntry(JMX_SERVICE_URL_KEY, jmxServiceUrl);
writer.addEntry(INSTANCE_NAME_KEY, instanceName); writer.addEntry(INSTANCE_NAME_KEY, instanceName);
writer.addEntry(PROCESS_ID_KEY, processId);
writer.addEntry(RMI_HOSTNAME_KEY, rmiHostname);
writer.addEntry(BROADCAST_INTERVAL_KEY, broadcastInterval);
return writer.getPacketBytes(); return writer.getPacketBytes();
} }
......
/* /*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -35,8 +35,10 @@ import java.nio.channels.SelectionKey; ...@@ -35,8 +35,10 @@ import java.nio.channels.SelectionKey;
import java.nio.channels.Selector; import java.nio.channels.Selector;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Map;
import sun.management.jdp.JdpException; import sun.management.jdp.JdpException;
import sun.management.jdp.JdpJmxPacket; import sun.management.jdp.JdpJmxPacket;
import sun.management.jdp.JdpPacketReader;
public class JdpClient { public class JdpClient {
...@@ -47,6 +49,31 @@ public class JdpClient { ...@@ -47,6 +49,31 @@ public class JdpClient {
private static int maxPacketCount = 1; private static int maxPacketCount = 1;
private static int maxEmptyPacketCount = 10; private static int maxEmptyPacketCount = 10;
private void get(Map<?,?> map, String key)
throws JdpException {
if (map.get(key) == null) {
throw new JdpException("Test failed, packet field " + key + " missed");
}
}
private void checkFieldPresence(JdpJmxPacket p)
throws IOException, JdpException {
byte[] b = p.getPacketData();
JdpPacketReader reader = new JdpPacketReader(b);
Map<String,String> pMap = reader.getDiscoveryDataAsMap();
get(pMap, JdpJmxPacket.UUID_KEY);
get(pMap, JdpJmxPacket.MAIN_CLASS_KEY);
get(pMap, JdpJmxPacket.JMX_SERVICE_URL_KEY);
// get(pMap, JdpJmxPacket.INSTANCE_NAME_KEY);
get(pMap, JdpJmxPacket.PROCESS_ID_KEY);
get(pMap, JdpJmxPacket.BROADCAST_INTERVAL_KEY);
get(pMap, JdpJmxPacket.RMI_HOSTNAME_KEY);
}
PacketListener(DatagramChannel channel) { PacketListener(DatagramChannel channel) {
this.channel = channel; this.channel = channel;
...@@ -67,6 +94,8 @@ public class JdpClient { ...@@ -67,6 +94,8 @@ public class JdpClient {
try { try {
while (true) { while (true) {
// Use tcpdump -U -w - -s 1400 -c 2 -vv port 7095
// to verify that correct packet being sent
sel.selectedKeys().clear(); sel.selectedKeys().clear();
buf.rewind(); buf.rewind();
...@@ -87,10 +116,10 @@ public class JdpClient { ...@@ -87,10 +116,10 @@ public class JdpClient {
buf.flip(); buf.flip();
byte[] dgramData = new byte[buf.remaining()]; byte[] dgramData = new byte[buf.remaining()];
buf.get(dgramData); buf.get(dgramData);
try { try {
JdpJmxPacket packet = new JdpJmxPacket(dgramData); JdpJmxPacket packet = new JdpJmxPacket(dgramData);
JdpDoSomething.printJdpPacket(packet); JdpDoSomething.printJdpPacket(packet);
checkFieldPresence(packet);
if(++count > maxPacketCount){ if(++count > maxPacketCount){
break; break;
} }
......
/* /*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -32,7 +32,7 @@ import sun.management.jdp.JdpException; ...@@ -32,7 +32,7 @@ import sun.management.jdp.JdpException;
public class JdpDoSomething { public class JdpDoSomething {
private static final String lockFileName = "JdpDoSomething.lck"; private static final String lockFileName = "JdpDoSomething.lck";
private static final boolean verbose=false; private static final boolean verbose = false;
public static boolean getVerbose(){ public static boolean getVerbose(){
return verbose; return verbose;
...@@ -52,6 +52,9 @@ public class JdpDoSomething { ...@@ -52,6 +52,9 @@ public class JdpDoSomething {
System.out.println("Jmx: " + p.getJmxServiceUrl()); System.out.println("Jmx: " + p.getJmxServiceUrl());
System.out.println("Main: " + p.getMainClass()); System.out.println("Main: " + p.getMainClass());
System.out.println("InstanceName: " + p.getInstanceName()); System.out.println("InstanceName: " + p.getInstanceName());
System.out.println("ProccessId: " + p.getProcessId());
System.out.println("BroadcastInterval: " + p.getBroadcastInterval());
System.out.println("Rmi Hostname: " + p.getRmiHostname());
System.out.flush(); System.out.flush();
} }
......
#!/bin/sh -x #!/bin/sh -x
# Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. # Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
# #
# This code is free software; you can redistribute it and/or modify it # This code is free software; you can redistribute it and/or modify it
...@@ -135,7 +135,6 @@ _testme(){ ...@@ -135,7 +135,6 @@ _testme(){
-Dcom.sun.management.jdp.port=${_port} \ -Dcom.sun.management.jdp.port=${_port} \
-Dcom.sun.management.jdp.address=${_ip} \ -Dcom.sun.management.jdp.address=${_ip} \
JdpClient JdpClient
} }
...@@ -158,6 +157,8 @@ test_01(){ ...@@ -158,6 +157,8 @@ test_01(){
_app_start JdpUnitTest \ _app_start JdpUnitTest \
-Dcom.sun.management.jdp.port=${_port} \ -Dcom.sun.management.jdp.port=${_port} \
-Dcom.sun.management.jdp.address=${_ip} \ -Dcom.sun.management.jdp.address=${_ip} \
-Dcom.sun.management.jdp.name=testme \
-Djava.rmi.server.hostname=localhost \
-Dcom.sun.management.jdp.pause=5 -Dcom.sun.management.jdp.pause=5
res=`_testme` res=`_testme`
...@@ -182,6 +183,8 @@ test_02(){ ...@@ -182,6 +183,8 @@ test_02(){
-Dcom.sun.management.jdp.port=${_port} \ -Dcom.sun.management.jdp.port=${_port} \
-Dcom.sun.management.jdp.address=${_ip} \ -Dcom.sun.management.jdp.address=${_ip} \
-Dcom.sun.management.jdp.pause=5 \ -Dcom.sun.management.jdp.pause=5 \
-Dcom.sun.management.jdp.name=testme \
-Djava.rmi.server.hostname=localhost \
-Dcom.sun.management.jmxremote.port=${_jmxport} \ -Dcom.sun.management.jmxremote.port=${_jmxport} \
-Dcom.sun.management.jmxremote.authenticate=false \ -Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.ssl=false
...@@ -234,6 +237,8 @@ test_04(){ ...@@ -234,6 +237,8 @@ test_04(){
_app_start JdpDoSomething \ _app_start JdpDoSomething \
-Dcom.sun.management.jmxremote.autodiscovery=true \ -Dcom.sun.management.jmxremote.autodiscovery=true \
-Dcom.sun.management.jdp.name=testme \
-Djava.rmi.server.hostname=localhost \
-Dcom.sun.management.jmxremote.port=${_jmxport} \ -Dcom.sun.management.jmxremote.port=${_jmxport} \
-Dcom.sun.management.jmxremote.authenticate=false \ -Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.ssl=false
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册