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 "JMSQueueAdapter.java".  Description: 
010"An implementation of JMSDestination that uses an underlying Queue." 
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.Queue;
033import javax.jms.QueueConnection;
034import javax.jms.QueueReceiver;
035import javax.jms.QueueSender;
036import javax.jms.QueueSession;
037import javax.jms.Session;
038import javax.jms.TextMessage;
039
040import ca.uhn.hl7v2.protocol.JMSDestination;
041
042/**
043 * An implementation of <code>JMSDestination</code> that uses an underlying 
044 * Queue.  
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 JMSQueueAdapter implements JMSDestination {
050
051    private QueueSession mySendingSession;
052    private QueueSession myReceivingSession; 
053    private QueueSender mySender;
054    private QueueReceiver myReceiver;
055    private QueueConnection myConnection;
056    private Queue myQueue;
057    private boolean myIsConnected;
058
059    /**
060     * @param theConnection
061     * @param theDestination
062     */
063    public JMSQueueAdapter(QueueConnection theConnection, Queue theDestination) {
064        myConnection = theConnection;
065        myQueue = theDestination;
066    }
067
068    /**
069     * @see ca.uhn.hl7v2.protocol.JMSDestination#getName()
070     */
071    public String getName() throws JMSException {
072        return myQueue.getQueueName();
073    }
074
075    /**
076     * @see ca.uhn.hl7v2.protocol.JMSDestination#createMessage()
077     */
078    public TextMessage createMessage() throws JMSException {
079        return mySendingSession.createTextMessage();
080    }
081
082    /** 
083     * @see ca.uhn.hl7v2.protocol.JMSDestination#connect()
084     */
085    public void connect() throws JMSException {
086        boolean transacted = false;
087        int ackMode = Session.AUTO_ACKNOWLEDGE;
088
089        disconnect();
090
091        mySendingSession = myConnection.createQueueSession(transacted, ackMode);
092        mySender = mySendingSession.createSender(myQueue);
093
094        myReceivingSession = myConnection.createQueueSession(transacted, ackMode);
095        myReceiver = myReceivingSession.createReceiver(myQueue);
096        
097        myIsConnected = true;
098    }
099
100    /** 
101     * @see ca.uhn.hl7v2.protocol.JMSDestination#disconnect()
102     */
103    public void disconnect() throws JMSException {
104        myIsConnected = false;
105        if (mySendingSession != null) {
106            mySendingSession.close();
107        }
108        if (myReceivingSession != null) {
109            myReceivingSession.close();
110        }
111    }
112
113    /** 
114     * @see ca.uhn.hl7v2.protocol.JMSDestination#send(javax.jms.Message)
115     */
116    public void send(Message theMessage) throws JMSException {
117        mySender.send(theMessage);
118    }
119
120    /** 
121     * @see ca.uhn.hl7v2.protocol.JMSDestination#receive()
122     */
123    public Message receive() throws JMSException {
124        return myReceiver.receive();
125    }
126
127    /** 
128     * @see ca.uhn.hl7v2.protocol.JMSDestination#isConnected()
129     */
130    public boolean isConnected() {
131        return myIsConnected;
132    }
133
134    /**
135     * @see ca.uhn.hl7v2.protocol.JMSDestination#getConnection()
136     */
137    public Connection getConnection() {
138        return myConnection;
139    }
140
141}