1 7 package org.jboss.messaging.memory; 8 9 import java.util.Comparator ; 10 import java.util.Iterator ; 11 import java.util.Set ; 12 import java.util.TreeSet ; 13 14 import org.jboss.messaging.interfaces.Consumer; 15 import org.jboss.messaging.interfaces.MessageReference; 16 import org.jboss.messaging.interfaces.MessageSet; 17 18 import EDU.oswego.cs.dl.util.concurrent.ReentrantLock; 19 20 26 public class MemoryMessageSet implements MessageSet 27 { 28 30 32 33 private Set messages; 34 35 36 private ReentrantLock mutex = new ReentrantLock(); 37 38 40 42 47 public MemoryMessageSet(Comparator comparator) 48 { 49 messages = new TreeSet (comparator); 50 } 51 52 54 56 public void add(MessageReference reference) 57 { 58 messages.add(reference); 59 } 60 61 public MessageReference remove(Consumer consumer) 62 { 63 if (messages.size() > 0) 65 { 66 for (Iterator iterator = messages.iterator(); iterator.hasNext();) 67 { 68 MessageReference message = (MessageReference) iterator.next(); 69 if (consumer.accepts(message, true)) 70 { 71 iterator.remove(); 72 return message; 73 } 74 } 75 } 76 return null; 77 } 78 79 public void lock() 80 { 81 boolean interrupted = false; 82 try 83 { 84 mutex.acquire(); 85 } 86 catch (InterruptedException e) 87 { 88 interrupted = true; 89 } 90 if (interrupted) 91 Thread.currentThread().interrupt(); 92 } 93 94 public void unlock() 95 { 96 mutex.release(); 97 } 98 99 public void setConsumer(Consumer consumer) 100 { 101 } 103 104 106 108 110 } 112 | Popular Tags |