001: /*
002: * Copyright 2002-2005 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 JmsTransactionManager that uses the JMS 1.0.2 specification,
030: * rather than the JMS 1.1 methods used by 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 pubSubDomain property accordingly, as this
035: * class will always create either QueueConnections/QueueSessions or
036: * TopicConnections/TopicSessions.
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 JmsTransactionManager102 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. Default is Point-to-Point (Queues).
076: * @param pubSubDomain true for Publish/Subscribe domain (Topics),
077: * false for Point-to-Point domain (Queues)
078: */
079: public void setPubSubDomain(boolean pubSubDomain) {
080: this .pubSubDomain = pubSubDomain;
081: }
082:
083: /**
084: * Return whether the Publish/Subscribe domain (Topics) is used.
085: * Otherwise, the Point-to-Point domain (Queues) is used.
086: */
087: public boolean isPubSubDomain() {
088: return pubSubDomain;
089: }
090:
091: /**
092: * In addition to checking if the connection factory is set, make sure
093: * that the supplied connection factory is of the appropriate type for
094: * the specified destination type: QueueConnectionFactory for queues,
095: * and TopicConnectionFactory for topics.
096: */
097: public void afterPropertiesSet() {
098: super .afterPropertiesSet();
099:
100: // Make sure that the ConnectionFactory passed is consistent.
101: // Some provider implementations of the ConnectionFactory interface
102: // implement both domain interfaces under the cover, so just check if
103: // the selected domain is consistent with the type of connection factory.
104: if (isPubSubDomain()) {
105: if (!(getConnectionFactory() instanceof TopicConnectionFactory)) {
106: throw new IllegalArgumentException(
107: "Specified a Spring JMS 1.0.2 transaction manager for topics "
108: + "but did not supply an instance of TopicConnectionFactory");
109: }
110: } else {
111: if (!(getConnectionFactory() instanceof QueueConnectionFactory)) {
112: throw new IllegalArgumentException(
113: "Specified a Spring JMS 1.0.2 transaction manager for queues "
114: + "but did not supply an instance of QueueConnectionFactory");
115: }
116: }
117: }
118:
119: /**
120: * This implementation overrides the superclass method to use JMS 1.0.2 API.
121: */
122: protected Connection createConnection() throws JMSException {
123: if (isPubSubDomain()) {
124: return ((TopicConnectionFactory) getConnectionFactory())
125: .createTopicConnection();
126: } else {
127: return ((QueueConnectionFactory) getConnectionFactory())
128: .createQueueConnection();
129: }
130: }
131:
132: /**
133: * This implementation overrides the superclass method to use JMS 1.0.2 API.
134: */
135: protected Session createSession(Connection con) throws JMSException {
136: if (isPubSubDomain()) {
137: return ((TopicConnection) con).createTopicSession(true,
138: Session.AUTO_ACKNOWLEDGE);
139: } else {
140: return ((QueueConnection) con).createQueueSession(true,
141: Session.AUTO_ACKNOWLEDGE);
142: }
143: }
144:
145: }
|