1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 import javax.jms.TextMessage;
39
40 import ca.uhn.hl7v2.protocol.JMSDestination;
41
42
43
44
45
46
47
48
49 public class JMSQueueAdapter implements JMSDestination {
50
51 private QueueSession mySendingSession;
52 private QueueSession myReceivingSession;
53 private QueueSender mySender;
54 private QueueReceiver myReceiver;
55 private final QueueConnection myConnection;
56 private final Queue myQueue;
57 private boolean myIsConnected;
58
59
60
61
62
63 public JMSQueueAdapter(QueueConnection theConnection, Queue theDestination) {
64 myConnection = theConnection;
65 myQueue = theDestination;
66 }
67
68
69
70
71 public String getName() throws JMSException {
72 return myQueue.getQueueName();
73 }
74
75
76
77
78 public TextMessage createMessage() throws JMSException {
79 return mySendingSession.createTextMessage();
80 }
81
82
83
84
85 public void connect() throws JMSException {
86 boolean transacted = false;
87 int ackMode = Session.AUTO_ACKNOWLEDGE;
88
89 disconnect();
90
91 mySendingSession = myConnection.createQueueSession(transacted, ackMode);
92 mySender = mySendingSession.createSender(myQueue);
93
94 myReceivingSession = myConnection.createQueueSession(transacted, ackMode);
95 myReceiver = myReceivingSession.createReceiver(myQueue);
96
97 myIsConnected = true;
98 }
99
100
101
102
103 public void disconnect() throws JMSException {
104 myIsConnected = false;
105 if (mySendingSession != null) {
106 mySendingSession.close();
107 }
108 if (myReceivingSession != null) {
109 myReceivingSession.close();
110 }
111 }
112
113
114
115
116 public void send(Message theMessage) throws JMSException {
117 mySender.send(theMessage);
118 }
119
120
121
122
123 public Message receive() throws JMSException {
124 return myReceiver.receive();
125 }
126
127
128
129
130 public boolean isConnected() {
131 return myIsConnected;
132 }
133
134
135
136
137 public Connection getConnection() {
138 return myConnection;
139 }
140
141 }