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 ca.uhn.hl7v2.DefaultHapiContext;
30  import ca.uhn.hl7v2.HapiContext;
31  import ca.uhn.hl7v2.app.Connection;
32  import ca.uhn.hl7v2.app.HL7Service;
33  import ca.uhn.hl7v2.app.Initiator;
34  import ca.uhn.hl7v2.model.Message;
35  import ca.uhn.hl7v2.parser.Parser;
36  import ca.uhn.hl7v2.protocol.ReceivingApplication;
37  
38  /**
39   * Example code
40   *
41   * @author Christian Ohr
42   */
43  public class LazilySendAMessage {
44  
45      /**
46       * Example for how to send messages on a lazy client connection
47       */
48      public static void main(String[] args) throws Exception {
49  
50  
51          HapiContext context = new DefaultHapiContext();
52          int port = 1011; // The port to listen on
53          boolean useTls = false; // Should we use TLS/SSL?
54  
55          // A connection object represents a socket attached to an HL7 server. Because the
56          // server has not been started yet, we need to construct a lazy client.
57          Connection connection = context.newLazyClient("localhost", port, useTls);
58          Initiator initiator = connection.getInitiator();
59  
60        /*
61         * Now create a server to listen for incoming
62         * messages.
63         */
64          HL7Service server = context.newServer(port, useTls);
65  
66        /*
67         * The server may have any number of "application" objects registered to
68         * handle messages. We are going to create an application to listen to
69         * ADT^A01 messages.
70         * 
71         * You might want to look at the source of ExampleReceiverApplication
72         * (it's a nested class below) to see how it works.
73         */
74          ReceivingApplication<Message> handler = new ExampleReceiverApplication();
75          server.registerApplication("*", "*", handler);
76  
77          // Start the server listening for messages
78          server.startAndWait();
79  
80        /*
81         * Note: if you don't want to wait for the server to initialize itself, it
82         * can start in the background:
83         */
84  
85          // server.start();
86  
87  
88          // Create a message to send it to the server. The physical connection is
89          // established transparently.
90          String msg = "MSH|^~\\&|HIS|RIH|EKG|EKG|199904140038||ADT^A01|12345|P|2.2\r"
91                  + "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"
92                  + "NK1|0222555|NOTREAL^JAMES^R|FA|STREET^OTHER STREET^CITY^ST^55566|(222)111-3333|(888)999-0000|||||||ORGANIZATION\r"
93                  + "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"
94                  + "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"
95                  + "AL1||SEV|001^POLLEN\r"
96                  + "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"
97                  + "IN1||022254P|4558PD|BLUE CROSS|STREET^OTHER STREET^CITY^ST^00990||(333)333-6666||221K|LENIX|||19980515|19990515|||PATIENT01 TEST D||||||||||||||||||02LL|022LP554";
98          Parser p = context.getPipeParser();
99          Message adt = p.parse(msg);
100         Message response = initiator.sendAndReceive(adt);
101 
102         String responseString = p.encode(response);
103         System.out.println("Received response:\n" + responseString);
104 
105       /*
106        * MSH|^~\&|||||20070218200627.515-0500||ACK|54|P|2.2 MSA|AA|12345
107        */
108 
109       /*
110        * Close the connection when you are done with it. If you are designing a
111        * system which will continuously send out messages, you may want to
112        * consider not closing the connection until you have no more messages to
113        * send out. This is more efficient, as most (if not all) HL7 receiving
114        * applications are capable of receiving lots of messages in a row over
115        * the same connection, even with a long delay between messages.
116        * 
117        * See
118        * http://hl7api.sourceforge.net/xref/ca/uhn/hl7v2/examples/SendLotsOfMessages.html 
119        * for an example of this.
120        */
121         connection.close();
122 
123         // Stop the receiving server and client
124         server.stopAndWait();
125 
126     }
127 
128 
129 }