1 45 package org.exolab.jms.messagemgr; 46 47 import java.util.Collections ; 48 import java.util.SortedMap ; 49 import java.util.TreeMap ; 50 import java.util.Map ; 51 import java.util.HashMap ; 52 53 54 64 class MessageQueue { 65 66 69 private final SortedMap _queue = Collections.synchronizedSortedMap( 70 new TreeMap (new MessageHandleComparator())); 71 72 75 private final Map _handles = Collections.synchronizedMap(new HashMap ()); 76 77 78 84 public boolean add(MessageHandle handle) { 85 boolean added = false; 86 if (_queue.put(handle, handle) == null) { 87 _handles.put(handle.getMessageId(), handle); 88 added = true; 89 } 90 return added; 91 } 92 93 99 public boolean contains(MessageHandle handle) { 100 return _queue.containsKey(handle); 101 } 102 103 108 public MessageHandle[] toArray() { 109 return (MessageHandle[]) _queue.keySet().toArray(new MessageHandle[0]); 110 } 111 112 118 public MessageHandle remove(MessageHandle handle) { 119 MessageHandle result = (MessageHandle) _queue.remove(handle); 120 if (result != null) { 121 _handles.remove(handle.getMessageId()); 122 } 123 return result; 124 } 125 126 132 public MessageHandle remove(String messageId) { 133 MessageHandle result = (MessageHandle) _handles.remove(messageId); 134 if (result != null) { 135 _queue.remove(result); 136 } 137 return result; 138 } 139 140 143 public void clear() { 144 _queue.clear(); 145 _handles.clear(); 146 } 147 148 153 public int size() { 154 return _queue.size(); 155 } 156 157 163 public MessageHandle removeFirst() { 164 MessageHandle first = null; 165 synchronized (_queue) { 166 if (_queue.size() > 0) { 167 first = (MessageHandle) _queue.firstKey(); 168 _queue.remove(first); 169 _handles.remove(first.getMessageId()); 170 } 171 } 172 return first; 173 } 174 175 } 176 | Popular Tags |