INT-1903 added support for allowing to use custom MessageIdGenerationStrategy
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd">
|
||||
|
||||
<bean id="idGenerator" class="org.mockito.Mockito" factory-method="spy">
|
||||
<constructor-arg>
|
||||
<bean class="org.springframework.integration.core.MessageIdGenerationTests.SampleIdGenerator"/>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<int:channel id="input"/>
|
||||
|
||||
<int:service-activator input-channel="input" output-channel="nextA" expression="'nextA'"/>
|
||||
|
||||
<int:service-activator input-channel="nextA" output-channel="nextB" expression="'nextB'"/>
|
||||
|
||||
<int:service-activator input-channel="nextB" output-channel="nextC" expression="'nextC'"/>
|
||||
|
||||
<int:logging-channel-adapter id="nextC" level="WARN" log-full-message="true"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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 static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.MessageChannel;
|
||||
import org.springframework.integration.MessageHeaders;
|
||||
import org.springframework.integration.MessageHeaders.MessageIdGenerationStrategy;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.util.StopWatch;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class MessageIdGenerationTests {
|
||||
|
||||
@Test
|
||||
public void testCustomIdGeneration(){
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("MessageIdGenerationTests-context.xml", this.getClass());
|
||||
MessageIdGenerationStrategy idGenerator = context.getBean("idGenerator", MessageIdGenerationStrategy.class);
|
||||
MessageChannel inputChannel = context.getBean("input", MessageChannel.class);
|
||||
inputChannel.send(new GenericMessage<Integer>(0));
|
||||
verify(idGenerator, times(4)).generateId();
|
||||
reset(idGenerator);
|
||||
context.destroy();
|
||||
new GenericMessage<Integer>(0);
|
||||
verify(idGenerator, times(0)).generateId();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void performanceTest(){
|
||||
int times = 1000000;
|
||||
StopWatch watch = new StopWatch();
|
||||
watch.start();
|
||||
for (int i = 0; i < times; i++) {
|
||||
new GenericMessage<Integer>(0);
|
||||
}
|
||||
watch.stop();
|
||||
double defaultGeneratorElapsedTime = watch.getTotalTimeSeconds();
|
||||
|
||||
MessageHeaders.setMessageIdGenerationStrategy(new MessageIdGenerationStrategy() {
|
||||
public UUID generateId() {
|
||||
return TimeBasedUUIDGenerator.generateId();
|
||||
}
|
||||
});
|
||||
watch = new StopWatch();
|
||||
watch.start();
|
||||
for (int i = 0; i < times; i++) {
|
||||
new GenericMessage<Integer>(0);
|
||||
}
|
||||
watch.stop();
|
||||
double timebasedGeneratorElapsedTime = watch.getTotalTimeSeconds();
|
||||
|
||||
System.out.println("Generated " + times + " messages using default UUID generator " +
|
||||
"in " + defaultGeneratorElapsedTime + " seconds");
|
||||
System.out.println("Generated " + times + " messages using Timebased UUID generator " +
|
||||
"in " + timebasedGeneratorElapsedTime + " seconds");
|
||||
|
||||
System.out.println(defaultGeneratorElapsedTime/timebasedGeneratorElapsedTime);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static class SampleIdGenerator implements MessageIdGenerationStrategy {
|
||||
|
||||
public UUID generateId() {
|
||||
return UUID.nameUUIDFromBytes(((System.currentTimeMillis() - System.nanoTime()) + "").getBytes());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright 2002-2011 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.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
class TimeBasedUUIDGenerator {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(TimeBasedUUIDGenerator.class.getName());
|
||||
|
||||
public static final Object lock = new Object();
|
||||
|
||||
private static boolean canNotDetermineMac = true;
|
||||
private static long lastTime;
|
||||
private static long clockSequence = 0;
|
||||
private static final long macAddress = getMac();
|
||||
|
||||
/**
|
||||
* Will generate unique time based UUID where the next UUID is
|
||||
* always greater then the previous.
|
||||
*/
|
||||
public final static UUID generateId() {
|
||||
return generateIdFromTimestamp(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public final static UUID generateIdFromTimestamp(long currentTimeMillis){
|
||||
long time;
|
||||
|
||||
synchronized (lock) {
|
||||
if (currentTimeMillis > lastTime) {
|
||||
lastTime = currentTimeMillis;
|
||||
clockSequence = 0;
|
||||
} else {
|
||||
++clockSequence;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
time = currentTimeMillis;
|
||||
|
||||
// low Time
|
||||
time = currentTimeMillis << 32;
|
||||
|
||||
// mid Time
|
||||
time |= ((currentTimeMillis & 0xFFFF00000000L) >> 16);
|
||||
|
||||
// hi Time
|
||||
time |= 0x1000 | ((currentTimeMillis >> 48) & 0x0FFF); // version 1
|
||||
|
||||
long clock_seq_hi_and_reserved = clockSequence;
|
||||
|
||||
clock_seq_hi_and_reserved <<=48;
|
||||
|
||||
long cls = 0 | clock_seq_hi_and_reserved;
|
||||
|
||||
long lsb = cls | macAddress;
|
||||
if (canNotDetermineMac){
|
||||
logger.warning("UUID generation process was not able to determine your MAC address. Returning random UUID (non version 1 UUID)");
|
||||
return UUID.randomUUID();
|
||||
} else {
|
||||
return new UUID(time, lsb);
|
||||
}
|
||||
}
|
||||
private static final long getMac(){
|
||||
long macAddressAsLong = 0;
|
||||
try {
|
||||
InetAddress address = InetAddress.getLocalHost();
|
||||
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
|
||||
if (ni != null) {
|
||||
byte[] mac = ni.getHardwareAddress();
|
||||
//Converts array of unsigned bytes to an long
|
||||
if (mac != null) {
|
||||
for (int i = 0; i < mac.length; i++) {
|
||||
macAddressAsLong <<= 8;
|
||||
macAddressAsLong ^= (long)mac[i] & 0xFF;
|
||||
}
|
||||
}
|
||||
}
|
||||
canNotDetermineMac = false;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return macAddressAsLong;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user