会话msgId
每次会话某段时间内唯一,int类型,每次请求和响应msgId相同。
JAVA参考代码
import java.io.IOException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class NetworkUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(NetworkUtil.class);
public static String getHostAddress() {
return getHostAddress(getLocalAddress());
}
public static String getHostAddress(byte[] addr) {
try {
return InetAddress.getByAddress(addr).getHostAddress();
} catch (UnknownHostException ex) {
LOGGER.error("Unexpected exception: ", ex);
}
return InetAddress.getLoopbackAddress().getHostAddress();
}
public static byte[] getLocalAddress() {
try {
for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces
.hasMoreElements();) {
NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()
|| networkInterface.isPointToPoint()) {
continue;
}
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
if (addr instanceof Inet6Address) {
continue;
}
return addr.getAddress();
}
}
} catch (IOException ex) {
LOGGER.error("Unexpected exception: ", ex);
}
return InetAddress.getLoopbackAddress().getAddress();
}
}
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
public class MsgIdUtil {
private MsgIdUtil(){
}
private static AtomicInteger atomicInt = new AtomicInteger(0);
private static int NET_WORK = 0;
static{
String ip = NetworkUtil.getHostAddress();
NET_WORK=Integer.valueOf(ip.substring(ip.lastIndexOf(".")+1));
}
public static Integer createMsgId() {
return (getAtomicInt()<<24) | (getHostInt()<<16)
| (getTimeInt()<<6) | getRandomInt();
}
private synchronized static int getAtomicInt() {
if(atomicInt.get()>=127){
atomicInt.set(0);
}
return atomicInt.getAndIncrement();
}
private static int getHostInt() {
return ThreadLocalRandom.current().nextInt(: 255);
}
private static int getTimeInt() {
return (int)(System.currentTimeMillis()%1000);
}
private static int getRandomInt() {
return ThreadLocalRandom.current().nextInt(63);
}
public static void main(String[] args) {
for(int i=0;i<150;i++){
System.out.println(MsgIdUtil.createMsgId());
}
}
}