1 45 package org.exolab.jms.client; 46 47 import java.util.Enumeration ; 48 import java.util.LinkedList ; 49 import java.util.List ; 50 51 import javax.jms.JMSException ; 52 import javax.jms.Message ; 53 import javax.jms.Queue ; 54 import javax.jms.QueueBrowser ; 55 56 import org.apache.commons.logging.Log; 57 import org.apache.commons.logging.LogFactory; 58 59 60 67 class JmsQueueBrowser 68 extends JmsMessageConsumer 69 implements QueueBrowser , Enumeration { 70 71 74 private LinkedList _messages = new LinkedList (); 75 76 79 private static final Log _log = LogFactory.getLog(JmsQueueBrowser.class); 80 81 82 90 public JmsQueueBrowser(JmsSession session, long consumerId, 91 Queue queue, String selector) { 92 super(session, consumerId, queue, selector); 93 } 94 95 100 public Queue getQueue() { 101 return (Queue ) getDestination(); 102 } 103 104 110 public Enumeration getEnumeration() { 111 return this; 112 } 113 114 119 public void close() throws JMSException { 120 super.close(); 121 if (_messages != null) { 122 _messages.clear(); 123 _messages = null; 124 } 125 } 126 127 133 public void onMessage(Message message) { 134 throw new RuntimeException ( 135 "JmsQueueBrowsder.onMessage() has been called"); 136 } 137 138 143 public boolean hasMoreElements() { 144 return !isEmpty(); 145 } 146 147 152 public synchronized Object nextElement() { 153 if (!isEmpty()) { 154 return _messages.removeFirst(); 155 } 156 157 return null; 158 } 159 160 168 private boolean isEmpty() { 169 final int count = 20; 170 if (!_messages.isEmpty()) { 172 return false; 173 } 174 175 List messages = null; 179 try { 180 messages = getSession().browse(getConsumerId(), count); 181 } catch (JMSException exception) { 182 _log.error("Error in JmsQueueBrowser.isEmpty", exception); 183 } 184 185 if (messages != null) { 186 _messages.addAll(messages); 187 } 188 189 return _messages.isEmpty(); 190 } 191 192 } 193 | Popular Tags |