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