@Test public void testNextId() { IdSpaceIdGenerator idGenerator = new IdSpaceIdGenerator(); idGenerator.setIdServerUrl("http: CountDownLatch countDownLatch = new CountDownLatch(3); for (int i = 0; i < 3; i++) { new Thread(() -> { for (int j = 0; j < 10; j++) { String id = idGenerator.nextId("test"); System.out.println(Thread.currentThread().getName() + "===" + id); Thread.yield(); } countDownLatch.countDown(); }).start(); } try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } }
public String nextId(String spaceName) { if (!idSpaceHolder.containsKey(spaceName)) { initHolder(spaceName); } IdSpaceQueue idSpaces = idSpaceHolder.get(spaceName); if (idSpaces.isEmpty()) { idSpaces.lock.lock(); try { idSpaces.replenishCondition.await(15, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new IllegalArgumentException(String.format("Can not find the specified id space with space name \"%s\"", spaceName)); } finally { idSpaces.lock.unlock(); } } IdSpace idSpace = idSpaces.peek(); if (idSpace == null) { return null; } StringBuilder id = new StringBuilder(); if (idSpace.getPrefix() != null && !idSpace.getPrefix().equals("")) { id.append(idSpace.getPrefix()); } long seq = idSpace.getFrom().getAndIncrement(); String seqStr = String.valueOf(seq); if (seqStr.length() < idSpace.getSeqLength()) { for (int i = 0; i < idSpace.getSeqLength() - seqStr.length(); i++) { id.append("0"); } } id.append(seqStr); if (idSpace.getSuffix() != null && !idSpace.getSuffix().equals("")) { id.append(idSpace.getSuffix()); } if (idSpaces.size() <= 1 && idSpace.needReplenish()) { replenish(spaceName); } if (idSpace.isEmpty()) { idSpaces.poll(); } return id.toString(); }
IdSpaceIdGenerator { public String nextId(String spaceName) { if (!idSpaceHolder.containsKey(spaceName)) { initHolder(spaceName); } IdSpaceQueue idSpaces = idSpaceHolder.get(spaceName); if (idSpaces.isEmpty()) { idSpaces.lock.lock(); try { idSpaces.replenishCondition.await(15, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new IllegalArgumentException(String.format("Can not find the specified id space with space name \"%s\"", spaceName)); } finally { idSpaces.lock.unlock(); } } IdSpace idSpace = idSpaces.peek(); if (idSpace == null) { return null; } StringBuilder id = new StringBuilder(); if (idSpace.getPrefix() != null && !idSpace.getPrefix().equals("")) { id.append(idSpace.getPrefix()); } long seq = idSpace.getFrom().getAndIncrement(); String seqStr = String.valueOf(seq); if (seqStr.length() < idSpace.getSeqLength()) { for (int i = 0; i < idSpace.getSeqLength() - seqStr.length(); i++) { id.append("0"); } } id.append(seqStr); if (idSpace.getSuffix() != null && !idSpace.getSuffix().equals("")) { id.append(idSpace.getSuffix()); } if (idSpaces.size() <= 1 && idSpace.needReplenish()) { replenish(spaceName); } if (idSpace.isEmpty()) { idSpaces.poll(); } return id.toString(); } }
IdSpaceIdGenerator { public String nextId(String spaceName) { if (!idSpaceHolder.containsKey(spaceName)) { initHolder(spaceName); } IdSpaceQueue idSpaces = idSpaceHolder.get(spaceName); if (idSpaces.isEmpty()) { idSpaces.lock.lock(); try { idSpaces.replenishCondition.await(15, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new IllegalArgumentException(String.format("Can not find the specified id space with space name \"%s\"", spaceName)); } finally { idSpaces.lock.unlock(); } } IdSpace idSpace = idSpaces.peek(); if (idSpace == null) { return null; } StringBuilder id = new StringBuilder(); if (idSpace.getPrefix() != null && !idSpace.getPrefix().equals("")) { id.append(idSpace.getPrefix()); } long seq = idSpace.getFrom().getAndIncrement(); String seqStr = String.valueOf(seq); if (seqStr.length() < idSpace.getSeqLength()) { for (int i = 0; i < idSpace.getSeqLength() - seqStr.length(); i++) { id.append("0"); } } id.append(seqStr); if (idSpace.getSuffix() != null && !idSpace.getSuffix().equals("")) { id.append(idSpace.getSuffix()); } if (idSpaces.size() <= 1 && idSpace.needReplenish()) { replenish(spaceName); } if (idSpace.isEmpty()) { idSpaces.poll(); } return id.toString(); }  }
IdSpaceIdGenerator { public String nextId(String spaceName) { if (!idSpaceHolder.containsKey(spaceName)) { initHolder(spaceName); } IdSpaceQueue idSpaces = idSpaceHolder.get(spaceName); if (idSpaces.isEmpty()) { idSpaces.lock.lock(); try { idSpaces.replenishCondition.await(15, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new IllegalArgumentException(String.format("Can not find the specified id space with space name \"%s\"", spaceName)); } finally { idSpaces.lock.unlock(); } } IdSpace idSpace = idSpaces.peek(); if (idSpace == null) { return null; } StringBuilder id = new StringBuilder(); if (idSpace.getPrefix() != null && !idSpace.getPrefix().equals("")) { id.append(idSpace.getPrefix()); } long seq = idSpace.getFrom().getAndIncrement(); String seqStr = String.valueOf(seq); if (seqStr.length() < idSpace.getSeqLength()) { for (int i = 0; i < idSpace.getSeqLength() - seqStr.length(); i++) { id.append("0"); } } id.append(seqStr); if (idSpace.getSuffix() != null && !idSpace.getSuffix().equals("")) { id.append(idSpace.getSuffix()); } if (idSpaces.size() <= 1 && idSpace.needReplenish()) { replenish(spaceName); } if (idSpace.isEmpty()) { idSpaces.poll(); } return id.toString(); }  void setIdServerUrl(String idServerUrl); String nextId(String spaceName); }
IdSpaceIdGenerator { public String nextId(String spaceName) { if (!idSpaceHolder.containsKey(spaceName)) { initHolder(spaceName); } IdSpaceQueue idSpaces = idSpaceHolder.get(spaceName); if (idSpaces.isEmpty()) { idSpaces.lock.lock(); try { idSpaces.replenishCondition.await(15, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new IllegalArgumentException(String.format("Can not find the specified id space with space name \"%s\"", spaceName)); } finally { idSpaces.lock.unlock(); } } IdSpace idSpace = idSpaces.peek(); if (idSpace == null) { return null; } StringBuilder id = new StringBuilder(); if (idSpace.getPrefix() != null && !idSpace.getPrefix().equals("")) { id.append(idSpace.getPrefix()); } long seq = idSpace.getFrom().getAndIncrement(); String seqStr = String.valueOf(seq); if (seqStr.length() < idSpace.getSeqLength()) { for (int i = 0; i < idSpace.getSeqLength() - seqStr.length(); i++) { id.append("0"); } } id.append(seqStr); if (idSpace.getSuffix() != null && !idSpace.getSuffix().equals("")) { id.append(idSpace.getSuffix()); } if (idSpaces.size() <= 1 && idSpace.needReplenish()) { replenish(spaceName); } if (idSpace.isEmpty()) { idSpaces.poll(); } return id.toString(); }  void setIdServerUrl(String idServerUrl); String nextId(String spaceName);  }