1 48 package org.exolab.jms.messagemgr; 49 50 import java.sql.Connection ; 51 import java.util.Collections ; 52 import java.util.HashMap ; 53 import java.util.Map ; 54 import javax.jms.JMSException ; 55 56 import org.apache.commons.logging.Log; 57 import org.apache.commons.logging.LogFactory; 58 59 import org.exolab.jms.message.MessageImpl; 60 import org.exolab.jms.persistence.DatabaseService; 61 import org.exolab.jms.persistence.PersistenceAdapter; 62 import org.exolab.jms.persistence.SQLHelper; 63 64 65 72 final class DefaultMessageCache implements MessageCache { 73 74 77 private final Map _transient = Collections.synchronizedMap( 78 new HashMap (1023)); 79 80 83 private final Map _persistent = Collections.synchronizedMap( 84 new HashMap (1023)); 85 86 89 private final Map _references = Collections.synchronizedMap( 90 new HashMap (1023)); 91 92 95 private static final Log _log = LogFactory.getLog( 96 DefaultMessageCache.class); 97 98 99 105 public void addMessage(MessageRef reference, MessageImpl message) { 106 String messageId = reference.getMessageId(); 107 if (reference.isPersistent()) { 108 _persistent.put(messageId, message); 109 } else { 110 _transient.put(messageId, message); 111 } 112 addMessageRef(messageId, reference); 113 } 114 115 120 public void addMessageRef(MessageRef reference) { 121 addMessageRef(reference.getMessageId(), reference); 122 } 123 124 131 public MessageRef getMessageRef(String messageId) { 132 return (MessageRef) _references.get(messageId); 133 } 134 135 142 public MessageImpl getMessage(MessageRef reference) throws JMSException { 143 MessageImpl message = null; 144 final String messageId = reference.getMessageId(); 145 146 if (reference.isPersistent()) { 147 message = (MessageImpl) _persistent.get(messageId); 148 149 if (message == null) { 152 PersistenceAdapter adapter = DatabaseService.getAdapter(); 154 Connection connection = null; 155 try { 156 connection = DatabaseService.getConnection(); 157 message = adapter.getMessage(connection, messageId); 158 connection.commit(); 159 } catch (Exception exception) { 160 SQLHelper.rollback(connection); 161 _log.error("Failed to retrieve message", exception); 162 throw new JMSException ("Failed to retrieve message: " 163 + exception.getMessage()); 164 } finally { 165 SQLHelper.close(connection); 166 } 167 168 if (message != null) { 171 _persistent.put(messageId, message); 172 } 173 } 174 } else { 175 message = (MessageImpl) _transient.get(messageId); 176 } 177 178 if (message != null && !message.getReadOnly()) { 179 message.setReadOnly(true); 181 } 182 183 return message; 184 } 185 186 191 public void destroy(MessageRef reference) throws JMSException { 192 if (reference.isPersistent()) { 193 Connection connection = null; 194 try { 195 connection = DatabaseService.getConnection(); 196 destroy(reference, connection); 197 connection.commit(); 198 } catch (JMSException exception) { 199 SQLHelper.rollback(connection); 200 throw exception; 201 } catch (Exception exception) { 202 SQLHelper.rollback(connection); 203 _log.error("Failed to remove message", exception); 204 throw new JMSException ("Failed to remove message: " 205 + exception.getMessage()); 206 } finally { 207 SQLHelper.close(connection); 208 } 209 } else { 210 final String messageId = reference.getMessageId(); 211 _transient.remove(messageId); 212 _references.remove(messageId); 213 } 214 } 215 216 222 public void destroy(MessageRef reference, Connection connection) 223 throws JMSException { 224 final String messageId = reference.getMessageId(); 225 226 if (reference.isPersistent()) { 227 try { 228 PersistenceAdapter adapter = DatabaseService.getAdapter(); 229 adapter.removeMessage(connection, messageId); 230 } catch (Exception exception) { 231 _log.error("Failed to remove message", exception); 232 throw new JMSException ("Failed to remove message: " 233 + exception.getMessage()); 234 } 235 _persistent.remove(messageId); 236 } else { 237 _transient.remove(messageId); 238 } 239 _references.remove(messageId); 240 } 241 242 245 public void clear() { 246 _transient.clear(); 247 _persistent.clear(); 248 _references.clear(); 249 } 250 251 254 public void clearPersistentMessages() { 255 _persistent.clear(); 256 } 257 258 261 public void clearTransientMessages() { 262 _transient.clear(); 263 } 264 265 270 public int getTransientCount() { 271 return _transient.size(); 272 } 273 274 279 public int getPersistentCount() { 280 return _persistent.size(); 281 } 282 283 288 public int getMessageCount() { 289 return _references.size(); 290 } 291 292 298 private void addMessageRef(String messageId, MessageRef reference) { 299 _references.put(messageId, reference); 300 } 301 302 } 303 304 | Popular Tags |