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