001: package org.objectweb.celtix.bus.transports.jms;
002:
003: import javax.jms.Destination;
004: import javax.jms.JMSException;
005: import javax.jms.MessageConsumer;
006: import javax.jms.MessageProducer;
007: import javax.jms.Session;
008: import javax.jms.TemporaryQueue;
009:
010: /**
011: * Encapsulates pooled session, unidentified producer, destination &
012: * associated consumer (certain elements may be null depending on the
013: * context).
014: * <p>
015: * Currently only the point-to-point domain is supported,
016: * though the intention is to genericize this to the pub-sub domain
017: * also.
018: *
019: * @author Eoghan Glynn
020: */
021: public class PooledSession {
022: private final Session theSession;
023: private Destination theDestination;
024: private final MessageProducer theProducer;
025: private MessageConsumer theConsumer;
026:
027: private String correlationID;
028:
029: /**
030: * Constructor.
031: */
032: PooledSession(Session session, Destination destination,
033: MessageProducer producer, MessageConsumer consumer) {
034: theSession = session;
035: theDestination = destination;
036: theProducer = producer;
037: theConsumer = consumer;
038: }
039:
040: /**
041: * @return the pooled JMS Session
042: */
043: Session session() {
044: return theSession;
045: }
046:
047: /**
048: * @return the destination associated with the consumer
049: */
050: Destination destination() {
051: return theDestination;
052: }
053:
054: /**
055: * @param destination the destination to encapsulate
056: */
057: void destination(Destination destination) {
058: theDestination = destination;
059: }
060:
061: /**
062: * @return the unidentified producer
063: */
064: MessageProducer producer() {
065: return theProducer;
066: }
067:
068: /**
069: * @return the per-destination consumer
070: */
071: MessageConsumer consumer() {
072: return theConsumer;
073: }
074:
075: /**
076: * @return messageSelector if any set.
077: */
078:
079: String getCorrelationID() throws JMSException {
080: if (correlationID == null && theConsumer != null) {
081: //Must be request/reply
082: String selector = theConsumer.getMessageSelector();
083:
084: if (selector != null
085: && selector.startsWith("JMSCorrelationID")) {
086: int i = selector.indexOf('\'');
087: correlationID = selector.substring(i + 1, selector
088: .length() - 1);
089: }
090: }
091:
092: return correlationID;
093: }
094:
095: /**
096: * @param consumer the consumer to encapsulate
097: */
098: void consumer(MessageConsumer consumer) {
099: theConsumer = consumer;
100: }
101:
102: void close() throws JMSException {
103: if (theProducer != null) {
104: theProducer.close();
105: }
106:
107: if (theConsumer != null) {
108: theConsumer.close();
109: }
110:
111: if (theDestination instanceof TemporaryQueue) {
112: ((TemporaryQueue) theDestination).delete();
113: }
114:
115: if (theSession != null) {
116: theSession.close();
117: }
118: }
119: }
|