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 "JMSQueueTransport.java". Description:
10 "A TransportLayer that uses a JMS 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
39 import ca.uhn.hl7v2.protocol.TransportException;
40
41 /**
42 * A <code>TransportLayer</code> that uses a JMS Queue.
43 *
44 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
45 * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
46 */
47 public class JMSQueueTransport extends AbstractJMSTransport {
48
49 private QueueSession mySendingSession;
50 private QueueSession myReceivingSession;
51 private QueueSender mySender;
52 private QueueReceiver myReceiver;
53 private final QueueConnection myConnection;
54 private final Queue myQueue;
55
56 /**
57 * @param theConnection
58 * @param theDestination
59 */
60 public JMSQueueTransport(QueueConnection theConnection, Queue theDestination) {
61 myConnection = theConnection;
62 myQueue = theDestination;
63 }
64
65 /**
66 * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#getDestinationName()
67 */
68 protected String getDestinationName() throws JMSException {
69 return myQueue.getQueueName();
70 }
71
72 /**
73 * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#getConnection()
74 */
75 public Connection getConnection() {
76 return myConnection;
77 }
78
79 /**
80 * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#getMessage()
81 */
82 protected Message getMessage() throws JMSException {
83 return mySendingSession.createTextMessage();
84 }
85
86 /**
87 * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#sendJMS(Message) (javax.jms.Message)
88 */
89 protected void sendJMS(Message theMessage) throws JMSException {
90 mySender.send(theMessage);
91 }
92
93 /**
94 * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#receive()
95 */
96 protected Message receiveJMS() throws JMSException {
97 return myReceiver.receive();
98 }
99
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 }