UUID中的TimeBasedGenerator

时间:2013-10-15 15:11:29

标签: java uuid

嗨,我有以下代码,它在行上给我错误:

uuidGenerator = Generators.timeBasedGenerator(nic, new UUIDTimer(new Random(), TimestampSynchronizer));

TimestampSynchronizer无法解析为变量。

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Random;
import java.util.UUID;
import javax.crypto.KeyGenerator;
import com.fasterxml.uuid.EthernetAddress;
import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.TimestampSynchronizer;
import com.fasterxml.uuid.UUIDTimer;
import com.fasterxml.uuid.ext.FileBasedTimestampSynchronizer;
import com.fasterxml.uuid.impl.TimeBasedGenerator;
import com.google.common.base.Charsets;
import com.google.common.io.BaseEncoding;
import com.google.gdata.util.common.util.*;

public class UUID_Test {

 public static void main(String[] args) {
  for (int i = 0; i < 10000; i++) {
   try {
    UUID_Test.uuidToBase32();
} catch (IOException e) {

    e.printStackTrace();
}
  }
 }

 private static String uuidToBase32() throws IOException 
 {

        EthernetAddress nic = EthernetAddress.fromInterface();
        TimeBasedGenerator uuidGenerator;   
            uuidGenerator = Generators.timeBasedGenerator(nic, new UUIDTimer(new Random(), TimestampSynchronizer)); 
        ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
        bb.putLong(uuidGenerator.generate().getMostSignificantBits());
        bb.putLong(uuidGenerator.generate().getLeastSignificantBits());

        return BaseEncoding.base32().encode(bb.array());


}
}

如何解决这个问题? 当我使用

   uuidGenerator = Generators.timeBasedGenerator(nic, new UUIDTimer(new Random(), new FileBasedTimestampSynchronizer()));

    Getting Exception
Exception in thread "main" java.nio.channels.OverlappingFileLockException

或者如果我写

 uuidGenerator = Generators.timeBasedGenerator(nic,new FileBasedTimestampSynchronizer(new File("d://abc"), new File("d://def")));

Getting Exception

WARNING: (file 'd:\abc') Missing or empty file, can not read timestamp value
WARNING: (file 'd:\def') Missing or empty file, can not read timestamp value
WARNING: Could not determine safe timer starting point: assuming current system time is acceptable
GNKOP7JVWII6HMAJ2S7NSZXSYE
Exception in thread "main" java.nio.channels.OverlappingFileLockException
    at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)

由于

修改

从评论复制到答案的代码:

我将代码放在静态块

static EthernetAddress nic = EthernetAddress.fromInterface();
static File f = new File("D://a.txt");
static File f1 = new File("D://f.txt");
static TimeBasedGenerator uuidGenerator;

static {
    try {
         uuidGenerator = Generators.timeBasedGenerator(nic, new UUIDTimer(new Random(), new FileBasedTimestampSynchronizer(f, f1)));
    }
    catch (IOException e) {
         e.printStackTrace();
    }
 }

它给出了例外

java.io.IOException: Failed to lock 'd://a.txt' (another JVM running UUIDGenerator?)

1 个答案:

答案 0 :(得分:0)

TimestampSynchronizer是一个非公共类,您不能在代码中使用它。

重叠锁定的问题来自此行

uuidGenerator = Generators.timeBasedGenerator(nic, new UUIDTimer(new Random(), TimestampSynchronizer)); 

您每次都在创建一个新生成器 - 以确保使用锁定文件的基于时间的UUID的唯一性。锁文件只能用于一个生成器。

解决方案:只创建一个计时器,甚至一个生成器,并在循环中重复使用它。