1 package ca.uhn.hl7v2.examples;
2
3 import java.io.FileNotFoundException;
4 import java.io.FileReader;
5 import java.io.IOException;
6
7 import ca.uhn.hl7v2.DefaultHapiContext;
8 import ca.uhn.hl7v2.HL7Exception;
9 import ca.uhn.hl7v2.HapiContext;
10 import ca.uhn.hl7v2.app.Connection;
11 import ca.uhn.hl7v2.llp.LLPException;
12 import ca.uhn.hl7v2.model.Message;
13 import ca.uhn.hl7v2.util.Hl7InputStreamMessageIterator;
14
15 public class SendLotsOfMessages {
16
17 public static void main(String[] args) throws FileNotFoundException, HL7Exception, LLPException {
18
19
20
21
22
23
24
25
26
27
28
29
30 FileReader reader = new FileReader("message_file.txt");
31
32
33 Hl7InputStreamMessageIterator iter = new Hl7InputStreamMessageIterator(reader);
34
35
36 HapiContext context = new DefaultHapiContext();
37
38 Connection conn = null;
39 while (iter.hasNext()) {
40
41
42
43
44
45
46
47
48
49 if (conn == null) {
50 boolean useTls = false;
51 int port = 8888;
52 conn = context.newClient("localhost", port, useTls);
53 }
54
55 try {
56 Message next = iter.next();
57 Message response = conn.getInitiator().sendAndReceive(next);
58 System.out.println("Sent message. Response was " + response.encode());
59 } catch (IOException e) {
60 System.out.println("Didn't send out this message!");
61 e.printStackTrace();
62
63
64 try {
65 conn.close();
66 } catch (IOException ignored) {
67 }
68 conn = null;
69
70 }
71
72 }
73
74 }
75
76 }