1 16 17 package org.springframework.jms.support; 18 19 import javax.jms.Connection ; 20 import javax.jms.JMSException ; 21 import javax.jms.MessageConsumer ; 22 import javax.jms.MessageProducer ; 23 import javax.jms.QueueRequestor ; 24 import javax.jms.Session ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.springframework.jms.InvalidClientIDException; 30 import org.springframework.jms.InvalidDestinationException; 31 import org.springframework.jms.InvalidSelectorException; 32 import org.springframework.jms.JmsException; 33 import org.springframework.jms.JmsSecurityException; 34 import org.springframework.jms.MessageEOFException; 35 import org.springframework.jms.MessageFormatException; 36 import org.springframework.jms.MessageNotReadableException; 37 import org.springframework.jms.MessageNotWriteableException; 38 import org.springframework.jms.ResourceAllocationException; 39 import org.springframework.jms.TransactionInProgressException; 40 import org.springframework.jms.TransactionRolledBackException; 41 import org.springframework.jms.UncategorizedJmsException; 42 import org.springframework.util.Assert; 43 44 51 public abstract class JmsUtils { 52 53 private static final Log logger = LogFactory.getLog(JmsUtils.class); 54 55 56 61 public static void closeConnection(Connection con) { 62 closeConnection(con, false); 63 } 64 65 71 public static void closeConnection(Connection con, boolean stop) { 72 if (con != null) { 73 try { 74 if (stop) { 75 try { 76 con.stop(); 77 } 78 finally { 79 con.close(); 80 } 81 } 82 else { 83 con.close(); 84 } 85 } 86 catch (JMSException ex) { 87 logger.debug("Could not close JMS Connection", ex); 88 } 89 catch (Throwable ex) { 90 logger.debug("Unexpected exception on closing JMS Connection", ex); 92 } 93 } 94 } 95 96 101 public static void closeSession(Session session) { 102 if (session != null) { 103 try { 104 session.close(); 105 } 106 catch (JMSException ex) { 107 logger.debug("Could not close JMS Session", ex); 108 } 109 catch (Throwable ex) { 110 logger.debug("Unexpected exception on closing JMS Session", ex); 112 } 113 } 114 } 115 116 121 public static void closeMessageProducer(MessageProducer producer) { 122 if (producer != null) { 123 try { 124 producer.close(); 125 } 126 catch (JMSException ex) { 127 logger.debug("Could not close JMS MessageProducer", ex); 128 } 129 catch (Throwable ex) { 130 logger.debug("Unexpected exception on closing JMS MessageProducer", ex); 132 } 133 } 134 } 135 136 141 public static void closeMessageConsumer(MessageConsumer consumer) { 142 if (consumer != null) { 143 try { 144 consumer.close(); 145 } 146 catch (JMSException ex) { 147 logger.debug("Could not close JMS MessageConsumer", ex); 148 } 149 catch (Throwable ex) { 150 logger.debug("Unexpected exception on closing JMS MessageConsumer", ex); 152 } 153 } 154 } 155 156 161 public static void closeQueueRequestor(QueueRequestor requestor) { 162 if (requestor != null) { 163 try { 164 requestor.close(); 165 } 166 catch (JMSException ex) { 167 logger.debug("Could not close JMS QueueRequestor", ex); 168 } 169 catch (Throwable ex) { 170 logger.debug("Unexpected exception on closing JMS QueueRequestor", ex); 172 } 173 } 174 } 175 176 181 public static void commitIfNecessary(Session session) throws JMSException { 182 Assert.notNull(session, "Session must not be null"); 183 try { 184 session.commit(); 185 } 186 catch (javax.jms.TransactionInProgressException ex) { 187 } 189 catch (javax.jms.IllegalStateException ex) { 190 } 192 } 193 194 199 public static void rollbackIfNecessary(Session session) throws JMSException { 200 Assert.notNull(session, "Session must not be null"); 201 try { 202 session.rollback(); 203 } 204 catch (javax.jms.TransactionInProgressException ex) { 205 } 207 catch (javax.jms.IllegalStateException ex) { 208 } 210 } 211 212 219 public static JmsException convertJmsAccessException(JMSException ex) { 220 Assert.notNull(ex, "JMSException must not be null"); 221 222 if (ex instanceof javax.jms.IllegalStateException ) { 223 return new org.springframework.jms.IllegalStateException((javax.jms.IllegalStateException ) ex); 224 } 225 if (ex instanceof javax.jms.InvalidClientIDException ) { 226 return new InvalidClientIDException((javax.jms.InvalidClientIDException ) ex); 227 } 228 if (ex instanceof javax.jms.InvalidDestinationException ) { 229 return new InvalidDestinationException((javax.jms.InvalidDestinationException ) ex); 230 } 231 if (ex instanceof javax.jms.InvalidSelectorException ) { 232 return new InvalidSelectorException((javax.jms.InvalidSelectorException ) ex); 233 } 234 if (ex instanceof javax.jms.JMSSecurityException ) { 235 return new JmsSecurityException((javax.jms.JMSSecurityException ) ex); 236 } 237 if (ex instanceof javax.jms.MessageEOFException ) { 238 return new MessageEOFException((javax.jms.MessageEOFException ) ex); 239 } 240 if (ex instanceof javax.jms.MessageFormatException ) { 241 return new MessageFormatException((javax.jms.MessageFormatException ) ex); 242 } 243 if (ex instanceof javax.jms.MessageNotReadableException ) { 244 return new MessageNotReadableException((javax.jms.MessageNotReadableException ) ex); 245 } 246 if (ex instanceof javax.jms.MessageNotWriteableException ) { 247 return new MessageNotWriteableException((javax.jms.MessageNotWriteableException ) ex); 248 } 249 if (ex instanceof javax.jms.ResourceAllocationException ) { 250 return new ResourceAllocationException((javax.jms.ResourceAllocationException ) ex); 251 } 252 if (ex instanceof javax.jms.TransactionInProgressException ) { 253 return new TransactionInProgressException((javax.jms.TransactionInProgressException ) ex); 254 } 255 if (ex instanceof javax.jms.TransactionRolledBackException ) { 256 return new TransactionRolledBackException((javax.jms.TransactionRolledBackException ) ex); 257 } 258 259 return new UncategorizedJmsException(ex); 261 } 262 263 } 264 | Popular Tags |