KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > support > JmsUtils


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jms.support;
18
19 import javax.jms.Connection JavaDoc;
20 import javax.jms.JMSException JavaDoc;
21 import javax.jms.MessageConsumer JavaDoc;
22 import javax.jms.MessageProducer JavaDoc;
23 import javax.jms.QueueRequestor JavaDoc;
24 import javax.jms.Session JavaDoc;
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 /**
45  * Generic utility methods for working with JMS. Mainly for internal use
46  * within the framework, but also useful for custom JMS access code.
47  *
48  * @author Juergen Hoeller
49  * @since 1.1
50  */

51 public abstract class JmsUtils {
52
53     private static final Log logger = LogFactory.getLog(JmsUtils.class);
54
55
56     /**
57      * Close the given JMS Connection and ignore any thrown exception.
58      * This is useful for typical <code>finally</code> blocks in manual JMS code.
59      * @param con the JMS Connection to close (may be <code>null</code>)
60      */

61     public static void closeConnection(Connection JavaDoc con) {
62         closeConnection(con, false);
63     }
64
65     /**
66      * Close the given JMS Connection and ignore any thrown exception.
67      * This is useful for typical <code>finally</code> blocks in manual JMS code.
68      * @param con the JMS Connection to close (may be <code>null</code>)
69      * @param stop whether to call <code>stop()</code> before closing
70      */

71     public static void closeConnection(Connection JavaDoc 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 JavaDoc ex) {
87                 logger.debug("Could not close JMS Connection", ex);
88             }
89             catch (Throwable JavaDoc ex) {
90                 // We don't trust the JMS provider: It might throw RuntimeException or Error.
91
logger.debug("Unexpected exception on closing JMS Connection", ex);
92             }
93         }
94     }
95
96     /**
97      * Close the given JMS Session and ignore any thrown exception.
98      * This is useful for typical <code>finally</code> blocks in manual JMS code.
99      * @param session the JMS Session to close (may be <code>null</code>)
100      */

101     public static void closeSession(Session JavaDoc session) {
102         if (session != null) {
103             try {
104                 session.close();
105             }
106             catch (JMSException JavaDoc ex) {
107                 logger.debug("Could not close JMS Session", ex);
108             }
109             catch (Throwable JavaDoc ex) {
110                 // We don't trust the JMS provider: It might throw RuntimeException or Error.
111
logger.debug("Unexpected exception on closing JMS Session", ex);
112             }
113         }
114     }
115
116     /**
117      * Close the given JMS MessageProducer and ignore any thrown exception.
118      * This is useful for typical <code>finally</code> blocks in manual JMS code.
119      * @param producer the JMS MessageProducer to close (may be <code>null</code>)
120      */

121     public static void closeMessageProducer(MessageProducer JavaDoc producer) {
122         if (producer != null) {
123             try {
124                 producer.close();
125             }
126             catch (JMSException JavaDoc ex) {
127                 logger.debug("Could not close JMS MessageProducer", ex);
128             }
129             catch (Throwable JavaDoc ex) {
130                 // We don't trust the JMS provider: It might throw RuntimeException or Error.
131
logger.debug("Unexpected exception on closing JMS MessageProducer", ex);
132             }
133         }
134     }
135
136     /**
137      * Close the given JMS MessageConsumer and ignore any thrown exception.
138      * This is useful for typical <code>finally</code> blocks in manual JMS code.
139      * @param consumer the JMS MessageConsumer to close (may be <code>null</code>)
140      */

141     public static void closeMessageConsumer(MessageConsumer JavaDoc consumer) {
142         if (consumer != null) {
143             try {
144                 consumer.close();
145             }
146             catch (JMSException JavaDoc ex) {
147                 logger.debug("Could not close JMS MessageConsumer", ex);
148             }
149             catch (Throwable JavaDoc ex) {
150                 // We don't trust the JMS provider: It might throw RuntimeException or Error.
151
logger.debug("Unexpected exception on closing JMS MessageConsumer", 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 JavaDoc requestor) {
162         if (requestor != null) {
163             try {
164                 requestor.close();
165             }
166             catch (JMSException JavaDoc ex) {
167                 logger.debug("Could not close JMS QueueRequestor", ex);
168             }
169             catch (Throwable JavaDoc ex) {
170                 // We don't trust the JMS provider: It might throw RuntimeException or Error.
171
logger.debug("Unexpected exception on closing JMS QueueRequestor", ex);
172             }
173         }
174     }
175
176     /**
177      * Commit the Session if not within a JTA transaction.
178      * @param session the JMS Session to commit
179      * @throws JMSException if committing failed
180      */

181     public static void commitIfNecessary(Session JavaDoc session) throws JMSException JavaDoc {
182         Assert.notNull(session, "Session must not be null");
183         try {
184             session.commit();
185         }
186         catch (javax.jms.TransactionInProgressException JavaDoc ex) {
187             // Ignore -> can only happen in case of a JTA transaction.
188
}
189         catch (javax.jms.IllegalStateException JavaDoc 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 JavaDoc session) throws JMSException JavaDoc {
200         Assert.notNull(session, "Session must not be null");
201         try {
202             session.rollback();
203         }
204         catch (javax.jms.TransactionInProgressException JavaDoc ex) {
205             // Ignore -> can only happen in case of a JTA transaction.
206
}
207         catch (javax.jms.IllegalStateException JavaDoc ex) {
208             // Ignore -> can only happen in case of a JTA transaction.
209
}
210     }
211
212     /**
213      * Convert the specified checked {@link javax.jms.JMSException JMSException} to
214      * a Spring runtime {@link org.springframework.jms.JmsException JmsException}
215      * equivalent.
216      * @param ex the original checked JMSException to convert
217      * @return the Spring runtime JmsException wrapping the given exception
218      */

219     public static JmsException convertJmsAccessException(JMSException JavaDoc ex) {
220         Assert.notNull(ex, "JMSException must not be null");
221
222         if (ex instanceof javax.jms.IllegalStateException JavaDoc) {
223             return new org.springframework.jms.IllegalStateException((javax.jms.IllegalStateException JavaDoc) ex);
224         }
225         if (ex instanceof javax.jms.InvalidClientIDException JavaDoc) {
226             return new InvalidClientIDException((javax.jms.InvalidClientIDException JavaDoc) ex);
227         }
228         if (ex instanceof javax.jms.InvalidDestinationException JavaDoc) {
229             return new InvalidDestinationException((javax.jms.InvalidDestinationException JavaDoc) ex);
230         }
231         if (ex instanceof javax.jms.InvalidSelectorException JavaDoc) {
232             return new InvalidSelectorException((javax.jms.InvalidSelectorException JavaDoc) ex);
233         }
234         if (ex instanceof javax.jms.JMSSecurityException JavaDoc) {
235             return new JmsSecurityException((javax.jms.JMSSecurityException JavaDoc) ex);
236         }
237         if (ex instanceof javax.jms.MessageEOFException JavaDoc) {
238             return new MessageEOFException((javax.jms.MessageEOFException JavaDoc) ex);
239         }
240         if (ex instanceof javax.jms.MessageFormatException JavaDoc) {
241             return new MessageFormatException((javax.jms.MessageFormatException JavaDoc) ex);
242         }
243         if (ex instanceof javax.jms.MessageNotReadableException JavaDoc) {
244             return new MessageNotReadableException((javax.jms.MessageNotReadableException JavaDoc) ex);
245         }
246         if (ex instanceof javax.jms.MessageNotWriteableException JavaDoc) {
247             return new MessageNotWriteableException((javax.jms.MessageNotWriteableException JavaDoc) ex);
248         }
249         if (ex instanceof javax.jms.ResourceAllocationException JavaDoc) {
250             return new ResourceAllocationException((javax.jms.ResourceAllocationException JavaDoc) ex);
251         }
252         if (ex instanceof javax.jms.TransactionInProgressException JavaDoc) {
253             return new TransactionInProgressException((javax.jms.TransactionInProgressException JavaDoc) ex);
254         }
255         if (ex instanceof javax.jms.TransactionRolledBackException JavaDoc) {
256             return new TransactionRolledBackException((javax.jms.TransactionRolledBackException JavaDoc) ex);
257         }
258
259         // fallback
260
return new UncategorizedJmsException(ex);
261     }
262
263 }
264
Popular Tags