public class HL7Sender {
public void send(String host, int port) throws HL7Exception {
System.out.println("[ Executing HL7Sender : HOST:" + host + " ;port :" + port + " ]");
// The connection hub connects to listening servers
ConnectionHub connectionHub = ConnectionHub.getInstance();
// A connection object represents a socket attached to an HL7 server
Connection connection = connectionHub
.attach(host, port, new PipeParser(), MinLowerLayerProtocol.class);
// The initiator is used to transmit unsolicited messages
Initiator initiator = connection.getInitiator();
HL7Message sampleMessage = new HL7Message();
//send
Message response = null;
try {
response = initiator.sendAndReceive(sampleMessage.getHL7Message());
PipeParser parser = new PipeParser();
String responseString = parser.encode(response);
System.out.println("Received response:\n" + responseString);
} catch (LLPException e) {
System.out.println("Error : " + e);
} catch (IOException e) {
System.out.println("Error : " + e);
}
// Close the connection and server
connectionHub.discard(connection);
}