View Javadoc

1   package ca.uhn.hl7v2.examples.hoh;
2   
3   import java.io.IOException;
4   
5   import ca.uhn.hl7v2.DefaultHapiContext;
6   import ca.uhn.hl7v2.HL7Exception;
7   import ca.uhn.hl7v2.app.Connection;
8   import ca.uhn.hl7v2.app.DefaultApplication;
9   import ca.uhn.hl7v2.app.SimpleServer;
10  import ca.uhn.hl7v2.hoh.llp.Hl7OverHttpLowerLayerProtocol;
11  import ca.uhn.hl7v2.hoh.util.ServerRoleEnum;
12  import ca.uhn.hl7v2.llp.LLPException;
13  import ca.uhn.hl7v2.llp.LowerLayerProtocol;
14  import ca.uhn.hl7v2.model.Message;
15  import ca.uhn.hl7v2.model.v25.message.ADT_A01;
16  import ca.uhn.hl7v2.parser.PipeParser;
17  import ca.uhn.hl7v2.protocol.ReceivingApplication;
18  
19  public class HohLlpExample {
20  
21  	public void receiveMessage() {
22  		
23  // START SNIPPET: server 
24  /*
25   * Sending a message with HAPI and HL7 over HTTP. First
26   * an LLP instance is created. Note that you must tell
27   * the LLP class whether it will be used in a client
28   * or a server.
29   */
30  LowerLayerProtocol llp;
31  llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.SERVER);
32  
33  /* 
34   * Create the server, and pass in the HoH LLP instance
35   * 
36   * Note that the HoH LLP implementation will not
37   * work in two-socket servers
38   */
39  PipeParser parser = PipeParser.getInstanceWithNoValidation();
40  int port = 8080;
41  SimpleServer server = new SimpleServer(port, llp, parser);
42  
43  // Register an application to the server, and start it
44  // You are now ready to receive HL7 messages!
45  server.registerApplication("*", "*", (ReceivingApplication)new DefaultApplication());
46  server.start();
47  // END SNIPPET: server 
48  
49  		
50  	}
51  	
52  	@SuppressWarnings("unused")
53     public void sendMessage() throws HL7Exception, IOException, LLPException {
54  		
55  // START SNIPPET: client 
56  /*
57   * Sending a message with HAPI and HL7 over HTTP. First
58   * an LLP instance is created. Note that you must tell
59   * the LLP class whether it will be used in a client
60   * or a server.
61   */
62  DefaultHapiContext ctx = new DefaultHapiContext();
63  
64  // Create an HoH LLP for the context
65  LowerLayerProtocol llp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT);
66  ctx.setLowerLayerProtocol(llp);
67  
68  // Use the LLP in a HapiContext to get a client connection
69  String host = "localhost";
70  int port = 8080;
71  boolean tls = false;
72  Connection connection = ctx.newClient(host, port, tls);
73  
74  // Create a message to send
75  ADT_A01 message = new ADT_A01();
76  message.initQuickstart("ADT", "A01", "P");
77  // ... populate message ...
78  
79  // Send the message
80  Message response = connection.getInitiator().sendAndReceive(message);
81  // END SNIPPET: client 
82  	
83  	}
84  	
85  }