KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > transports > jms > PooledSession


1 package org.objectweb.celtix.bus.transports.jms;
2
3 import javax.jms.Destination JavaDoc;
4 import javax.jms.JMSException JavaDoc;
5 import javax.jms.MessageConsumer JavaDoc;
6 import javax.jms.MessageProducer JavaDoc;
7 import javax.jms.Session JavaDoc;
8 import javax.jms.TemporaryQueue JavaDoc;
9
10 /**
11  * Encapsulates pooled session, unidentified producer, destination &
12  * associated consumer (certain elements may be null depending on the
13  * context).
14  * <p>
15  * Currently only the point-to-point domain is supported,
16  * though the intention is to genericize this to the pub-sub domain
17  * also.
18  *
19  * @author Eoghan Glynn
20  */

21 public class PooledSession {
22     private final Session JavaDoc theSession;
23     private Destination JavaDoc theDestination;
24     private final MessageProducer JavaDoc theProducer;
25     private MessageConsumer JavaDoc theConsumer;
26
27     private String JavaDoc correlationID;
28
29     /**
30      * Constructor.
31      */

32     PooledSession(Session JavaDoc session,
33                   Destination JavaDoc destination,
34                   MessageProducer JavaDoc producer,
35                   MessageConsumer JavaDoc consumer) {
36         theSession = session;
37         theDestination = destination;
38         theProducer = producer;
39         theConsumer = consumer;
40     }
41     
42
43     /**
44      * @return the pooled JMS Session
45      */

46     Session JavaDoc session() {
47         return theSession;
48     }
49
50
51     /**
52      * @return the destination associated with the consumer
53      */

54     Destination JavaDoc destination() {
55         return theDestination;
56     }
57
58
59     /**
60      * @param destination the destination to encapsulate
61      */

62     void destination(Destination JavaDoc destination) {
63         theDestination = destination;
64     }
65
66
67     /**
68      * @return the unidentified producer
69      */

70     MessageProducer JavaDoc producer() {
71         return theProducer;
72     }
73
74
75     /**
76      * @return the per-destination consumer
77      */

78     MessageConsumer JavaDoc consumer() {
79         return theConsumer;
80     }
81
82     /**
83      * @return messageSelector if any set.
84      */

85     
86     String JavaDoc getCorrelationID() throws JMSException JavaDoc {
87         if (correlationID == null && theConsumer != null) {
88             //Must be request/reply
89
String JavaDoc selector = theConsumer.getMessageSelector();
90             
91             if (selector != null && selector.startsWith("JMSCorrelationID")) {
92                 int i = selector.indexOf('\'');
93                 correlationID = selector.substring(i + 1, selector.length() - 1);
94             }
95         }
96         
97         return correlationID;
98     }
99
100     /**
101      * @param consumer the consumer to encapsulate
102      */

103     void consumer(MessageConsumer JavaDoc consumer) {
104         theConsumer = consumer;
105     }
106
107
108     void close() throws JMSException JavaDoc {
109         if (theProducer != null) {
110             theProducer.close();
111         }
112
113         if (theConsumer != null) {
114             theConsumer.close();
115         }
116
117         if (theDestination instanceof TemporaryQueue JavaDoc) {
118             ((TemporaryQueue JavaDoc)theDestination).delete();
119         }
120
121         if (theSession != null) {
122             theSession.close();
123         }
124     }
125 }
126
Popular Tags