Hl7 Simple Java Client
HL7 Sender Java Code
HL7 Receiver Java Code
This main class is used to
Simple Application class that is used to receive the message and create and send the acknowledgement
Event Router class that is used to rout the message to the appropriate versions and types.
| package hl7sender; import ca.uhn.hl7v2.DefaultHapiContext; import ca.uhn.hl7v2.HapiContext; import ca.uhn.hl7v2.app.Connection; import ca.uhn.hl7v2.app.Initiator; import ca.uhn.hl7v2.model.Message; import ca.uhn.hl7v2.parser.Parser; public class hl7sender { public static void main(String[] args) { final int PORT_NUMBER = 9014; // In HAPI, almost all things revolve around a context object. HapiContext context = new DefaultHapiContext(); try { String adtMessage = "MSH|^~\\&|NES|NINTENDO|TESTSYSTEM|TESTFACILITY|" + "20010101000000||ADT^A01|Q123456789T123456789X123456|P|2.3\r" + "EVN|A01|20010101000000|||^KOOPA^BOWSER\r" + "PID|1||123456789|0123456789^AA^^JP|BROS^MARIO||19850101000000|" + "M|||123 FAKE STREET^MARIO \\T\\ LUIGI BROS PLACE^TOADSTOOL" + "KINGDOM^NES^A1B2C3^JP^HOME^^1234|1234|(555)555-0123^HOME^JP:" + "1234567|||S|MSH|12345678|||||||0|||||N\r" + "NK1|1|PEACH^PRINCESS|SO|ANOTHER CASTLE^^TOADSTOOL KINGDOM" + "^NES^^JP|(123)555-1234|(123)555-2345|NOK\r" + "NK1|2|TOADSTOOL^PRINCESS|SO|YET ANOTHER CASTLE^^TOADSTOOL" + "KINGDOM^NES^^JP|(123)555-3456|(123)555-4567|EMC\r" + "PV1|1|O|ABCD^EFGH||||123456^DINO^YOSHI^^^^^^MSRM^CURRENT" + "^^^NEIGHBOURHOOD DR NBR|^DOG^DUCKHUNT^^^^^^^CURRENT||CRD||" + "|||||123456^DINO^YOSHI^^^^^^MSRM^CURRENT^^^NEIGHBOURHOOD DR" + " NBR|AO|0123456789|1|||||||||||||||||||MSH||A|||20010101000000\r" + "IN1|1|PAR^PARENT||||LUIGI\r"; // create a new MLLP client over the specified port. Connection connection = context.newClient("localhost", PORT_NUMBER, false); // The initiator which will be used to transmit our message. Initiator initiator = connection.getInitiator(); // send the created HL7 message over the connection established Parser parser = context.getPipeParser(); System.out.println("Sending message:" + "\n" + adtMessage); Message response = initiator.sendAndReceive(parser.parse(adtMessage)); // display the message response received from the remote party String responseString = parser.encode(response); System.out.println("Received response:\n" + responseString.replaceAll("\r", "\n")); } catch (Exception e) { e.printStackTrace(); } } } |
HL7 Receiver Java Code
This main class is used to
| package hl7receiver; import ca.uhn.hl7v2.DefaultHapiContext; import ca.uhn.hl7v2.HapiContext; import ca.uhn.hl7v2.app.HL7Service; import ca.uhn.hl7v2.parser.Parser; import ca.uhn.hl7v2.parser.PipeParser; public class hl7receiver { public static void main(String args[]) { final int PORT_NUMBER = 9014; //In HAPI,almost all things revolve around a context object HapiContext context = new DefaultHapiContext(); try { Parser parser = new PipeParser(); HL7Service ourHl7Server = context.newServer(PORT_NUMBER, false); // You can set up routing rules for your HL7 listener by extending the AppRoutingData class ourHl7Server.registerApplication(new RegisterEventRouter(), new ourSimpleApplication()); ourHl7Server.setShutdownTimeout(10000); ourHl7Server.startAndWait(); } catch (Exception e) { e.printStackTrace(); } } } |
Simple Application class that is used to receive the message and create and send the acknowledgement
| package hl7receiver; import ca.uhn.hl7v2.HL7Exception; import ca.uhn.hl7v2.model.Message; import ca.uhn.hl7v2.parser.PipeParser; import ca.uhn.hl7v2.protocol.ReceivingApplication; import ca.uhn.hl7v2.protocol.ReceivingApplicationException; import java.io.IOException; import java.util.Map; public class ourSimpleApplication implements ReceivingApplication { PipeParser pipeParser = new PipeParser(); public Message processMessage(Message message, Map map) throws ReceivingApplicationException, HL7Exception { System.out.println("Received Message\n" + pipeParser.encode(message)); Message response = null; try { response = message.generateACK(); System.out.println("Sent Response\n" + pipeParser.encode(response)); } catch (IOException e) { e.printStackTrace(); } return response; } public boolean canProcess(Message message) { return true; } } |
Event Router class that is used to rout the message to the appropriate versions and types.
| package hl7receiver; import ca.uhn.hl7v2.protocol.ApplicationRouter; public class RegisterEventRouter implements ApplicationRouter.AppRoutingData { public String getMessageType() { return "*"; } public String getTriggerEvent() { return "*"; } public String getProcessingId() { return "*"; } public String getVersion() { return "*"; } } |


