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 TopicConnection myConnection;
55      private 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#send(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     /** 
116      * @see ca.uhn.hl7v2.protocol.AbstractJMSTransport#doConnect()
117      */
118     public void doConnect() throws TransportException {
119         boolean transacted = false;
120         int ackMode = Session.AUTO_ACKNOWLEDGE;
121 
122         doDisconnect();
123         try {
124             mySendingSession = myConnection.createTopicSession(transacted, ackMode);
125             myPublisher = mySendingSession.createPublisher(myTopic);
126 
127             myReceivingSession = myConnection.createTopicSession(transacted, ackMode);
128             mySubscriber = myReceivingSession.createSubscriber(myTopic);
129         } catch (JMSException e) {
130             throw new TransportException(e);
131         }
132     }
133 
134     /**
135      * @see ca.uhn.hl7v2.protocol.impl.AbstractTransport#doDisconnect()
136      */
137     public void doDisconnect() throws TransportException {
138         try {
139             if (mySendingSession != null) {
140                 mySendingSession.close();
141             }
142             if (myReceivingSession != null) {
143                 myReceivingSession.close();
144             }
145         } catch (JMSException e) {
146             throw new TransportException(e);
147         }        
148     }
149 
150 }