All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.datahub.client.util.ClientUtils Maven / Gradle / Ivy

The newest version!
package com.aliyun.datahub.client.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;

public abstract class ClientUtils {
    private static final String clientId;
    private static final String ipAddress;
    private static final AtomicInteger id = new AtomicInteger();

    static {
        ipAddress = genIpAddress();
        clientId = genClientId();
    }

    public static String getClientId() {
        return clientId;
    }

    public static String getIpAddress() {
        return ipAddress;
    }

    public static String genClientTraceId(String traceIdPrefix) {
        return traceIdPrefix + String.format("%016x", ((System.currentTimeMillis() << 16) | (id.getAndIncrement() & 0xffff)));
    }

    private static String genClientId() {
        String processId = genProcessId();
        // version
        String path = "META-INF/maven/com.aliyun.datahub/aliyun-sdk-datahub/pom.properties";
        Properties p = new Properties();
        try {
            p.load(ClientUtils.class.getClassLoader().getResourceAsStream(path));
        } catch (Throwable ignored) {
        }

        String versionNumber = p.getProperty("version", "Unknown");
        return String.format(Locale.ENGLISH, "%s@%s-(Java-%s)", ipAddress, processId, versionNumber);
    }

    private static String genIpAddress() {
        String ip = null;
        Enumeration interfaces = null;
        try {
            interfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            throw new RuntimeException(e);
        }

        while (interfaces.hasMoreElements()) {
            NetworkInterface element = interfaces.nextElement();
            Enumeration addresses = element.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();
                if (address.isLoopbackAddress()) {
                    continue;
                }
                if (address instanceof Inet4Address) {
                    ip = address.getHostAddress();
                }
            }
        }
        return ip;
    }

    private static String genProcessId() {
        String processName = java.lang.management.ManagementFactory
                .getRuntimeMXBean().getName();
        return processName.substring(0, processName.indexOf('@'));
    }

    public static String extractExceptionCode(Exception ex) {
        try {
            String name = ex.getClass().getSimpleName();
            return name.replace("Exception", "");
        } catch (Exception e) {
            return "null";
        }
    }

    public static void silentSleep(long ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            // nothing
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy