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 "SendAndReceiveAMessage.java". Description:
10 * "Example Code"
11 *
12 * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13 * 2001. All Rights Reserved.
14 *
15 * Contributor(s): James Agnew
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.examples;
28
29 import java.util.Map;
30
31 import ca.uhn.hl7v2.DefaultHapiContext;
32 import ca.uhn.hl7v2.HapiContext;
33 import ca.uhn.hl7v2.app.*;
34 import ca.uhn.hl7v2.model.Message;
35 import ca.uhn.hl7v2.parser.Parser;
36 import ca.uhn.hl7v2.protocol.ReceivingApplication;
37 import ca.uhn.hl7v2.protocol.ReceivingApplicationExceptionHandler;
38
39 /**
40 * Example code
41 *
42 * @author James Agnew
43 * @author Christian Ohr
44 * @version $Revision: 1.2 $ updated on $Date: 2010-09-06 17:29:21 $ by $Author:
45 * jamesagnew $
46 */
47 public class SendAndReceiveAMessage {
48
49 /**
50 * Example for how to send messages out
51 */
52 public static void main(String[] args) throws Exception {
53
54 /*
55 * Before we can send, let's create a server to listen for incoming
56 * messages. The following section of code establishes a server listening
57 * on port 1011 for new connections.
58 */
59 int port = 1011; // The port to listen on
60 boolean useTls = false; // Should we use TLS/SSL?
61 HapiContext context = new DefaultHapiContext();
62 HL7Service server = context.newServer(port, useTls);
63
64 /*
65 * The server may have any number of "application" objects registered to
66 * handle messages. We are going to create an application to listen to
67 * ADT^A01 messages.
68 *
69 * You might want to look at the source of ExampleReceiverApplication
70 * (it's a nested class below) to see how it works.
71 */
72 ReceivingApplication<Message> handler = new ExampleReceiverApplication();
73 server.registerApplication("ADT", "A01", handler);
74
75 /*
76 * We are going to register the same application to handle ADT^A02
77 * messages. Of course, we coud just as easily have specified a different
78 * handler.
79 */
80 server.registerApplication("ADT", "A02", handler);
81
82 /*
83 * Another option would be to specify a single application to handle all
84 * messages, like this:
85 *
86 * server.registerApplication("*", "*", handler);
87 */
88
89 /*
90 * If you want to be notified any time a new connection comes in or is
91 * lost, you might also want to register a connection listener (see the
92 * bottom of this class to see what the listener looks like). It's fine
93 * to skip this step.
94 */
95 server.registerConnectionListener(new MyConnectionListener());
96
97 /*
98 * If you want to be notified any processing failures when receiving,
99 * processing, or responding to messages with the server, you can
100 * also register an exception handler. (See the bottom of this class
101 * to see what the listener looks like. ) It's also fine to skip this
102 * step, in which case exceptions will simply be logged.
103 */
104 server.setExceptionHandler(new MyExceptionHandler());
105
106 // Start the server listening for messages
107 server.startAndWait();
108
109 /*
110 * Note: if you don't want to wait for the server to initialize itself, it
111 * can start in the background:
112 */
113
114 // server.start();
115
116 /*
117 * All of the code above created a listening server, which waits for
118 * connections to come in and then handles any messages that arrive on
119 * those connections.
120 *
121 * Now, the code below creates a client, which will connect to our waiting
122 * server and send messages to it.
123 */
124
125 // Create a message to send
126 String msg = "MSH|^~\\&|HIS|RIH|EKG|EKG|199904140038||ADT^A01|12345|P|2.2\r"
127 + "PID|0001|00009874|00001122|A00977|SMITH^JOHN^M|MOM|19581119|F|NOTREAL^LINDA^M|C|564 SPRING ST^^NEEDHAM^MA^02494^US|0002|(818)565-1551|(425)828-3344|E|S|C|0000444444|252-00-4414||||SA|||SA||||NONE|V1|0001|I|D.ER^50A^M110^01|ER|P00055|11B^M011^02|070615^BATMAN^GEORGE^L|555888^NOTREAL^BOB^K^DR^MD|777889^NOTREAL^SAM^T^DR^MD^PHD|ER|D.WT^1A^M010^01|||ER|AMB|02|070615^NOTREAL^BILL^L|ER|000001916994|D||||||||||||||||GDD|WA|NORM|02|O|02|E.IN^02D^M090^01|E.IN^01D^M080^01|199904072124|199904101200|199904101200||||5555112333|||666097^NOTREAL^MANNY^P\r"
128 + "NK1|0222555|NOTREAL^JAMES^R|FA|STREET^OTHER STREET^CITY^ST^55566|(222)111-3333|(888)999-0000|||||||ORGANIZATION\r"
129 + "PV1|0001|I|D.ER^1F^M950^01|ER|P000998|11B^M011^02|070615^BATMAN^GEORGE^L|555888^OKNEL^BOB^K^DR^MD|777889^NOTREAL^SAM^T^DR^MD^PHD|ER|D.WT^1A^M010^01|||ER|AMB|02|070615^VOICE^BILL^L|ER|000001916994|D||||||||||||||||GDD|WA|NORM|02|O|02|E.IN^02D^M090^01|E.IN^01D^M080^01|199904072124|199904101200|||||5555112333|||666097^DNOTREAL^MANNY^P\r"
130 + "PV2|||0112^TESTING|55555^PATIENT IS NORMAL|NONE|||19990225|19990226|1|1|TESTING|555888^NOTREAL^BOB^K^DR^MD||||||||||PROD^003^099|02|ER||NONE|19990225|19990223|19990316|NONE\r"
131 + "AL1||SEV|001^POLLEN\r"
132 + "GT1||0222PL|NOTREAL^BOB^B||STREET^OTHER STREET^CITY^ST^77787|(444)999-3333|(222)777-5555||||MO|111-33-5555||||NOTREAL GILL N|STREET^OTHER STREET^CITY^ST^99999|(111)222-3333\r"
133 + "IN1||022254P|4558PD|BLUE CROSS|STREET^OTHER STREET^CITY^ST^00990||(333)333-6666||221K|LENIX|||19980515|19990515|||PATIENT01 TEST D||||||||||||||||||02LL|022LP554";
134 Parser p = context.getPipeParser();
135 Message adt = p.parse(msg);
136
137 // Remember, we created our HAPI Context above like so:
138 // HapiContext context = new DefaultHapiContext();
139
140 // A connection object represents a socket attached to an HL7 server
141 Connection connection = context.newClient("localhost", port, useTls);
142
143 // The initiator is used to transmit unsolicited messages
144 Initiator initiator = connection.getInitiator();
145 Message response = initiator.sendAndReceive(adt);
146
147 String responseString = p.encode(response);
148 System.out.println("Received response:\n" + responseString);
149
150 /*
151 * MSH|^~\&|||||20070218200627.515-0500||ACK|54|P|2.2 MSA|AA|12345
152 */
153
154 /*
155 * If you want to send another message to the same destination, it's fine
156 * to ask the context again for a client to attach to the same host/port.
157 * The context will be smart about it and return the same (already
158 * connected) client Connection instance, assuming it hasn't been closed.
159 */
160 connection = context.newClient("localhost", port, useTls);
161 initiator = connection.getInitiator();
162 response = initiator.sendAndReceive(adt);
163
164 /*
165 * Close the connection when you are done with it. If you are designing a
166 * system which will continuously send out messages, you may want to
167 * consider not closing the connection until you have no more messages to
168 * send out. This is more efficient, as most (if not all) HL7 receiving
169 * applications are capable of receiving lots of messages in a row over
170 * the same connection, even with a long delay between messages.
171 *
172 * See
173 * http://hl7api.sourceforge.net/xref/ca/uhn/hl7v2/examples/SendLotsOfMessages.html
174 * for an example of this.
175 */
176 connection.close();
177
178 // Stop the receiving server and client
179 server.stopAndWait();
180
181 }
182
183 /**
184 * Connection listener which is notified whenever a new
185 * connection comes in or is lost
186 */
187 public static class MyConnectionListener implements ConnectionListener {
188
189 public void connectionReceived(Connection theC) {
190 System.out.println("New connection received: " + theC.getRemoteAddress().toString());
191 }
192
193 public void connectionDiscarded(Connection theC) {
194 System.out.println("Lost connection from: " + theC.getRemoteAddress().toString());
195 }
196
197 }
198
199 /**
200 * Exception handler which is notified any time
201 */
202 public static class MyExceptionHandler implements ReceivingApplicationExceptionHandler {
203
204 /**
205 * Process an exception.
206 *
207 * @param theIncomingMessage
208 * the incoming message. This is the raw message which was
209 * received from the external system
210 * @param theIncomingMetadata
211 * Any metadata that accompanies the incoming message. See {@link ca.uhn.hl7v2.protocol.Transportable#getMetadata()}
212 * @param theOutgoingMessage
213 * the outgoing message. The response NAK message generated by
214 * HAPI.
215 * @param theE
216 * the exception which was received
217 * @return The new outgoing message. This can be set to the value provided
218 * by HAPI in <code>outgoingMessage</code>, or may be replaced with
219 * another message. <b>This method may not return <code>null</code></b>.
220 */
221 public String processException(String theIncomingMessage, Map<String, Object> theIncomingMetadata, String theOutgoingMessage, Exception theE) {
222
223 /*
224 * Here you can do any processing you like. If you want to change
225 * the response (NAK) message which will be returned you may do
226 * so, or just return the NAK which HAPI already created (theOutgoingMessage)
227 */
228
229 return theOutgoingMessage;
230 }
231
232 }
233
234 }