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 "JMSTopicAdapter.java".  Description: 
10  "An implementation of JMSDestination that uses an underlying 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  
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.Session;
33  import javax.jms.TextMessage;
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.JMSDestination;
41  
42  /**
43   * An implementation of <code>JMSDestination</code> that uses an underlying 
44   * Topic.
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 JMSTopicAdapter implements JMSDestination {
50  
51      private TopicSession mySendingSession;
52      private TopicSession myReceivingSession; 
53      private TopicPublisher myPublisher;
54      private TopicSubscriber mySubscriber;
55      private final TopicConnection myConnection;
56      private final Topic myTopic;
57      private boolean myIsConnected;
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 JMSTopicAdapter(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 (see JMS docs)
73       */
74      public JMSTopicAdapter(TopicConnection theConnection, Topic theDestination, String theMessageSelector) {
75          myConnection = theConnection;
76          myTopic = theDestination;
77      }
78  
79      /** 
80       * @see ca.uhn.hl7v2.protocol.JMSDestination#getName()
81       */
82      public String getName() throws JMSException {
83          return myTopic.getTopicName();
84      }
85  
86      /**
87       * @see ca.uhn.hl7v2.protocol.JMSDestination#createMessage()
88       */
89      public TextMessage createMessage() throws JMSException {
90          return mySendingSession.createTextMessage();
91      }
92  
93      /**
94       * @see ca.uhn.hl7v2.protocol.JMSDestination#connect()
95       */
96      public void connect() throws JMSException {
97          boolean transacted = false;
98          int ackMode = Session.AUTO_ACKNOWLEDGE;
99  
100         disconnect();
101         
102         mySendingSession = myConnection.createTopicSession(transacted, ackMode);
103         myPublisher = mySendingSession.createPublisher(myTopic);
104 
105         myReceivingSession = myConnection.createTopicSession(transacted, ackMode);
106         mySubscriber = myReceivingSession.createSubscriber(myTopic);
107         
108         myIsConnected = true;
109     }
110 
111     /**
112      * @see ca.uhn.hl7v2.protocol.JMSDestination#disconnect()
113      */
114     public void disconnect() throws JMSException {
115         myIsConnected = false;
116         
117         if (mySendingSession != null) {
118             mySendingSession.close();
119         }
120         if (myReceivingSession != null) {
121             myReceivingSession.close();
122         }
123     }
124 
125     /**
126      * @see ca.uhn.hl7v2.protocol.JMSDestination#isConnected()
127      */
128     public boolean isConnected() {
129         return myIsConnected;
130     }
131 
132     /** 
133      * @see ca.uhn.hl7v2.protocol.JMSDestination#send(javax.jms.Message)
134      */
135     public void send(Message theMessage) throws JMSException {
136         myPublisher.publish(theMessage);
137     }
138 
139     /**
140      * @see ca.uhn.hl7v2.protocol.JMSDestination#receive()
141      */
142     public Message receive() throws JMSException {
143         return mySubscriber.receive();
144     }
145 
146     /**
147      * @see ca.uhn.hl7v2.protocol.JMSDestination#getConnection()
148      */
149     public Connection getConnection() {
150         return myConnection;
151     }
152 
153 }