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 "JMSTopicTransport.java".  Description: 
10  "A TransportLayer that uses a JMS Topic" 
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   * Created on 5-May-2004
27   */
28  package ca.uhn.hl7v2.protocol.impl;
29  
30  import javax.jms.Connection;
31  import javax.jms.JMSException;
32  import javax.jms.Message;
33  import javax.jms.Session;
34  import javax.jms.Topic;
35  import javax.jms.TopicConnection;
36  import javax.jms.TopicPublisher;
37  import javax.jms.TopicSession;
38  import javax.jms.TopicSubscriber;
39  
40  import ca.uhn.hl7v2.protocol.TransportException;
41  
42  /**
43   * A <code>TransportLayer</code> that uses a JMS Topic.
44   * 
45   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
46   * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
47   */
48  public class JMSTopicTransport extends AbstractJMSTransport {
49  
50      private TopicSession mySendingSession;
51      private TopicSession myReceivingSession; 
52      private TopicPublisher myPublisher;
53      private TopicSubscriber mySubscriber;
54      private final TopicConnection myConnection;
55      private final Topic myTopic;
56      private String myMessageSelector;
57      
58  
59      /**
60       * @param theConnection the connection over which messages are written and read 
61       * @param theDestination the destination to/from which messages are written/read
62       */
63      public JMSTopicTransport(TopicConnection theConnection, Topic theDestination) {
64          myConnection = theConnection;
65          myTopic = theDestination;
66      }
67  
68      /**
69       * @param theConnection the connection over which messages are written and read 
70       * @param theDestination the destination to/from which messages are written/read
71       * @param theMessageSelector a JMS message selector which restricts the inbound 
72       *      messages that are received (se JMS docs)
73       */
74      public JMSTopicTransport(TopicConnection theConnection, Topic theDestination, String theMessageSelector) {
75          myConnection = theConnection;
76          myTopic = theDestination;
77          myMessageSelector = theMessageSelector;
78      }
79  
80      /** 
81       * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#getDestinationName()
82       */
83      protected String getDestinationName() throws JMSException {
84          return myTopic.getTopicName();
85      }
86  
87      /** 
88       * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#getConnection()
89       */
90      public Connection getConnection() {
91          return myConnection;
92      }
93  
94      /** 
95       * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#getMessage()
96       */
97      protected Message getMessage() throws JMSException {        
98          return mySendingSession.createTextMessage();
99      }
100 
101     /** 
102      * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#sendJMS(Message) (javax.jms.Message)
103      */
104     protected void sendJMS(Message theMessage) throws JMSException {
105         myPublisher.publish(theMessage);
106     }
107 
108     /** 
109      * @see ca.uhn.hl7v2.protocol.impl.AbstractJMSTransport#receive()
110      */
111     protected Message receiveJMS() throws JMSException {
112         return mySubscriber.receive();
113     }
114 
115     public void doConnect() throws TransportException {
116         boolean transacted = false;
117         int ackMode = Session.AUTO_ACKNOWLEDGE;
118 
119         doDisconnect();
120         try {
121             mySendingSession = myConnection.createTopicSession(transacted, ackMode);
122             myPublisher = mySendingSession.createPublisher(myTopic);
123 
124             myReceivingSession = myConnection.createTopicSession(transacted, ackMode);
125             mySubscriber = myReceivingSession.createSubscriber(myTopic);
126         } catch (JMSException e) {
127             throw new TransportException(e);
128         }
129     }
130 
131     /**
132      * @see ca.uhn.hl7v2.protocol.impl.AbstractTransport#doDisconnect()
133      */
134     public void doDisconnect() throws TransportException {
135         try {
136             if (mySendingSession != null) {
137                 mySendingSession.close();
138             }
139             if (myReceivingSession != null) {
140                 myReceivingSession.close();
141             }
142         } catch (JMSException e) {
143             throw new TransportException(e);
144         }        
145     }
146 
147 }