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 "JMSQueueTransport.java".  Description: 
010"A TransportLayer that uses a JMS 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;
038
039import ca.uhn.hl7v2.protocol.TransportException;
040
041/**
042 * A <code>TransportLayer</code> that uses a JMS Queue.
043 *   
044 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
045 * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
046 */
047public class JMSQueueTransport extends AbstractJMSTransport {
048
049    private QueueSession mySendingSession;
050    private QueueSession myReceivingSession; 
051    private QueueSender mySender;
052    private QueueReceiver myReceiver;
053    private QueueConnection myConnection;
054    private Queue myQueue;
055    
056    /**
057     * @param theConnection
058     * @param theDestination
059     */
060    public JMSQueueTransport(QueueConnection theConnection, Queue theDestination) {
061        myConnection = theConnection;
062        myQueue = theDestination;
063    }
064
065    /**
066     * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#getDestinationName()
067     */
068    protected String getDestinationName() throws JMSException {
069        return myQueue.getQueueName();
070    }
071
072    /**
073     * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#getConnection()
074     */
075    public Connection getConnection() {
076        return myConnection;
077    }
078
079    /**
080     * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#getMessage()
081     */
082    protected Message getMessage() throws JMSException {        
083        return mySendingSession.createTextMessage();
084    }
085
086    /** 
087     * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#send(javax.jms.Message)
088     */
089    protected void sendJMS(Message theMessage) throws JMSException {
090        mySender.send(theMessage);
091    }
092
093    /**
094     * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#receive()
095     */
096    protected Message receiveJMS() throws JMSException {
097        return myReceiver.receive();
098    }
099
100    /** 
101     * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#connect()
102     */
103    public void doConnect() throws TransportException {
104        boolean transacted = false;
105        int ackMode = Session.AUTO_ACKNOWLEDGE;
106
107        doDisconnect();
108        try {
109            mySendingSession = myConnection.createQueueSession(transacted, ackMode);
110            mySender = mySendingSession.createSender(myQueue);
111
112            myReceivingSession = myConnection.createQueueSession(transacted, ackMode);
113            myReceiver = myReceivingSession.createReceiver(myQueue);
114        } catch (JMSException e) {
115            throw new TransportException(e);
116        }
117    }
118
119    /**
120     * @see ca.uhn.hl7v2.protocol.impl.AbstractTransport#doDisconnect()
121     */
122    public void doDisconnect() throws TransportException {
123        try {
124            if (mySendingSession != null) {
125                mySendingSession.close();
126            }
127            if (myReceivingSession != null) {
128                myReceivingSession.close();
129            }
130        } catch (JMSException e) {
131            throw new TransportException(e);
132        }        
133    }
134
135}