@Test public void testGetNextMessageIdFailsIfAllMidsAreInUse() throws Exception { NetworkConfig config = network.createStandardTestConfig(); MapBasedMessageIdTracker tracker = new MapBasedMessageIdTracker(INITIAL_MID, 0, TOTAL_NO_OF_MIDS, config); for (int i = 0; i < TOTAL_NO_OF_MIDS; i++) { tracker.getNextMessageId(); } int mid = tracker.getNextMessageId(); assertThat(mid, is(-1)); }
public int getNextMessageId() { int result = Message.NONE; boolean wrapped = false; final long now = ClockUtil.nanoRealtime(); synchronized (messageIds) { counter = (counter & 0xffff) % range; int startIdx = counter; while (result < 0 && !wrapped) { int idx = counter++ % range; Long earliestUsage = messageIds.get(idx); if (earliestUsage == null || (earliestUsage - now) <= 0) { result = idx + min; messageIds.put(idx, now + exchangeLifetimeNanos); } wrapped = (counter % range) == startIdx; } } return result; }
MapBasedMessageIdTracker implements MessageIdTracker { public int getNextMessageId() { int result = Message.NONE; boolean wrapped = false; final long now = ClockUtil.nanoRealtime(); synchronized (messageIds) { counter = (counter & 0xffff) % range; int startIdx = counter; while (result < 0 && !wrapped) { int idx = counter++ % range; Long earliestUsage = messageIds.get(idx); if (earliestUsage == null || (earliestUsage - now) <= 0) { result = idx + min; messageIds.put(idx, now + exchangeLifetimeNanos); } wrapped = (counter % range) == startIdx; } } return result; } }
MapBasedMessageIdTracker implements MessageIdTracker { public int getNextMessageId() { int result = Message.NONE; boolean wrapped = false; final long now = ClockUtil.nanoRealtime(); synchronized (messageIds) { counter = (counter & 0xffff) % range; int startIdx = counter; while (result < 0 && !wrapped) { int idx = counter++ % range; Long earliestUsage = messageIds.get(idx); if (earliestUsage == null || (earliestUsage - now) <= 0) { result = idx + min; messageIds.put(idx, now + exchangeLifetimeNanos); } wrapped = (counter % range) == startIdx; } } return result; } MapBasedMessageIdTracker(int initialMid, int minMid, int maxMid, NetworkConfig config); }
MapBasedMessageIdTracker implements MessageIdTracker { public int getNextMessageId() { int result = Message.NONE; boolean wrapped = false; final long now = ClockUtil.nanoRealtime(); synchronized (messageIds) { counter = (counter & 0xffff) % range; int startIdx = counter; while (result < 0 && !wrapped) { int idx = counter++ % range; Long earliestUsage = messageIds.get(idx); if (earliestUsage == null || (earliestUsage - now) <= 0) { result = idx + min; messageIds.put(idx, now + exchangeLifetimeNanos); } wrapped = (counter % range) == startIdx; } } return result; } MapBasedMessageIdTracker(int initialMid, int minMid, int maxMid, NetworkConfig config); int getNextMessageId(); }
MapBasedMessageIdTracker implements MessageIdTracker { public int getNextMessageId() { int result = Message.NONE; boolean wrapped = false; final long now = ClockUtil.nanoRealtime(); synchronized (messageIds) { counter = (counter & 0xffff) % range; int startIdx = counter; while (result < 0 && !wrapped) { int idx = counter++ % range; Long earliestUsage = messageIds.get(idx); if (earliestUsage == null || (earliestUsage - now) <= 0) { result = idx + min; messageIds.put(idx, now + exchangeLifetimeNanos); } wrapped = (counter % range) == startIdx; } } return result; } MapBasedMessageIdTracker(int initialMid, int minMid, int maxMid, NetworkConfig config); int getNextMessageId();  }