001: /*
002: * Copyright 2002-2007 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.connection;
018:
019: import javax.jms.Connection;
020: import javax.jms.ConnectionFactory;
021: import javax.jms.JMSException;
022: import javax.jms.QueueConnection;
023: import javax.jms.QueueConnectionFactory;
024: import javax.jms.Session;
025: import javax.jms.TopicConnection;
026: import javax.jms.TopicConnectionFactory;
027:
028: /**
029: * A subclass of {@link JmsTransactionManager} for the JMS 1.0.2 specification,
030: * not relying on JMS 1.1 methods like JmsTransactionManager itself.
031: * This class can be used for JMS 1.0.2 providers, offering the same API as
032: * JmsTransactionManager does for JMS 1.1 providers.
033: *
034: * <p>You need to set the {@link #setPubSubDomain "pubSubDomain" property},
035: * since this class will always explicitly differentiate between a
036: * {@link javax.jms.QueueConnection} and a {@link javax.jms.TopicConnection}.
037: *
038: * @author Juergen Hoeller
039: * @since 1.1
040: * @see #setConnectionFactory
041: * @see #setPubSubDomain
042: */
043: public class JmsTransactionManager102 extends JmsTransactionManager {
044:
045: private boolean pubSubDomain = false;
046:
047: /**
048: * Create a new JmsTransactionManager102 for bean-style usage.
049: * <p>Note: The ConnectionFactory has to be set before using the instance.
050: * This constructor can be used to prepare a JmsTemplate via a BeanFactory,
051: * typically setting the ConnectionFactory via setConnectionFactory.
052: * @see #setConnectionFactory
053: */
054: public JmsTransactionManager102() {
055: super ();
056: }
057:
058: /**
059: * Create a new JmsTransactionManager102, given a ConnectionFactory.
060: * @param connectionFactory the ConnectionFactory to manage transactions for
061: * @param pubSubDomain whether the Publish/Subscribe domain (Topics) or
062: * Point-to-Point domain (Queues) should be used
063: * @see #setPubSubDomain
064: */
065: public JmsTransactionManager102(
066: ConnectionFactory connectionFactory, boolean pubSubDomain) {
067: setConnectionFactory(connectionFactory);
068: this .pubSubDomain = pubSubDomain;
069: afterPropertiesSet();
070: }
071:
072: /**
073: * Configure the transaction manager with knowledge of the JMS domain used.
074: * This tells the JMS 1.0.2 provider which class hierarchy to use for creating
075: * Connections and Sessions.
076: * <p>Default is Point-to-Point (Queues).
077: * @param pubSubDomain <code>true</code> for Publish/Subscribe domain (Topics),
078: * <code>false</code> for Point-to-Point domain (Queues)
079: */
080: public void setPubSubDomain(boolean pubSubDomain) {
081: this .pubSubDomain = pubSubDomain;
082: }
083:
084: /**
085: * Return whether the Publish/Subscribe domain (Topics) is used.
086: * Otherwise, the Point-to-Point domain (Queues) is used.
087: */
088: public boolean isPubSubDomain() {
089: return this .pubSubDomain;
090: }
091:
092: /**
093: * In addition to checking if the connection factory is set, make sure
094: * that the supplied connection factory is of the appropriate type for
095: * the specified destination type: QueueConnectionFactory for queues,
096: * and TopicConnectionFactory for topics.
097: */
098: public void afterPropertiesSet() {
099: super .afterPropertiesSet();
100:
101: // Make sure that the ConnectionFactory passed is consistent.
102: // Some provider implementations of the ConnectionFactory interface
103: // implement both domain interfaces under the cover, so just check if
104: // the selected domain is consistent with the type of connection factory.
105: if (isPubSubDomain()) {
106: if (!(getConnectionFactory() instanceof TopicConnectionFactory)) {
107: throw new IllegalArgumentException(
108: "Specified a Spring JMS 1.0.2 transaction manager for topics "
109: + "but did not supply an instance of TopicConnectionFactory");
110: }
111: } else {
112: if (!(getConnectionFactory() instanceof QueueConnectionFactory)) {
113: throw new IllegalArgumentException(
114: "Specified a Spring JMS 1.0.2 transaction manager for queues "
115: + "but did not supply an instance of QueueConnectionFactory");
116: }
117: }
118: }
119:
120: /**
121: * This implementation overrides the superclass method to use JMS 1.0.2 API.
122: */
123: protected Connection createConnection() throws JMSException {
124: if (isPubSubDomain()) {
125: return ((TopicConnectionFactory) getConnectionFactory())
126: .createTopicConnection();
127: } else {
128: return ((QueueConnectionFactory) getConnectionFactory())
129: .createQueueConnection();
130: }
131: }
132:
133: /**
134: * This implementation overrides the superclass method to use JMS 1.0.2 API.
135: */
136: protected Session createSession(Connection con) throws JMSException {
137: if (isPubSubDomain()) {
138: return ((TopicConnection) con).createTopicSession(true,
139: Session.AUTO_ACKNOWLEDGE);
140: } else {
141: return ((QueueConnection) con).createQueueSession(true,
142: Session.AUTO_ACKNOWLEDGE);
143: }
144: }
145:
146: }
|