View Javadoc

1   /*
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "JMSQueueAdapter.java".  Description: 
10  "An implementation of JMSDestination that uses an underlying Queue." 
11  
12  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
13  2004.  All Rights Reserved. 
14  
15  Contributor(s): ______________________________________. 
16  
17  Alternatively, the contents of this file may be used under the terms of the 
18  GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
19  applicable instead of those above.  If you wish to allow use of your version of this 
20  file only under the terms of the GPL and not to allow others to use your version 
21  of this file under the MPL, indicate your decision by deleting  the provisions above 
22  and replace  them with the notice and other provisions required by the GPL License.  
23  If you do not delete the provisions above, a recipient may use your version of 
24  this file under either the MPL or the GPL. 
25  */
26  
27  package ca.uhn.hl7v2.protocol.impl;
28  
29  import javax.jms.Connection;
30  import javax.jms.JMSException;
31  import javax.jms.Message;
32  import javax.jms.Queue;
33  import javax.jms.QueueConnection;
34  import javax.jms.QueueReceiver;
35  import javax.jms.QueueSender;
36  import javax.jms.QueueSession;
37  import javax.jms.Session;
38  import javax.jms.TextMessage;
39  
40  import ca.uhn.hl7v2.protocol.JMSDestination;
41  
42  /**
43   * An implementation of <code>JMSDestination</code> that uses an underlying 
44   * Queue.  
45   * 
46   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
47   * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
48   */
49  public class JMSQueueAdapter implements JMSDestination {
50  
51      private QueueSession mySendingSession;
52      private QueueSession myReceivingSession; 
53      private QueueSender mySender;
54      private QueueReceiver myReceiver;
55      private QueueConnection myConnection;
56      private Queue myQueue;
57      private boolean myIsConnected;
58  
59      /**
60       * @param theConnection
61       * @param theDestination
62       */
63      public JMSQueueAdapter(QueueConnection theConnection, Queue theDestination) {
64          myConnection = theConnection;
65          myQueue = theDestination;
66      }
67  
68      /**
69       * @see ca.uhn.hl7v2.protocol.JMSDestination#getName()
70       */
71      public String getName() throws JMSException {
72          return myQueue.getQueueName();
73      }
74  
75      /**
76       * @see ca.uhn.hl7v2.protocol.JMSDestination#createMessage()
77       */
78      public TextMessage createMessage() throws JMSException {
79          return mySendingSession.createTextMessage();
80      }
81  
82      /** 
83       * @see ca.uhn.hl7v2.protocol.JMSDestination#connect()
84       */
85      public void connect() throws JMSException {
86          boolean transacted = false;
87          int ackMode = Session.AUTO_ACKNOWLEDGE;
88  
89          disconnect();
90  
91          mySendingSession = myConnection.createQueueSession(transacted, ackMode);
92          mySender = mySendingSession.createSender(myQueue);
93  
94          myReceivingSession = myConnection.createQueueSession(transacted, ackMode);
95          myReceiver = myReceivingSession.createReceiver(myQueue);
96          
97          myIsConnected = true;
98      }
99  
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 }