jL7: How to receive an HL7 message

Let’s assume we have a remote system which sends us HL7 messages on port xxx. We first need to create an MLLPMetaData object which will contain the information concerning host and port we’ll be listening to:

        private MLLPMetaData metaData = new MLLPMetaData(host, port);

The receiving of a message is done using an MLLPTransport object and invoking it’s receiveMessage method:

                MLLPTransport transport = new MLLPTransport();
                MLLPTransportable transportable = null;
                try {
                        transportable = transport.receiveMessage(metaData);
                } catch (IOException e) {
                        fail("IOException: " + e);
                }
                HL7Message msg = transportable.message;

The receiveMessage method returns an MLLPTransportable object which contains a reveived message in its Message member variable.

Since we usually do not want to close the listening socket after every message, the receiveMessage keeps an open connection which needs to be closed:

                try {
                        transport.disconnect();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

That’s it !

Leave a Reply

Your email address will not be published.