Created
January 21, 2014 13:38
-
-
Save gurbuzali/8540191 to your computer and use it in GitHub Desktop.
QueueBackinMap example for 3.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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