KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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.ConnectionFactory JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.Session JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.springframework.beans.factory.InitializingBean;
28 import org.springframework.core.Constants;
29 import org.springframework.jms.JmsException;
30
31 /**
32  * Base class for {@link org.springframework.jms.core.JmsTemplate} and other
33  * JMS-accessing gateway helpers, defining common properties such as the
34  * JMS {@link ConnectionFactory} to operate on. The subclass
35  * {@link org.springframework.jms.support.destination.JmsDestinationAccessor}
36  * adds further, destination-related properties.
37  *
38  * <p>Not intended to be used directly.
39  * See {@link org.springframework.jms.core.JmsTemplate}.
40  *
41  * @author Juergen Hoeller
42  * @since 1.2
43  * @see org.springframework.jms.support.destination.JmsDestinationAccessor
44  * @see org.springframework.jms.core.JmsTemplate
45  */

46 public abstract class JmsAccessor implements InitializingBean {
47
48     /** Constants instance for javax.jms.Session */
49     private static final Constants sessionConstants = new Constants(Session JavaDoc.class);
50
51
52     /** Logger available to subclasses */
53     protected final Log logger = LogFactory.getLog(getClass());
54
55     private ConnectionFactory JavaDoc connectionFactory;
56
57     private boolean sessionTransacted = false;
58
59     private int sessionAcknowledgeMode = Session.AUTO_ACKNOWLEDGE;
60
61
62     /**
63      * Set the ConnectionFactory to use for obtaining JMS
64      * {@link Connection Connections}.
65      */

66     public void setConnectionFactory(ConnectionFactory JavaDoc connectionFactory) {
67         this.connectionFactory = connectionFactory;
68     }
69
70     /**
71      * Return the ConnectionFactory that this accessor uses for
72      * obtaining JMS {@link Connection Connections}.
73      */

74     public ConnectionFactory JavaDoc getConnectionFactory() {
75         return this.connectionFactory;
76     }
77
78     /**
79      * Set the transaction mode that is used when creating a JMS {@link Session}.
80      * Default is "false".
81      * <p>Note that within a JTA transaction, the parameters passed to
82      * <code>create(Queue/Topic)Session(boolean transacted, int acknowledgeMode)</code>
83      * method are not taken into account. Depending on the J2EE transaction context,
84      * the container makes its own decisions on these values. Analogously, these
85      * parameters are not taken into account within a locally managed transaction
86      * either, since the accessor operates on an existing JMS Session in this case.
87      * <p>Setting this flag to "true" will use a short local JMS transaction
88      * when running outside of a managed transaction, and a synchronized local
89      * JMS transaction in case of a managed transaction (other than an XA
90      * transaction) being present. The latter has the effect of a local JMS
91      * transaction being managed alongside the main transaction (which might
92      * be a native JDBC transaction), with the JMS transaction committing
93      * right after the main transaction.
94      * @param sessionTransacted the transaction mode
95      * @see javax.jms.Connection#createSession(boolean, int)
96      */

97     public void setSessionTransacted(boolean sessionTransacted) {
98         this.sessionTransacted = sessionTransacted;
99     }
100
101     /**
102      * Return whether the JMS {@link Session sessions} used by this
103      * accessor are supposed to be transacted.
104      * @return <code>true</code> if the JMS Sessions used are transacted
105      * @see #setSessionTransacted(boolean)
106      */

107     public boolean isSessionTransacted() {
108         return this.sessionTransacted;
109     }
110
111     /**
112      * Set the JMS acknowledgement mode by the name of the corresponding constant
113      * in the JMS {@link Session} interface, e.g. "CLIENT_ACKNOWLEDGE".
114      * <p>If you want to use vendor-specific extensions to the acknowledgment mode,
115      * use {@link #setSessionAcknowledgeModeName(String)} instead.
116      * @param constantName the name of the {@link Session} acknowledge mode constant
117      * @see javax.jms.Session#AUTO_ACKNOWLEDGE
118      * @see javax.jms.Session#CLIENT_ACKNOWLEDGE
119      * @see javax.jms.Session#DUPS_OK_ACKNOWLEDGE
120      * @see javax.jms.Connection#createSession(boolean, int)
121      */

122     public void setSessionAcknowledgeModeName(String JavaDoc constantName) {
123         setSessionAcknowledgeMode(sessionConstants.asNumber(constantName).intValue());
124     }
125
126     /**
127      * Set the JMS acknowledgement mode that is used when creating a JMS
128      * {@link Session} to send a message.
129      * <p>Default is {@link Session#AUTO_ACKNOWLEDGE}.
130      * <p>Vendor-specific extensions to the acknowledgment mode can be set here as well.
131      * <p>Note that that inside an EJB the parameters to
132      * create(Queue/Topic)Session(boolean transacted, int acknowledgeMode) method
133      * are not taken into account. Depending on the transaction context in the EJB,
134      * the container makes its own decisions on these values. See section 17.3.5
135      * of the EJB spec.
136      * @param sessionAcknowledgeMode the acknowledgement mode
137      * @see javax.jms.Session#AUTO_ACKNOWLEDGE
138      * @see javax.jms.Session#CLIENT_ACKNOWLEDGE
139      * @see javax.jms.Session#DUPS_OK_ACKNOWLEDGE
140      * @see javax.jms.Connection#createSession(boolean, int)
141      */

142     public void setSessionAcknowledgeMode(int sessionAcknowledgeMode) {
143         this.sessionAcknowledgeMode = sessionAcknowledgeMode;
144     }
145
146     /**
147      * Return the acknowledgement mode for JMS {@link Session sessions}.
148      * @return the acknowledgement mode applied by this accessor
149      */

150     public int getSessionAcknowledgeMode() {
151         return this.sessionAcknowledgeMode;
152     }
153
154     public void afterPropertiesSet() {
155         if (getConnectionFactory() == null) {
156             throw new IllegalArgumentException JavaDoc("Property 'connectionFactory' is required");
157         }
158     }
159
160
161     /**
162      * Convert the specified checked {@link javax.jms.JMSException JMSException} to
163      * a Spring runtime {@link org.springframework.jms.JmsException JmsException}
164      * equivalent.
165      * <p>The default implementation delegates to the
166      * {@link org.springframework.jms.support.JmsUtils#convertJmsAccessException} method.
167      * @param ex the original checked {@link JMSException} to convert
168      * @return the Spring runtime {@link JmsException} wrapping <code>ex</code>
169      * @see org.springframework.jms.support.JmsUtils#convertJmsAccessException
170      */

171     protected JmsException convertJmsAccessException(JMSException JavaDoc ex) {
172         return JmsUtils.convertJmsAccessException(ex);
173     }
174
175
176     //-------------------------------------------------------------------------
177
// JMS 1.1 factory methods, potentially overridden for JMS 1.0.2
178
//-------------------------------------------------------------------------
179

180     /**
181      * Create a JMS Connection via this template's ConnectionFactory.
182      * <p>This implementation uses JMS 1.1 API.
183      * @return the new JMS Connection
184      * @throws JMSException if thrown by JMS API methods
185      * @see javax.jms.ConnectionFactory#createConnection()
186      */

187     protected Connection JavaDoc createConnection() throws JMSException JavaDoc {
188         return getConnectionFactory().createConnection();
189     }
190
191     /**
192      * Create a JMS Session for the given Connection.
193      * <p>This implementation uses JMS 1.1 API.
194      * @param con the JMS Connection to create a Session for
195      * @return the new JMS Session
196      * @throws JMSException if thrown by JMS API methods
197      * @see javax.jms.Connection#createSession(boolean, int)
198      */

199     protected Session JavaDoc createSession(Connection JavaDoc con) throws JMSException JavaDoc {
200         return con.createSession(isSessionTransacted(), getSessionAcknowledgeMode());
201     }
202
203     /**
204      * Determine whether the given Session is in client acknowledge mode.
205      * <p>This implementation uses JMS 1.1 API.
206      * @param session the JMS Session to check
207      * @return whether the given Session is in client acknowledge mode
208      * @throws javax.jms.JMSException if thrown by JMS API methods
209      * @see javax.jms.Session#getAcknowledgeMode()
210      * @see javax.jms.Session#CLIENT_ACKNOWLEDGE
211      */

212     protected boolean isClientAcknowledge(Session JavaDoc session) throws JMSException JavaDoc {
213         return (session.getAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE);
214     }
215
216 }
217
Popular Tags