diff --git a/spring-integration-core/src/main/java/org/springframework/integration/core/MessageHeaders.java b/spring-integration-core/src/main/java/org/springframework/integration/core/MessageHeaders.java index 26762a3acb..83d721ee5a 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/core/MessageHeaders.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/core/MessageHeaders.java @@ -31,7 +31,6 @@ import java.util.UUID; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - import org.springframework.integration.history.MessageHistory; /** @@ -78,7 +77,7 @@ public final class MessageHeaders implements Map, Serializable { public MessageHeaders(Map headers) { this.headers = (headers != null) ? new HashMap(headers) : new HashMap(); - this.headers.put(ID, UUID.randomUUID()); + this.headers.put(ID, TimeBasedUUIDGenerator.generateId()); this.headers.put(TIMESTAMP, new Long(System.currentTimeMillis())); if (this.headers.get(HISTORY) == null) { this.headers.put(HISTORY, new MessageHistory()); 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 new file mode 100644 index 0000000000..b24a41f0b3 --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/core/TimeBasedUUIDGenerator.java @@ -0,0 +1,69 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.core; + +import java.util.UUID; + +/** + * Will generate time-based UUID (version 1 UUID). + * This will allow Message ID to be unique but also contain an + * embedded timestamp which could be retrieved via UUID.timestamp() + * + * @author Oleg Zhurakousky + * @since 2.0 + */ +class TimeBasedUUIDGenerator { + public static final Object lock = new Object(); + private static long lastTime; + + /** + * Will generate unique time based UUID where the next UUID is + * always greater then the previous. + * + * @return + */ + public final static UUID generateId() { + return generateIdFromTimestamp(System.currentTimeMillis()); + } + /** + * + * @param currentTimeMillis + * @return + */ + public final static UUID generateIdFromTimestamp(long currentTimeMillis){ + long time; + synchronized (lock) { + if (currentTimeMillis > lastTime) { + lastTime = currentTimeMillis; + } else { + currentTimeMillis = ++lastTime; + } + } + + time = currentTimeMillis; + + // low Time + time = currentTimeMillis << 32; + + // mid Time + time |= (currentTimeMillis & 0xFFFF00000000L) >> 16; + + // hi Time + time |= 0x1000 | ((currentTimeMillis >> 48) & 0x0FFF); // version 1 + + return new UUID(time, System.nanoTime()); + } +}