KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > listener > adapter > MessageListenerAdapter102


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.adapter;
18
19 import javax.jms.Destination JavaDoc;
20 import javax.jms.JMSException JavaDoc;
21 import javax.jms.Message JavaDoc;
22 import javax.jms.MessageProducer JavaDoc;
23 import javax.jms.Queue JavaDoc;
24 import javax.jms.QueueSender JavaDoc;
25 import javax.jms.QueueSession JavaDoc;
26 import javax.jms.Session JavaDoc;
27 import javax.jms.Topic JavaDoc;
28 import javax.jms.TopicPublisher JavaDoc;
29 import javax.jms.TopicSession JavaDoc;
30
31 import org.springframework.jms.support.JmsUtils;
32 import org.springframework.jms.support.converter.SimpleMessageConverter102;
33
34 /**
35  * A {@link MessageListenerAdapter} subclass that uses the JMS 1.0.2 specification,
36  * rather than the JMS 1.1 methods used by MessageListenerAdapter itself.
37  *
38  * <p>This class can be used for JMS 1.0.2 providers, offering the same facility
39  * as MessageListenerAdapter does for JMS 1.1 providers.
40  * @author Juergen Hoeller
41  * @author Rick Evans
42  * @since 2.0
43  */

44 public class MessageListenerAdapter102 extends MessageListenerAdapter {
45
46     /**
47      * Create a new instance of the {@link MessageListenerAdapter102} class
48      * with the default settings.
49      */

50     public MessageListenerAdapter102() {
51     }
52
53     /**
54      * Create a new instance of the {@link MessageListenerAdapter102} class
55      * for the given delegate.
56      * @param delegate the target object to delegate message listening to
57      */

58     public MessageListenerAdapter102(Object JavaDoc delegate) {
59         super(delegate);
60     }
61
62
63     /**
64      * Initialize the default implementations for the adapter's strategies:
65      * SimpleMessageConverter102.
66      * @see #setMessageConverter
67      * @see org.springframework.jms.support.converter.SimpleMessageConverter102
68      */

69     protected void initDefaultStrategies() {
70         setMessageConverter(new SimpleMessageConverter102());
71     }
72
73     /**
74      * Overrides the superclass method to use the JMS 1.0.2 API to send a response.
75      * <p>Uses the JMS pub-sub API if the given destination is a topic,
76      * else uses the JMS queue API.
77      */

78     protected void sendResponse(Session JavaDoc session, Destination JavaDoc destination, Message JavaDoc response) throws JMSException JavaDoc {
79         MessageProducer JavaDoc producer = null;
80         try {
81             if (destination instanceof Topic JavaDoc) {
82                 producer = ((TopicSession JavaDoc) session).createPublisher((Topic JavaDoc) destination);
83                 postProcessProducer(producer, response);
84                 ((TopicPublisher JavaDoc) producer).publish(response);
85             }
86             else {
87                 producer = ((QueueSession JavaDoc) session).createSender((Queue JavaDoc) destination);
88                 postProcessProducer(producer, response);
89                 ((QueueSender JavaDoc) producer).send(response);
90             }
91         }
92         finally {
93             JmsUtils.closeMessageProducer(producer);
94         }
95     }
96
97 }
98
Popular Tags