001: /*
002: * Copyright 2002-2006 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.listener;
018:
019: import javax.jms.Connection;
020: import javax.jms.Destination;
021: import javax.jms.JMSException;
022: import javax.jms.MessageConsumer;
023: import javax.jms.Queue;
024: import javax.jms.QueueConnection;
025: import javax.jms.QueueConnectionFactory;
026: import javax.jms.QueueSession;
027: import javax.jms.Session;
028: import javax.jms.Topic;
029: import javax.jms.TopicConnection;
030: import javax.jms.TopicConnectionFactory;
031: import javax.jms.TopicSession;
032:
033: import org.springframework.jms.connection.JmsResourceHolder;
034:
035: /**
036: * A subclass of DefaultMessageListenerContainer that uses the JMS 1.0.2 specification,
037: * rather than the JMS 1.1 methods used by SimpleMessageListenerContainer itself.
038: * This class can be used for JMS 1.0.2 providers, offering the same facility as
039: * DefaultMessageListenerContainer does for JMS 1.1 providers.
040: *
041: * @author Juergen Hoeller
042: * @since 2.0
043: */
044: public class DefaultMessageListenerContainer102 extends
045: DefaultMessageListenerContainer {
046:
047: /**
048: * This implementation overrides the superclass method to accept either
049: * a QueueConnection or a TopicConnection, depending on the domain.
050: */
051: protected Connection getConnection(JmsResourceHolder holder) {
052: return holder
053: .getConnection(isPubSubDomain() ? (Class) TopicConnection.class
054: : QueueConnection.class);
055: }
056:
057: /**
058: * This implementation overrides the superclass method to accept either
059: * a QueueSession or a TopicSession, depending on the domain.
060: */
061: protected Session getSession(JmsResourceHolder holder) {
062: return holder
063: .getSession(isPubSubDomain() ? (Class) TopicSession.class
064: : QueueSession.class);
065: }
066:
067: /**
068: * This implementation overrides the superclass method to use JMS 1.0.2 API.
069: */
070: protected Connection createConnection() throws JMSException {
071: if (isPubSubDomain()) {
072: return ((TopicConnectionFactory) getConnectionFactory())
073: .createTopicConnection();
074: } else {
075: return ((QueueConnectionFactory) getConnectionFactory())
076: .createQueueConnection();
077: }
078: }
079:
080: /**
081: * This implementation overrides the superclass method to use JMS 1.0.2 API.
082: */
083: protected Session createSession(Connection con) throws JMSException {
084: if (isPubSubDomain()) {
085: return ((TopicConnection) con).createTopicSession(
086: isSessionTransacted(), getSessionAcknowledgeMode());
087: } else {
088: return ((QueueConnection) con).createQueueSession(
089: isSessionTransacted(), getSessionAcknowledgeMode());
090: }
091: }
092:
093: /**
094: * This implementation overrides the superclass method to use JMS 1.0.2 API.
095: */
096: protected MessageConsumer createConsumer(Session session,
097: Destination destination) throws JMSException {
098: if (isPubSubDomain()) {
099: if (isSubscriptionDurable()) {
100: return ((TopicSession) session)
101: .createDurableSubscriber((Topic) destination,
102: getDurableSubscriptionName(),
103: getMessageSelector(), isPubSubNoLocal());
104: } else {
105: return ((TopicSession) session).createSubscriber(
106: (Topic) destination, getMessageSelector(),
107: isPubSubNoLocal());
108: }
109: } else {
110: return ((QueueSession) session).createReceiver(
111: (Queue) destination, getMessageSelector());
112: }
113: }
114:
115: /**
116: * This implementation overrides the superclass method to avoid using
117: * JMS 1.1's Session <code>getAcknowledgeMode()</code> method.
118: * The best we can do here is to check the setting on the listener container.
119: */
120: protected boolean isClientAcknowledge(Session session)
121: throws JMSException {
122: return (getSessionAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE);
123: }
124:
125: }
|