001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jms.support;
018:
019: import javax.jms.Connection;
020: import javax.jms.JMSException;
021: import javax.jms.MessageConsumer;
022: import javax.jms.MessageProducer;
023: import javax.jms.QueueRequestor;
024: import javax.jms.Session;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: import org.springframework.jms.InvalidClientIDException;
030: import org.springframework.jms.InvalidDestinationException;
031: import org.springframework.jms.InvalidSelectorException;
032: import org.springframework.jms.JmsException;
033: import org.springframework.jms.JmsSecurityException;
034: import org.springframework.jms.MessageEOFException;
035: import org.springframework.jms.MessageFormatException;
036: import org.springframework.jms.MessageNotReadableException;
037: import org.springframework.jms.MessageNotWriteableException;
038: import org.springframework.jms.ResourceAllocationException;
039: import org.springframework.jms.TransactionInProgressException;
040: import org.springframework.jms.TransactionRolledBackException;
041: import org.springframework.jms.UncategorizedJmsException;
042: import org.springframework.util.Assert;
043:
044: /**
045: * Generic utility methods for working with JMS. Mainly for internal use
046: * within the framework, but also useful for custom JMS access code.
047: *
048: * @author Juergen Hoeller
049: * @since 1.1
050: */
051: public abstract class JmsUtils {
052:
053: private static final Log logger = LogFactory.getLog(JmsUtils.class);
054:
055: /**
056: * Close the given JMS Connection and ignore any thrown exception.
057: * This is useful for typical <code>finally</code> blocks in manual JMS code.
058: * @param con the JMS Connection to close (may be <code>null</code>)
059: */
060: public static void closeConnection(Connection con) {
061: closeConnection(con, false);
062: }
063:
064: /**
065: * Close the given JMS Connection and ignore any thrown exception.
066: * This is useful for typical <code>finally</code> blocks in manual JMS code.
067: * @param con the JMS Connection to close (may be <code>null</code>)
068: * @param stop whether to call <code>stop()</code> before closing
069: */
070: public static void closeConnection(Connection con, boolean stop) {
071: if (con != null) {
072: try {
073: if (stop) {
074: try {
075: con.stop();
076: } finally {
077: con.close();
078: }
079: } else {
080: con.close();
081: }
082: } catch (JMSException ex) {
083: logger.debug("Could not close JMS Connection", ex);
084: } catch (Throwable ex) {
085: // We don't trust the JMS provider: It might throw RuntimeException or Error.
086: logger
087: .debug(
088: "Unexpected exception on closing JMS Connection",
089: ex);
090: }
091: }
092: }
093:
094: /**
095: * Close the given JMS Session and ignore any thrown exception.
096: * This is useful for typical <code>finally</code> blocks in manual JMS code.
097: * @param session the JMS Session to close (may be <code>null</code>)
098: */
099: public static void closeSession(Session session) {
100: if (session != null) {
101: try {
102: session.close();
103: } catch (JMSException ex) {
104: logger.debug("Could not close JMS Session", ex);
105: } catch (Throwable ex) {
106: // We don't trust the JMS provider: It might throw RuntimeException or Error.
107: logger.debug(
108: "Unexpected exception on closing JMS Session",
109: ex);
110: }
111: }
112: }
113:
114: /**
115: * Close the given JMS MessageProducer and ignore any thrown exception.
116: * This is useful for typical <code>finally</code> blocks in manual JMS code.
117: * @param producer the JMS MessageProducer to close (may be <code>null</code>)
118: */
119: public static void closeMessageProducer(MessageProducer producer) {
120: if (producer != null) {
121: try {
122: producer.close();
123: } catch (JMSException ex) {
124: logger.debug("Could not close JMS MessageProducer", ex);
125: } catch (Throwable ex) {
126: // We don't trust the JMS provider: It might throw RuntimeException or Error.
127: logger
128: .debug(
129: "Unexpected exception on closing JMS MessageProducer",
130: ex);
131: }
132: }
133: }
134:
135: /**
136: * Close the given JMS MessageConsumer and ignore any thrown exception.
137: * This is useful for typical <code>finally</code> blocks in manual JMS code.
138: * @param consumer the JMS MessageConsumer to close (may be <code>null</code>)
139: */
140: public static void closeMessageConsumer(MessageConsumer consumer) {
141: if (consumer != null) {
142: try {
143: consumer.close();
144: } catch (JMSException ex) {
145: logger.debug("Could not close JMS MessageConsumer", ex);
146: } catch (Throwable ex) {
147: // We don't trust the JMS provider: It might throw RuntimeException or Error.
148: logger
149: .debug(
150: "Unexpected exception on closing JMS MessageConsumer",
151: ex);
152: }
153: }
154: }
155:
156: /**
157: * Close the given JMS QueueRequestor and ignore any thrown exception.
158: * This is useful for typical <code>finally</code> blocks in manual JMS code.
159: * @param requestor the JMS QueueRequestor to close (may be <code>null</code>)
160: */
161: public static void closeQueueRequestor(QueueRequestor requestor) {
162: if (requestor != null) {
163: try {
164: requestor.close();
165: } catch (JMSException ex) {
166: logger.debug("Could not close JMS QueueRequestor", ex);
167: } catch (Throwable ex) {
168: // We don't trust the JMS provider: It might throw RuntimeException or Error.
169: logger
170: .debug(
171: "Unexpected exception on closing JMS QueueRequestor",
172: ex);
173: }
174: }
175: }
176:
177: /**
178: * Commit the Session if not within a JTA transaction.
179: * @param session the JMS Session to commit
180: * @throws JMSException if committing failed
181: */
182: public static void commitIfNecessary(Session session)
183: throws JMSException {
184: Assert.notNull(session, "Session must not be null");
185: try {
186: session.commit();
187: } catch (javax.jms.TransactionInProgressException ex) {
188: // Ignore -> can only happen in case of a JTA transaction.
189: } catch (javax.jms.IllegalStateException ex) {
190: // Ignore -> can only happen in case of a JTA transaction.
191: }
192: }
193:
194: /**
195: * Rollback the Session if not within a JTA transaction.
196: * @param session the JMS Session to rollback
197: * @throws JMSException if committing failed
198: */
199: public static void rollbackIfNecessary(Session session)
200: throws JMSException {
201: Assert.notNull(session, "Session must not be null");
202: try {
203: session.rollback();
204: } catch (javax.jms.TransactionInProgressException ex) {
205: // Ignore -> can only happen in case of a JTA transaction.
206: } catch (javax.jms.IllegalStateException ex) {
207: // Ignore -> can only happen in case of a JTA transaction.
208: }
209: }
210:
211: /**
212: * Convert the specified checked {@link javax.jms.JMSException JMSException} to
213: * a Spring runtime {@link org.springframework.jms.JmsException JmsException}
214: * equivalent.
215: * @param ex the original checked JMSException to convert
216: * @return the Spring runtime JmsException wrapping the given exception
217: */
218: public static JmsException convertJmsAccessException(JMSException ex) {
219: Assert.notNull(ex, "JMSException must not be null");
220:
221: if (ex instanceof javax.jms.IllegalStateException) {
222: return new org.springframework.jms.IllegalStateException(
223: (javax.jms.IllegalStateException) ex);
224: }
225: if (ex instanceof javax.jms.InvalidClientIDException) {
226: return new InvalidClientIDException(
227: (javax.jms.InvalidClientIDException) ex);
228: }
229: if (ex instanceof javax.jms.InvalidDestinationException) {
230: return new InvalidDestinationException(
231: (javax.jms.InvalidDestinationException) ex);
232: }
233: if (ex instanceof javax.jms.InvalidSelectorException) {
234: return new InvalidSelectorException(
235: (javax.jms.InvalidSelectorException) ex);
236: }
237: if (ex instanceof javax.jms.JMSSecurityException) {
238: return new JmsSecurityException(
239: (javax.jms.JMSSecurityException) ex);
240: }
241: if (ex instanceof javax.jms.MessageEOFException) {
242: return new MessageEOFException(
243: (javax.jms.MessageEOFException) ex);
244: }
245: if (ex instanceof javax.jms.MessageFormatException) {
246: return new MessageFormatException(
247: (javax.jms.MessageFormatException) ex);
248: }
249: if (ex instanceof javax.jms.MessageNotReadableException) {
250: return new MessageNotReadableException(
251: (javax.jms.MessageNotReadableException) ex);
252: }
253: if (ex instanceof javax.jms.MessageNotWriteableException) {
254: return new MessageNotWriteableException(
255: (javax.jms.MessageNotWriteableException) ex);
256: }
257: if (ex instanceof javax.jms.ResourceAllocationException) {
258: return new ResourceAllocationException(
259: (javax.jms.ResourceAllocationException) ex);
260: }
261: if (ex instanceof javax.jms.TransactionInProgressException) {
262: return new TransactionInProgressException(
263: (javax.jms.TransactionInProgressException) ex);
264: }
265: if (ex instanceof javax.jms.TransactionRolledBackException) {
266: return new TransactionRolledBackException(
267: (javax.jms.TransactionRolledBackException) ex);
268: }
269:
270: // fallback
271: return new UncategorizedJmsException(ex);
272: }
273:
274: }
|