KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > core > JmsTemplate102


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.core;
18
19 import javax.jms.Connection JavaDoc;
20 import javax.jms.ConnectionFactory JavaDoc;
21 import javax.jms.Destination JavaDoc;
22 import javax.jms.JMSException JavaDoc;
23 import javax.jms.Message JavaDoc;
24 import javax.jms.MessageConsumer JavaDoc;
25 import javax.jms.MessageProducer JavaDoc;
26 import javax.jms.Queue JavaDoc;
27 import javax.jms.QueueConnection JavaDoc;
28 import javax.jms.QueueConnectionFactory JavaDoc;
29 import javax.jms.QueueSender JavaDoc;
30 import javax.jms.QueueSession JavaDoc;
31 import javax.jms.Session JavaDoc;
32 import javax.jms.Topic JavaDoc;
33 import javax.jms.TopicConnection JavaDoc;
34 import javax.jms.TopicConnectionFactory JavaDoc;
35 import javax.jms.TopicPublisher JavaDoc;
36 import javax.jms.TopicSession JavaDoc;
37
38 import org.springframework.jms.connection.JmsResourceHolder;
39 import org.springframework.jms.support.converter.SimpleMessageConverter102;
40
41 /**
42  * A subclass of JmsTemplate that uses the JMS 1.0.2 specification, rather than
43  * the JMS 1.1 methods used by JmsTemplate itself. This class can be used for JMS
44  * 1.0.2 providers, offering the same API as JmsTemplate does for JMS 1.1 providers.
45  *
46  * <p>You must specify the domain or style of messaging to be either
47  * Point-to-Point (Queues) or Publish/Subscribe (Topics), using the
48  * "pubSubDomain" property. Point-to-Point (Queues) is the default domain.
49  *
50  * <p>The "pubSubDomain" property is an important setting due to the use of similar
51  * but separate class hierarchies in the JMS 1.0.2 API. JMS 1.1 provides a new
52  * domain-independent API that allows for easy mix-and-match use of Point-to-Point
53  * and Publish/Subscribe domain.
54  *
55  * <p>This template uses a DynamicDestinationResolver and a SimpleMessageConverter102
56  * as default strategies for resolving a destination name or converting a message,
57  * respectively.
58  *
59  * @author Mark Pollack
60  * @author Juergen Hoeller
61  * @since 1.1
62  * @see #setConnectionFactory
63  * @see #setPubSubDomain
64  * @see JmsTemplate
65  * @see org.springframework.jms.support.destination.DynamicDestinationResolver
66  * @see org.springframework.jms.support.converter.SimpleMessageConverter102
67  * @see javax.jms.Queue
68  * @see javax.jms.Topic
69  * @see javax.jms.QueueSession
70  * @see javax.jms.TopicSession
71  * @see javax.jms.QueueSender
72  * @see javax.jms.TopicPublisher
73  * @see javax.jms.QueueReceiver
74  * @see javax.jms.TopicSubscriber
75  */

76 public class JmsTemplate102 extends JmsTemplate {
77
78     /**
79      * Create a new JmsTemplate102 for bean-style usage.
80      * <p>Note: The ConnectionFactory has to be set before using the instance.
81      * This constructor can be used to prepare a JmsTemplate via a BeanFactory,
82      * typically setting the ConnectionFactory via setConnectionFactory.
83      * @see #setConnectionFactory
84      */

85     public JmsTemplate102() {
86         super();
87     }
88
89     /**
90      * Create a new JmsTemplate102, given a ConnectionFactory.
91      * @param connectionFactory the ConnectionFactory to obtain Connections from
92      * @param pubSubDomain whether the Publish/Subscribe domain (Topics) or
93      * Point-to-Point domain (Queues) should be used
94      * @see #setPubSubDomain
95      */

96     public JmsTemplate102(ConnectionFactory JavaDoc connectionFactory, boolean pubSubDomain) {
97         this();
98         setConnectionFactory(connectionFactory);
99         setPubSubDomain(pubSubDomain);
100         afterPropertiesSet();
101     }
102
103     /**
104      * Initialize the default implementations for the template's strategies:
105      * DynamicDestinationResolver and SimpleMessageConverter102.
106      * @see #setDestinationResolver
107      * @see #setMessageConverter
108      * @see org.springframework.jms.support.destination.DynamicDestinationResolver
109      * @see org.springframework.jms.support.converter.SimpleMessageConverter102
110      */

111     protected void initDefaultStrategies() {
112         setMessageConverter(new SimpleMessageConverter102());
113     }
114
115     /**
116      * In addition to checking if the connection factory is set, make sure
117      * that the supplied connection factory is of the appropriate type for
118      * the specified destination type: QueueConnectionFactory for queues,
119      * and TopicConnectionFactory for topics.
120      */

121     public void afterPropertiesSet() {
122         super.afterPropertiesSet();
123
124         // Make sure that the ConnectionFactory passed is consistent.
125
// Some provider implementations of the ConnectionFactory interface
126
// implement both domain interfaces under the cover, so just check if
127
// the selected domain is consistent with the type of connection factory.
128
if (isPubSubDomain()) {
129             if (!(getConnectionFactory() instanceof TopicConnectionFactory JavaDoc)) {
130                 throw new IllegalArgumentException JavaDoc(
131                         "Specified a Spring JMS 1.0.2 template for topics " +
132                         "but did not supply an instance of TopicConnectionFactory");
133             }
134         }
135         else {
136             if (!(getConnectionFactory() instanceof QueueConnectionFactory JavaDoc)) {
137                 throw new IllegalArgumentException JavaDoc(
138                         "Specified a Spring JMS 1.0.2 template for queues " +
139                         "but did not supply an instance of QueueConnectionFactory");
140             }
141         }
142     }
143
144
145     /**
146      * This implementation overrides the superclass method to accept either
147      * a QueueConnection or a TopicConnection, depending on the domain.
148      */

149     protected Connection JavaDoc getConnection(JmsResourceHolder holder) {
150         return holder.getConnection(isPubSubDomain() ? (Class JavaDoc) TopicConnection JavaDoc.class : QueueConnection JavaDoc.class);
151     }
152
153     /**
154      * This implementation overrides the superclass method to accept either
155      * a QueueSession or a TopicSession, depending on the domain.
156      */

157     protected Session JavaDoc getSession(JmsResourceHolder holder) {
158         return holder.getSession(isPubSubDomain() ? (Class JavaDoc) TopicSession JavaDoc.class : QueueSession JavaDoc.class);
159     }
160
161     /**
162      * This implementation overrides the superclass method to use JMS 1.0.2 API.
163      */

164     protected Connection JavaDoc createConnection() throws JMSException JavaDoc {
165         if (isPubSubDomain()) {
166             return ((TopicConnectionFactory JavaDoc) getConnectionFactory()).createTopicConnection();
167         }
168         else {
169             return ((QueueConnectionFactory JavaDoc) getConnectionFactory()).createQueueConnection();
170         }
171     }
172
173     /**
174      * This implementation overrides the superclass method to use JMS 1.0.2 API.
175      */

176     protected Session JavaDoc createSession(Connection JavaDoc con) throws JMSException JavaDoc {
177         if (isPubSubDomain()) {
178             return ((TopicConnection JavaDoc) con).createTopicSession(isSessionTransacted(), getSessionAcknowledgeMode());
179         }
180         else {
181             return ((QueueConnection JavaDoc) con).createQueueSession(isSessionTransacted(), getSessionAcknowledgeMode());
182         }
183     }
184
185     /**
186      * This implementation overrides the superclass method to use JMS 1.0.2 API.
187      */

188     protected MessageProducer JavaDoc doCreateProducer(Session JavaDoc session, Destination JavaDoc destination) throws JMSException JavaDoc {
189         if (isPubSubDomain()) {
190             return ((TopicSession JavaDoc) session).createPublisher((Topic JavaDoc) destination);
191         }
192         else {
193             return ((QueueSession JavaDoc) session).createSender((Queue JavaDoc) destination);
194         }
195     }
196
197     /**
198      * This implementation overrides the superclass method to use JMS 1.0.2 API.
199      */

200     protected MessageConsumer JavaDoc createConsumer(Session JavaDoc session, Destination JavaDoc destination, String JavaDoc messageSelector)
201             throws JMSException JavaDoc {
202
203         if (isPubSubDomain()) {
204             return ((TopicSession JavaDoc) session).createSubscriber((Topic JavaDoc) destination, messageSelector, isPubSubNoLocal());
205         }
206         else {
207             return ((QueueSession JavaDoc) session).createReceiver((Queue JavaDoc) destination, messageSelector);
208         }
209     }
210
211     /**
212      * This implementation overrides the superclass method to use JMS 1.0.2 API.
213      */

214     protected void doSend(MessageProducer JavaDoc producer, Message JavaDoc message) throws JMSException JavaDoc {
215         if (isPubSubDomain()) {
216             if (isExplicitQosEnabled()) {
217                 ((TopicPublisher JavaDoc) producer).publish(message, getDeliveryMode(), getPriority(), getTimeToLive());
218             }
219             else {
220                 ((TopicPublisher JavaDoc) producer).publish(message);
221             }
222         }
223         else {
224             if (isExplicitQosEnabled()) {
225                 ((QueueSender JavaDoc) producer).send(message, getDeliveryMode(), getPriority(), getTimeToLive());
226             }
227             else {
228                 ((QueueSender JavaDoc) producer).send(message);
229             }
230         }
231     }
232
233     /**
234      * This implementation overrides the superclass method to avoid using
235      * JMS 1.1's Session <code>getAcknowledgeMode()</code> method.
236      * The best we can do here is to check the setting on the template.
237      * @see #getSessionAcknowledgeMode()
238      */

239     protected boolean isClientAcknowledge(Session JavaDoc session) throws JMSException JavaDoc {
240         return (getSessionAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE);
241     }
242
243 }
244
Popular Tags