001    /*
002    The contents of this file are subject to the Mozilla Public License Version 1.1 
003    (the "License"); you may not use this file except in compliance with the License. 
004    You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005    Software distributed under the License is distributed on an "AS IS" basis, 
006    WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007    specific language governing rights and limitations under the License. 
008    
009    The Original Code is "JMSTopicTransport.java".  Description: 
010    "A TransportLayer that uses a JMS Topic" 
011    
012    The Initial Developer of the Original Code is University Health Network. Copyright (C) 
013    2004.  All Rights Reserved. 
014    
015    Contributor(s): ______________________________________. 
016    
017    Alternatively, the contents of this file may be used under the terms of the 
018    GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are 
019    applicable instead of those above.  If you wish to allow use of your version of this 
020    file only under the terms of the GPL and not to allow others to use your version 
021    of this file under the MPL, indicate your decision by deleting  the provisions above 
022    and replace  them with the notice and other provisions required by the GPL License.  
023    If you do not delete the provisions above, a recipient may use your version of 
024    this file under either the MPL or the GPL. 
025    
026     * Created on 5-May-2004
027     */
028    package ca.uhn.hl7v2.protocol.impl;
029    
030    import javax.jms.Connection;
031    import javax.jms.JMSException;
032    import javax.jms.Message;
033    import javax.jms.Session;
034    import javax.jms.Topic;
035    import javax.jms.TopicConnection;
036    import javax.jms.TopicPublisher;
037    import javax.jms.TopicSession;
038    import javax.jms.TopicSubscriber;
039    
040    import ca.uhn.hl7v2.protocol.TransportException;
041    
042    /**
043     * A <code>TransportLayer</code> that uses a JMS Topic.
044     * 
045     * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
046     * @version $Revision: 1.1 $ updated on $Date: 2007/02/19 02:24:26 $ by $Author: jamesagnew $
047     */
048    public class JMSTopicTransport extends AbstractJMSTransport {
049    
050        private TopicSession mySendingSession;
051        private TopicSession myReceivingSession; 
052        private TopicPublisher myPublisher;
053        private TopicSubscriber mySubscriber;
054        private TopicConnection myConnection;
055        private Topic myTopic;
056        private String myMessageSelector;
057        
058    
059        /**
060         * @param theConnection the connection over which messages are written and read 
061         * @param theDestination the destination to/from which messages are written/read
062         */
063        public JMSTopicTransport(TopicConnection theConnection, Topic theDestination) {
064            myConnection = theConnection;
065            myTopic = theDestination;
066        }
067    
068        /**
069         * @param theConnection the connection over which messages are written and read 
070         * @param theDestination the destination to/from which messages are written/read
071         * @param theMessageSelector a JMS message selector which restricts the inbound 
072         *      messages that are received (se JMS docs)
073         */
074        public JMSTopicTransport(TopicConnection theConnection, Topic theDestination, String theMessageSelector) {
075            myConnection = theConnection;
076            myTopic = theDestination;
077            myMessageSelector = theMessageSelector;
078        }
079    
080        /** 
081         * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#getDestinationName()
082         */
083        protected String getDestinationName() throws JMSException {
084            return myTopic.getTopicName();
085        }
086    
087        /** 
088         * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#getConnection()
089         */
090        public Connection getConnection() {
091            return myConnection;
092        }
093    
094        /** 
095         * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#getMessage()
096         */
097        protected Message getMessage() throws JMSException {        
098            return mySendingSession.createTextMessage();
099        }
100    
101        /** 
102         * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#send(javax.jms.Message)
103         */
104        protected void sendJMS(Message theMessage) throws JMSException {
105            myPublisher.publish(theMessage);
106        }
107    
108        /** 
109         * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#receive()
110         */
111        protected Message receiveJMS() throws JMSException {
112            return mySubscriber.receive();
113        }
114    
115        /** 
116         * @see ca.uhn.hl7v2.protocol.AbstractJMSTransport#doConnect()
117         */
118        public void doConnect() throws TransportException {
119            boolean transacted = false;
120            int ackMode = Session.AUTO_ACKNOWLEDGE;
121    
122            doDisconnect();
123            try {
124                mySendingSession = myConnection.createTopicSession(transacted, ackMode);
125                myPublisher = mySendingSession.createPublisher(myTopic);
126    
127                myReceivingSession = myConnection.createTopicSession(transacted, ackMode);
128                mySubscriber = myReceivingSession.createSubscriber(myTopic);
129            } catch (JMSException e) {
130                throw new TransportException(e);
131            }
132        }
133    
134        /**
135         * @see ca.uhn.hl7v2.protocol.impl.AbstractTransport#doDisconnect()
136         */
137        public void doDisconnect() throws TransportException {
138            try {
139                if (mySendingSession != null) {
140                    mySendingSession.close();
141                }
142                if (myReceivingSession != null) {
143                    myReceivingSession.close();
144                }
145            } catch (JMSException e) {
146                throw new TransportException(e);
147            }        
148        }
149    
150    }