001/*
002The 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. 
004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005Software distributed under the License is distributed on an "AS IS" basis, 
006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007specific language governing rights and limitations under the License. 
008
009The Original Code is "JMSTopicAdapter.java".  Description: 
010"An implementation of JMSDestination that uses an underlying Topic." 
011
012The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0132004.  All Rights Reserved. 
014
015Contributor(s): ______________________________________. 
016
017Alternatively, the contents of this file may be used under the terms of the 
018GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
019applicable instead of those above.  If you wish to allow use of your version of this 
020file only under the terms of the GPL and not to allow others to use your version 
021of this file under the MPL, indicate your decision by deleting  the provisions above 
022and replace  them with the notice and other provisions required by the GPL License.  
023If you do not delete the provisions above, a recipient may use your version of 
024this file under either the MPL or the GPL. 
025*/
026
027package ca.uhn.hl7v2.protocol.impl;
028
029import javax.jms.Connection;
030import javax.jms.JMSException;
031import javax.jms.Message;
032import javax.jms.Session;
033import javax.jms.TextMessage;
034import javax.jms.Topic;
035import javax.jms.TopicConnection;
036import javax.jms.TopicPublisher;
037import javax.jms.TopicSession;
038import javax.jms.TopicSubscriber;
039
040import ca.uhn.hl7v2.protocol.JMSDestination;
041
042/**
043 * An implementation of <code>JMSDestination</code> that uses an underlying 
044 * Topic.
045 *   
046 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
047 * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
048 */
049public class JMSTopicAdapter implements JMSDestination {
050
051    private TopicSession mySendingSession;
052    private TopicSession myReceivingSession; 
053    private TopicPublisher myPublisher;
054    private TopicSubscriber mySubscriber;
055    private TopicConnection myConnection;
056    private Topic myTopic;
057    private boolean myIsConnected;
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 JMSTopicAdapter(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 (see JMS docs)
073     */
074    public JMSTopicAdapter(TopicConnection theConnection, Topic theDestination, String theMessageSelector) {
075        myConnection = theConnection;
076        myTopic = theDestination;
077    }
078
079    /** 
080     * @see ca.uhn.hl7v2.protocol.JMSDestination#getName()
081     */
082    public String getName() throws JMSException {
083        return myTopic.getTopicName();
084    }
085
086    /**
087     * @see ca.uhn.hl7v2.protocol.JMSDestination#createMessage()
088     */
089    public TextMessage createMessage() throws JMSException {
090        return mySendingSession.createTextMessage();
091    }
092
093    /**
094     * @see ca.uhn.hl7v2.protocol.JMSDestination#connect()
095     */
096    public void connect() throws JMSException {
097        boolean transacted = false;
098        int ackMode = Session.AUTO_ACKNOWLEDGE;
099
100        disconnect();
101        
102        mySendingSession = myConnection.createTopicSession(transacted, ackMode);
103        myPublisher = mySendingSession.createPublisher(myTopic);
104
105        myReceivingSession = myConnection.createTopicSession(transacted, ackMode);
106        mySubscriber = myReceivingSession.createSubscriber(myTopic);
107        
108        myIsConnected = true;
109    }
110
111    /**
112     * @see ca.uhn.hl7v2.protocol.JMSDestination#disconnect()
113     */
114    public void disconnect() throws JMSException {
115        myIsConnected = false;
116        
117        if (mySendingSession != null) {
118            mySendingSession.close();
119        }
120        if (myReceivingSession != null) {
121            myReceivingSession.close();
122        }
123    }
124
125    /**
126     * @see ca.uhn.hl7v2.protocol.JMSDestination#isConnected()
127     */
128    public boolean isConnected() {
129        return myIsConnected;
130    }
131
132    /** 
133     * @see ca.uhn.hl7v2.protocol.JMSDestination#send(javax.jms.Message)
134     */
135    public void send(Message theMessage) throws JMSException {
136        myPublisher.publish(theMessage);
137    }
138
139    /**
140     * @see ca.uhn.hl7v2.protocol.JMSDestination#receive()
141     */
142    public Message receive() throws JMSException {
143        return mySubscriber.receive();
144    }
145
146    /**
147     * @see ca.uhn.hl7v2.protocol.JMSDestination#getConnection()
148     */
149    public Connection getConnection() {
150        return myConnection;
151    }
152
153}