KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > listener > DefaultMessageListenerContainer102


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.listener;
18
19 import javax.jms.Connection JavaDoc;
20 import javax.jms.Destination JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.MessageConsumer JavaDoc;
23 import javax.jms.Queue JavaDoc;
24 import javax.jms.QueueConnection JavaDoc;
25 import javax.jms.QueueConnectionFactory JavaDoc;
26 import javax.jms.QueueSession JavaDoc;
27 import javax.jms.Session JavaDoc;
28 import javax.jms.Topic JavaDoc;
29 import javax.jms.TopicConnection JavaDoc;
30 import javax.jms.TopicConnectionFactory JavaDoc;
31 import javax.jms.TopicSession JavaDoc;
32
33 import org.springframework.jms.connection.JmsResourceHolder;
34
35 /**
36  * A subclass of DefaultMessageListenerContainer that uses the JMS 1.0.2 specification,
37  * rather than the JMS 1.1 methods used by SimpleMessageListenerContainer itself.
38  * This class can be used for JMS 1.0.2 providers, offering the same facility as
39  * DefaultMessageListenerContainer does for JMS 1.1 providers.
40  *
41  * @author Juergen Hoeller
42  * @since 2.0
43  */

44 public class DefaultMessageListenerContainer102 extends DefaultMessageListenerContainer {
45
46     /**
47      * This implementation overrides the superclass method to accept either
48      * a QueueConnection or a TopicConnection, depending on the domain.
49      */

50     protected Connection JavaDoc getConnection(JmsResourceHolder holder) {
51         return holder.getConnection(isPubSubDomain() ? (Class JavaDoc) TopicConnection JavaDoc.class : QueueConnection JavaDoc.class);
52     }
53
54     /**
55      * This implementation overrides the superclass method to accept either
56      * a QueueSession or a TopicSession, depending on the domain.
57      */

58     protected Session JavaDoc getSession(JmsResourceHolder holder) {
59         return holder.getSession(isPubSubDomain() ? (Class JavaDoc) TopicSession JavaDoc.class : QueueSession JavaDoc.class);
60     }
61
62     /**
63      * This implementation overrides the superclass method to use JMS 1.0.2 API.
64      */

65     protected Connection JavaDoc createConnection() throws JMSException JavaDoc {
66         if (isPubSubDomain()) {
67             return ((TopicConnectionFactory JavaDoc) getConnectionFactory()).createTopicConnection();
68         }
69         else {
70             return ((QueueConnectionFactory JavaDoc) getConnectionFactory()).createQueueConnection();
71         }
72     }
73
74     /**
75      * This implementation overrides the superclass method to use JMS 1.0.2 API.
76      */

77     protected Session JavaDoc createSession(Connection JavaDoc con) throws JMSException JavaDoc {
78         if (isPubSubDomain()) {
79             return ((TopicConnection JavaDoc) con).createTopicSession(isSessionTransacted(), getSessionAcknowledgeMode());
80         }
81         else {
82             return ((QueueConnection JavaDoc) con).createQueueSession(isSessionTransacted(), getSessionAcknowledgeMode());
83         }
84     }
85
86     /**
87      * This implementation overrides the superclass method to use JMS 1.0.2 API.
88      */

89     protected MessageConsumer JavaDoc createConsumer(Session JavaDoc session, Destination JavaDoc destination) throws JMSException JavaDoc {
90         if (isPubSubDomain()) {
91             if (isSubscriptionDurable()) {
92                 return ((TopicSession JavaDoc) session).createDurableSubscriber(
93                         (Topic JavaDoc) destination, getDurableSubscriptionName(), getMessageSelector(), isPubSubNoLocal());
94             }
95             else {
96                 return ((TopicSession JavaDoc) session).createSubscriber(
97                         (Topic JavaDoc) destination, getMessageSelector(), isPubSubNoLocal());
98             }
99         }
100         else {
101             return ((QueueSession JavaDoc) session).createReceiver((Queue JavaDoc) destination, getMessageSelector());
102         }
103     }
104
105     /**
106      * This implementation overrides the superclass method to avoid using
107      * JMS 1.1's Session <code>getAcknowledgeMode()</code> method.
108      * The best we can do here is to check the setting on the listener container.
109      */

110     protected boolean isClientAcknowledge(Session JavaDoc session) throws JMSException JavaDoc {
111         return (getSessionAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE);
112     }
113
114 }
115
Popular Tags