import java.net.InetAddress; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.lang.StringUtils; public class LogIdUtil { private static String machineIp; private static final transient AtomicInteger ID = new AtomicInteger(new Random().nextInt(99999999)); private static final DateFormat df = new SimpleDateFormat("yyMMddHHmmss"); static { try { String[] ips = InetAddress.getLocalHost().getHostAddress().split("\\."); machineIp = StringUtils.leftPad(Integer.toHexString(Integer.parseInt(ips[2])), 2, '0') + StringUtils.leftPad(Integer.toHexString(Integer.parseInt(ips[3])), 2, '0'); } catch (Exception e) { throw new RuntimeException("Init machineIp failed", e); } } /** * 根据系统ID、日期、IP地址、自增数生成LOGID * @param systemId 四位系统ID * @return */ public static String getId() { synchronized (LogIdUtil.class) { ID.compareAndSet(999999999, 0); return df.format(new Date()) + machineIp + StringUtils.leftPad(String.valueOf(ID.incrementAndGet()), 10, '0'); } } }
根据系统ID、日期、IP地址、自增数生成LOGID
猜你喜欢
转载自nxdjava.iteye.com/blog/2390248
今日推荐
周排行