KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > connection > JmsTransactionManager102


1 /*
2  * Copyright 2002-2005 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.connection;
18
19 import javax.jms.Connection JavaDoc;
20 import javax.jms.ConnectionFactory JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.QueueConnection JavaDoc;
23 import javax.jms.QueueConnectionFactory JavaDoc;
24 import javax.jms.Session JavaDoc;
25 import javax.jms.TopicConnection JavaDoc;
26 import javax.jms.TopicConnectionFactory JavaDoc;
27
28 /**
29  * A subclass of JmsTransactionManager that uses the JMS 1.0.2 specification,
30  * rather than the JMS 1.1 methods used by JmsTransactionManager itself.
31  * This class can be used for JMS 1.0.2 providers, offering the same API as
32  * JmsTransactionManager does for JMS 1.1 providers.
33  *
34  * <p>You need to set the pubSubDomain property accordingly, as this
35  * class will always create either QueueConnections/QueueSessions or
36  * TopicConnections/TopicSessions.
37  *
38  * @author Juergen Hoeller
39  * @since 1.1
40  * @see #setConnectionFactory
41  * @see #setPubSubDomain
42  */

43 public class JmsTransactionManager102 extends JmsTransactionManager {
44
45     private boolean pubSubDomain = false;
46
47
48     /**
49      * Create a new JmsTransactionManager102 for bean-style usage.
50      * <p>Note: The ConnectionFactory has to be set before using the instance.
51      * This constructor can be used to prepare a JmsTemplate via a BeanFactory,
52      * typically setting the ConnectionFactory via setConnectionFactory.
53      * @see #setConnectionFactory
54      */

55     public JmsTransactionManager102() {
56         super();
57     }
58
59     /**
60      * Create a new JmsTransactionManager102, given a ConnectionFactory.
61      * @param connectionFactory the ConnectionFactory to manage transactions for
62      * @param pubSubDomain whether the Publish/Subscribe domain (Topics) or
63      * Point-to-Point domain (Queues) should be used
64      * @see #setPubSubDomain
65      */

66     public JmsTransactionManager102(ConnectionFactory JavaDoc connectionFactory, boolean pubSubDomain) {
67         setConnectionFactory(connectionFactory);
68         this.pubSubDomain = pubSubDomain;
69         afterPropertiesSet();
70     }
71
72
73     /**
74      * Configure the JmsTransactionManager102 with knowledge of the JMS domain used.
75      * This tells the JMS 1.0.2 provider which class hierarchy to use for creating
76      * Connections and Sessions. Default is Point-to-Point (Queues).
77      * @param pubSubDomain true for Publish/Subscribe domain (Topics),
78      * false for Point-to-Point domain (Queues)
79      */

80     public void setPubSubDomain(boolean pubSubDomain) {
81         this.pubSubDomain = pubSubDomain;
82     }
83
84     /**
85      * Return whether the Publish/Subscribe domain (Topics) is used.
86      * Otherwise, the Point-to-Point domain (Queues) is used.
87      */

88     public boolean isPubSubDomain() {
89         return pubSubDomain;
90     }
91
92
93     /**
94      * In addition to checking if the connection factory is set, make sure
95      * that the supplied connection factory is of the appropriate type for
96      * the specified destination type: QueueConnectionFactory for queues,
97      * and TopicConnectionFactory for topics.
98      */

99     public void afterPropertiesSet() {
100         super.afterPropertiesSet();
101
102         // Make sure that the ConnectionFactory passed is consistent.
103
// Some provider implementations of the ConnectionFactory interface
104
// implement both domain interfaces under the cover, so just check if
105
// the selected domain is consistent with the type of connection factory.
106
if (isPubSubDomain()) {
107             if (!(getConnectionFactory() instanceof TopicConnectionFactory JavaDoc)) {
108                 throw new IllegalArgumentException JavaDoc(
109                         "Specified a Spring JMS 1.0.2 transaction manager for topics " +
110                         "but did not supply an instance of TopicConnectionFactory");
111             }
112         }
113         else {
114             if (!(getConnectionFactory() instanceof QueueConnectionFactory JavaDoc)) {
115                 throw new IllegalArgumentException JavaDoc(
116                         "Specified a Spring JMS 1.0.2 transaction manager for queues " +
117                         "but did not supply an instance of QueueConnectionFactory");
118             }
119         }
120     }
121
122
123     /**
124      * This implementation overrides the superclass method to use JMS 1.0.2 API.
125      */

126     protected Connection JavaDoc createConnection() throws JMSException JavaDoc {
127         if (isPubSubDomain()) {
128             return ((TopicConnectionFactory JavaDoc) getConnectionFactory()).createTopicConnection();
129         }
130         else {
131             return ((QueueConnectionFactory JavaDoc) getConnectionFactory()).createQueueConnection();
132         }
133     }
134
135     /**
136      * This implementation overrides the superclass method to use JMS 1.0.2 API.
137      */

138     protected Session JavaDoc createSession(Connection JavaDoc con) throws JMSException JavaDoc {
139         if (isPubSubDomain()) {
140             return ((TopicConnection JavaDoc) con).createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
141         }
142         else {
143             return ((QueueConnection JavaDoc) con).createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
144         }
145     }
146
147 }
148
Popular Tags