View Javadoc

1   package ca.uhn.hl7v2.examples.upgrade;
2   
3   import java.io.IOException;
4   import java.net.ServerSocket;
5   import java.util.Map;
6   
7   import ca.uhn.hl7v2.DefaultHapiContext;
8   import ca.uhn.hl7v2.HL7Exception;
9   import ca.uhn.hl7v2.app.HL7Service;
10  import ca.uhn.hl7v2.model.Message;
11  import ca.uhn.hl7v2.protocol.ReceivingApplicationException;
12  import ca.uhn.hl7v2.protocol.impl.AppRoutingDataImpl;
13  import ca.uhn.hl7v2.protocol.impl.ApplicationRouterImpl;
14  import ca.uhn.hl7v2.protocol.impl.HL7Server;
15  import ca.uhn.hl7v2.protocol.impl.NullSafeStorage;
16  
17  public class UpgradeToHapi2_1 {
18  
19     /**
20      * @param args
21      * @throws IOException
22      */
23     public static void main(String[] args) throws IOException {
24  
25     }
26  
27     @SuppressWarnings("unused")
28     private void oldWay() throws IOException {
29  
30        // START SNIPPET: oldServer
31        // Create a router
32        ApplicationRouterImpl router = new ApplicationRouterImpl();
33        AppRoutingDataImpl routingData = new AppRoutingDataImpl("*", "*", "*", "*");
34  
35        // Create an application and bind it to the router
36        MyReceivingApplication application = new MyReceivingApplication();
37        router.bindApplication(routingData, application);
38  
39        // Create a server and start it
40        ServerSocket socket = new ServerSocket(8888);
41        NullSafeStorage storage = new NullSafeStorage();
42        HL7Server hl7Server = new HL7Server(socket, router, storage);
43        hl7Server.start(null);
44        // END SNIPPET: oldServer
45  
46     }
47  
48     @SuppressWarnings("unused")
49     private void newWay() {
50  
51        // START SNIPPET: newServer
52        // Create a context and ask it for a service
53        DefaultHapiContext ctx = new DefaultHapiContext();
54        HL7Service server = ctx.newServer(8888, false);
55  
56        // Create an application and bind it to the server
57        MyReceivingApplication application = new MyReceivingApplication();
58        server.registerApplication(application);
59  
60        // Start the server
61        server.start();
62        // END SNIPPET: newServer
63  
64     }
65  
66  // START SNIPPET: newApp
67  public class MyReceivingApplication implements ca.uhn.hl7v2.protocol.ReceivingApplication {
68  
69     public Message processMessage(Message theMessage, Map<String, Object> theMetadata) throws ReceivingApplicationException, HL7Exception {
70  
71        // ...Do something with theMessage...
72  
73        try {
74           return theMessage.generateACK();
75        } catch (IOException e) {
76           throw new HL7Exception(e);
77        }
78     }
79  
80     public boolean canProcess(Message theMessage) {
81        return true;
82     }
83  
84  }
85  
86  // END SNIPPET: newApp
87  
88  // START SNIPPET: oldApp
89  public class MyApplication implements ca.uhn.hl7v2.app.Application {
90  
91     public Message processMessage(Message theMessage) throws HL7Exception {
92        // ...Do something with theMessage...
93        try {
94           return theMessage.generateACK();
95        } catch (IOException e) {
96           throw new HL7Exception(e);
97        }
98     }
99  
100    public boolean canProcess(Message theMessage) {
101       return true;
102    }
103 
104 }
105 // END SNIPPET: oldApp
106 
107 }