MessageIdFactory { public void initialize(String domain) throws IOException { m_domain = domain; m_ipAddress = genIpHex(); m_processID = getProcessID(); if( m_markFile != null ) { synchronized (this) { close(); } } File mark = createMarkFile(domain); m_markFile = new RandomAccessFile(mark, "rw"); m_markChannel = m_markFile.getChannel(); m_byteBuffer = m_markChannel.map(MapMode.READ_WRITE, 0, 1024 * 1024L); m_idPrefix = initIdPrefix(getTimestamp(), false); m_idPrefixOfMultiMode = initIdPrefix(getTimestamp(), true); if (m_byteBuffer.limit() > 0) { try { long lastTimestamp = m_byteBuffer.getLong(); int index = m_byteBuffer.getInt(); if (lastTimestamp == m_timestamp) { m_index = new AtomicInteger(index + 1000); int mapLength = m_byteBuffer.getInt(); for (int i = 0; i < mapLength; i++) { int domainLength = m_byteBuffer.getInt(); byte[] domainArray = new byte[domainLength]; m_byteBuffer.get(domainArray); int value = m_byteBuffer.getInt(); m_map.put(new String(domainArray), new AtomicInteger(value + 1000)); } } else { m_index = new AtomicInteger(0); } } catch (Exception e) { e.printStackTrace(); m_retry++; if (m_retry == 1) { mark.delete(); initialize(domain); } } } saveMark(); if( !shutdownHookOn ) { synchronized (this) { if( !shutdownHookOn ) { Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { close(); } }); } } shutdownHookOn = true; } }  void close(); String getNextId(); String getNextId(String domain); void initialize(String domain); int getIndex(); synchronized void saveMark(); void setDomain(String domain); void setIpAddress(String ipAddress); static final long HOUR; }
@Test public void testInit() throws IOException { m_factory.initialize("testInit"); }