001 /*
002 The contents of this file are subject to the Mozilla Public License Version 1.1
003 (the "License"); you may not use this file except in compliance with the License.
004 You may obtain a copy of the License at http://www.mozilla.org/MPL/
005 Software distributed under the License is distributed on an "AS IS" basis,
006 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
007 specific language governing rights and limitations under the License.
008
009 The Original Code is "JMSTopicAdapter.java". Description:
010 "An implementation of JMSDestination that uses an underlying Topic."
011
012 The Initial Developer of the Original Code is University Health Network. Copyright (C)
013 2004. All Rights Reserved.
014
015 Contributor(s): ______________________________________.
016
017 Alternatively, the contents of this file may be used under the terms of the
018 GNU General Public License (the "GPL"), in which case the provisions of the GPL are
019 applicable instead of those above. If you wish to allow use of your version of this
020 file only under the terms of the GPL and not to allow others to use your version
021 of this file under the MPL, indicate your decision by deleting the provisions above
022 and replace them with the notice and other provisions required by the GPL License.
023 If you do not delete the provisions above, a recipient may use your version of
024 this file under either the MPL or the GPL.
025 */
026
027 package ca.uhn.hl7v2.protocol.impl;
028
029 import javax.jms.Connection;
030 import javax.jms.JMSException;
031 import javax.jms.Message;
032 import javax.jms.Session;
033 import javax.jms.TextMessage;
034 import javax.jms.Topic;
035 import javax.jms.TopicConnection;
036 import javax.jms.TopicPublisher;
037 import javax.jms.TopicSession;
038 import javax.jms.TopicSubscriber;
039
040 import ca.uhn.hl7v2.protocol.JMSDestination;
041
042 /**
043 * An implementation of <code>JMSDestination</code> that uses an underlying
044 * Topic.
045 *
046 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
047 * @version $Revision: 1.1 $ updated on $Date: 2007-02-19 02:24:26 $ by $Author: jamesagnew $
048 */
049 public class JMSTopicAdapter implements JMSDestination {
050
051 private TopicSession mySendingSession;
052 private TopicSession myReceivingSession;
053 private TopicPublisher myPublisher;
054 private TopicSubscriber mySubscriber;
055 private TopicConnection myConnection;
056 private Topic myTopic;
057 private String myMessageSelector;
058 private boolean myIsConnected;
059
060 /**
061 * @param theConnection the connection over which messages are written and read
062 * @param theDestination the destination to/from which messages are written/read
063 */
064 public JMSTopicAdapter(TopicConnection theConnection, Topic theDestination) {
065 myConnection = theConnection;
066 myTopic = theDestination;
067 }
068
069 /**
070 * @param theConnection the connection over which messages are written and read
071 * @param theDestination the destination to/from which messages are written/read
072 * @param theMessageSelector a JMS message selector which restricts the inbound
073 * messages that are received (see JMS docs)
074 */
075 public JMSTopicAdapter(TopicConnection theConnection, Topic theDestination, String theMessageSelector) {
076 myConnection = theConnection;
077 myTopic = theDestination;
078 myMessageSelector = theMessageSelector;
079 }
080
081 /**
082 * @see ca.uhn.hl7v2.protocol.JMSDestination#getName()
083 */
084 public String getName() throws JMSException {
085 return myTopic.getTopicName();
086 }
087
088 /**
089 * @see ca.uhn.hl7v2.protocol.JMSDestination#createMessage()
090 */
091 public TextMessage createMessage() throws JMSException {
092 return mySendingSession.createTextMessage();
093 }
094
095 /**
096 * @see ca.uhn.hl7v2.protocol.JMSDestination#connect()
097 */
098 public void connect() throws JMSException {
099 boolean transacted = false;
100 int ackMode = Session.AUTO_ACKNOWLEDGE;
101
102 disconnect();
103
104 mySendingSession = myConnection.createTopicSession(transacted, ackMode);
105 myPublisher = mySendingSession.createPublisher(myTopic);
106
107 myReceivingSession = myConnection.createTopicSession(transacted, ackMode);
108 mySubscriber = myReceivingSession.createSubscriber(myTopic);
109
110 myIsConnected = true;
111 }
112
113 /**
114 * @see ca.uhn.hl7v2.protocol.JMSDestination#disconnect()
115 */
116 public void disconnect() throws JMSException {
117 myIsConnected = false;
118
119 if (mySendingSession != null) {
120 mySendingSession.close();
121 }
122 if (myReceivingSession != null) {
123 myReceivingSession.close();
124 }
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#send(javax.jms.Message)
136 */
137 public void send(Message theMessage) throws JMSException {
138 myPublisher.publish(theMessage);
139 }
140
141 /**
142 * @see ca.uhn.hl7v2.protocol.JMSDestination#receive()
143 */
144 public Message receive() throws JMSException {
145 return mySubscriber.receive();
146 }
147
148 /**
149 * @see ca.uhn.hl7v2.protocol.JMSDestination#getConnection()
150 */
151 public Connection getConnection() {
152 return myConnection;
153 }
154
155 }