0

嗨,我有下面的代码,这给了我错误:

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

LikeTimestampSynchronizer无法解析为变量。

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?)
4

1 回答 1

0

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

你的重叠锁的问题来自这条线

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

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

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

于 2013-10-15T16:33:41.697 回答