HL7v2 - MLLP Using HAPI JAVA

Vibin GuevaraVibin Guevara
2 min read

Table of contents

When the message is transmitted using MLLP protocol from one to another. There will be some junk characters that will pollute the message when it is received over the receiving end. But this will not happen every time.

There are cases where, the junk characters will be handled by the source systems like Mirth or any integration engines. But in most cases if the source system uses MLLP protocol for transferring the message then it is always possible that some junk characters will be sent along & then it is certainly possible that it will contain a lot of junk characters in it. It is essential to handle this when you’re receiving the HL7 message in JAVA, instead of using any integration engines for receiving the data.

Because if these characters exist then you will get the error as shown below:

Determine encoding for message. The following is the first 50 chars of the message for reference, although this may not be where the issue is:

MSH|^~\&|OPENEMR|DrJhonDoe|TEST|UNKNOWN|20210216190432||ADT^A01^ADT_A01|60b647d4-b5a5-4fae-a928-d4a3849de3c8|T|2.5

To fix this issue you need to make sure the incoming bytes from the source system needs to be handled properly and encoded with definite character set.

The detailed information on having this issue is produced here on the stack overflow link. I was shocked on looking at the upvote count of 6, in a world where I thought no one would be doing this, it came out as a surprise for me https://stackoverflow.com/questions/66230745/hl7v2-hapi-parser-exception-while-receiving-data-via-tcp-ip

Intermediate fix for the problem:

HAPI provides a very sophisticated way of receiving the incoming data properly using MLLP protocol. once data is received as input stream then we need to pass that to MinLowerLayerProtocol() to make it behave like a MLLP data only then HAPI handles the junk characters.

InputStream in = connection.getInputStream();
MinLowerLayerProtocol data = new MinLowerLayerProtocol();
HL7Reader val = data.getReader(in);
val.setInputStream(in);
String hl7Message = val.getMessage();

Here, we are receiving the data from the MLLP protocol as a InputStream and then passing the InputStream data to MinLowerLayerProtocol data type. This will solve the problem of having the junk characters coming along with the hl7 message via MLLP protocol.

But this is not the complete solution. That is because, when the source keeps the connection open then reading as the input stream will have a big problem. If you look at the implementation of the Mirth integration engine, then you can see that they will read the messages of MLLP in the frame level. So the solution work only when the connection is no kept open on the EHR/EMR side else it will be a problem.

0
Subscribe to my newsletter

Read articles from Vibin Guevara directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vibin Guevara
Vibin Guevara

I'm a software developer and the startup enthusiast from Banglore city of India. I develop, build and support system that are digital. Hands on with multiple technologies and love to collaborate.