Skip to content

Instantly share code, notes, and snippets.

@gurbuzali
Created January 21, 2014 13:38
Show Gist options
  • Save gurbuzali/8540191 to your computer and use it in GitHub Desktop.
Save gurbuzali/8540191 to your computer and use it in GitHub Desktop.
QueueBackinMap example for 3.x
public class QueueBackedByMap<I> implements IQueue<I> {
final IQueue<Long> queue;
final IMap<Long, I> map;
final IdGenerator idGenerator;
public QueueBackedByMap(HazelcastInstance instance, String name) {
queue = instance.getQueue(name);
map = instance.getMap(name);
idGenerator = instance.getIdGenerator(name);
}
.....
public boolean offer(I item) {
final long id = idGenerator.newId();
map.put(id, item);
if (!queue.offer(id)) {
map.remove(id);
return false;
}
return true;
}
public I poll() {
final Long id = queue.poll();
if (id == null) {
return null;
}
final I item = map.get(id);
return item;
}
.....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment