From 2aedd0d33ec48b6d4469b4f847b46a232ea1c164 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Sun, 20 Jun 2010 18:52:36 +0000 Subject: [PATCH] INT-1188, change the second argument from combination or MAC+nano to MAC+processId --- .../core/TimeBasedUUIDGenerator.java | 27 ++++++++++--------- .../core/TimeBasedUUIDGeneratorTests.java | 2 ++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/core/TimeBasedUUIDGenerator.java b/spring-integration-core/src/main/java/org/springframework/integration/core/TimeBasedUUIDGenerator.java index 3b6811f4c5..0fe313509e 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/core/TimeBasedUUIDGenerator.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/core/TimeBasedUUIDGenerator.java @@ -15,6 +15,7 @@ */ package org.springframework.integration.core; +import java.lang.management.ManagementFactory; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.UUID; @@ -32,7 +33,10 @@ import org.apache.commons.logging.LogFactory; */ class TimeBasedUUIDGenerator { public static final Object lock = new Object(); + private static boolean canNotDetermineMac = true; private static long lastTime; + private static long processId = Long.valueOf(ManagementFactory.getRuntimeMXBean().getName().hashCode()); + private static long pidMac; private static final Log logger = LogFactory.getLog(TimeBasedUUIDGenerator.class); private static long macAddressAsLong = 0; @@ -53,6 +57,13 @@ class TimeBasedUUIDGenerator { } catch (Exception e) { e.printStackTrace(); } + pidMac = macAddressAsLong; + if (pidMac == 0){ + pidMac = (processId << 32) | System.currentTimeMillis(); + } else { + canNotDetermineMac = false; + pidMac = (processId << 32) | macAddressAsLong; + } } /** @@ -71,7 +82,6 @@ class TimeBasedUUIDGenerator { */ public final static UUID generateIdFromTimestamp(long currentTimeMillis){ long time; - long macNanoTime; synchronized (lock) { if (currentTimeMillis > lastTime) { lastTime = currentTimeMillis; @@ -90,17 +100,10 @@ class TimeBasedUUIDGenerator { // hi Time time |= 0x1000 | ((currentTimeMillis >> 48) & 0x0FFF); // version 1 - - macNanoTime = macAddressAsLong; - if (macNanoTime == 0){ - logger.warn("Can not determine machine's MAC address. Will use System.nanoTime() for UUID generation, however there is a slim chance of it not being globally unique"); - macNanoTime = System.nanoTime(); - } else { - //considering the type of time returned by nanoTime, this will ensure that - // even if more then one process is running on the same machine, the id is still unique - macNanoTime |= System.nanoTime() & 0xFF; - macNanoTime <<= 8; + if (canNotDetermineMac){ + logger.warn("UUID generation process was not able to determine your MAC address. There is a slight chance for this UUID not to be globally unique"); } - return new UUID(time, macNanoTime); + + return new UUID(time, pidMac); } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/core/TimeBasedUUIDGeneratorTests.java b/spring-integration-core/src/test/java/org/springframework/integration/core/TimeBasedUUIDGeneratorTests.java index 8ad19dff03..baba961110 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/core/TimeBasedUUIDGeneratorTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/core/TimeBasedUUIDGeneratorTests.java @@ -32,9 +32,11 @@ public class TimeBasedUUIDGeneratorTests { @Test public void testGreaterThen(){ + UUID id = TimeBasedUUIDGenerator.generateId(); for (int i = 0; i < 1000; i++) { UUID newId = TimeBasedUUIDGenerator.generateId(); + //System.out.println(newId); // tests, that newly created UUID is always greater then the previous one. Assert.assertTrue(newId.compareTo(id) == 1); id = newId;