LibraryLink ToToggle FramesPrintFeedback

Chapter 19. HL7

The hl7 component is used for working with the HL7 MLLP protocol and the HL7 model using the HAPI library.

This component supports the following:

  • HL7 MLLP codec for Mina

  • Agnostic data format using either plain String objects or HAPI HL7 model objects.

  • Type Converter from/to HAPI and String

  • HL7 DataFormat using HAPI library

  • Even more easy-of-use as its integrated well with the camel-mina component.

HL7 is often used with the HL7 MLLP protocol that is a text based TCP socket based protocol. This component ships with a Mina Codec that conforms to the MLLP protocol so you can easily expose a HL7 listener that accepts HL7 requests over the TCP transport.

To expose a HL7 listener service we reuse the existing camel-mina component where we just use the HL7MLLPCodec as codec.

The HL7 MLLP codec has the following options:

Name Default Value Description
startByte 0x0b The start byte spanning the HL7 payload. Is the HL7 default value of 0x0b (11 decimal)
endByte1 0x1c The first end byte spanning the HL7 payload. Is the HL7 default value of 0x1c (28 decimal)
endByte2 0x0d The 2nd end byte spanning the HL7 payload. Is the HL7 default value of 0x0d (13 decimal)
charset JVM Default The encoding (is a charset name) to use for the codec. If not provided FUSE Mediation Router will use the JVM default Charset.
convertLFtoCR true Will convert \n to \r (0x0d, 13 decimal) as HL7 usually uses \r as segment terminators. The HAPI library requires to use \r.

In our spring xml file we configure an endpoint to listen for HL7 requests using TCP:

        <endpoint id="hl7listener" uri="mina:tcp://localhost:8888?sync=true&amp;codec=hl7codec"/>

Notice we configure it to use camel-mina with TCP on the localhost on port 8888. We use the sync=true to indicate that this listener is synchronous and therefore will return a HL7 response to the caller. Then we setup mina to use our HL7 codec with codec=hl7codec. Notice that hl7codec is just a spring bean id, so we could have named it mygreatcodecforhl7 or whatever. The codec is also setup in the spring xml file:

    <bean id="hl7codec" class="org.apache.camel.component.hl7.HL7MLLPCodec">
        <property name="charset" value="iso-8859-1"/>
    </bean>

And here we configure the charset encoding to use, and iso-8859-1 is commonly used.

The endpoint hl7listener can then be used in a route as a consumer, as this java DSL example illustrates:

    from("hl7socket").to("patientLookupService");

This is a very simple route that will listen for HL7 and route it to a service named patientLookupService that is also a spring bean id we have configured in the spring xml as:

    <bean id="patientLookupService" class="com.mycompany.healtcare.service.PatientLookupService"/>

And another powerful feature of FUSE Mediation Router is that we can have our busines logic in POJO classes that is not at all tied to FUSE Mediation Router as shown here:

public class PatientLookupService {
    public Message lookupPatient(Message input) throws HL7Exception {
        QRD qrd = (QRD)input.get("QRD");
        String patientId = qrd.getWhoSubjectFilter(0).getIDNumber().getValue();

        // find patient data based on the patient id and create a HL7 model object with the response
        Message response = ... create and set response data
        return response
    }

Notice that this class is just using imports from the HAPI library and none from FUSE Mediation Router.

The HL7MLLP codec uses plain String as data format. And FUSE Mediation Router uses Type Converter to convert from/to Strings to the HAPI HL7 model objects. However you can use the plain String objects if you would like to, for instance if you need to parse the data yourself.

See samples for such an example.

The HL7 model is Java objects from the HAPI library. Using this library we can encode and decode from the EDI format (ER7) that is mostly used with HL7. With this model you can code with Java objects instead of the EDI based HL7 format that can be hard for humans to read and understand.

The ER7 sample below is a request to lookup a patient with the patient id 0101701234.

MSH|^~\\&|MYSENDER|MYRECEIVER|MYAPPLICATION||200612211200||QRY^A19|1234|P|2.4
QRD|200612211200|R|I|GetPatient|||1^RD|0101701234|DEM||

Using the HL7 model we can work with the data as a ca.uhn.hl7v2.model.Message.Message object. To retrieve the patient id for the patient in the ER7 above you can do this in java code:

Message msg = exchange.getIn().getBody(Message.class);
QRD qrd = (QRD)msg.get("QRD");
String patientId = qrd.getWhoSubjectFilter(0).getIDNumber().getValue();

FUSE Mediation Router has build in type converters so when this operation is invoked:

Message msg = exchange.getIn().getBody(Message.class);

FUSE Mediation Router will converter the received HL7 data from String to the Message object. This is powerful when combined with the HL7 listener, then you as the end-user don't have to work with byte[], String or any other simple object formats. You can just use the HAPI HL7 model objects.

In the following example we send a HL7 request to a HL7 listener and retrieves a response. We use plain String types in this example:

String line1 = "MSH|^~\\&|MYSENDER|MYRECEIVER|MYAPPLICATION||200612211200||QRY^A19|1234|P|2.4";
String line2 = "QRD|200612211200|R|I|GetPatient|||1^RD|0101701234|DEM||";

StringBuffer in = new StringBuffer();
in.append(line1);
in.append("\n");
in.append(line2);

String out = (String)template.requestBody("mina:tcp://127.0.0.1:8888?sync=true&codec=#hl7codec", in.toString());

In the next sample we want to route HL7 requests from our HL7 listener to our business logic. We have our business logic in a plain POJO that we have registered in the registry as hl7service = for instance using Spring and letting the bean id = hl7service.

Our business logic is a plain POJO only using the HAPI library so we have these operations defined:

public class MyHL7BusinessLogic {

    // This is a plain POJO that has NO imports whatsoever on Apache Camel.
    // its a plain POJO only importing the HAPI library so we can much easier work with the HL7 format.

    public Message handleA19(Message msg) throws Exception {
        // here you can have your business logic for A19 messages
        assertTrue(msg instanceof QRY_A19);
        // just return the same dummy response
        return createADR19Message();
    }

    public Message handleA01(Message msg) throws Exception {
        // here you can have your business logic for A01 messages
        assertTrue(msg instanceof ADT_A01);
        // just return the same dummy response
        return createADT01Message();
    }
}

Then we setup the FUSE Mediation Router routes using the RouteBuilder as:

DataFormat hl7 = new HL7DataFormat();
// we setup or HL7 listener on port 8888 (using the hl7codec) and in sync mode so we can return a response
from("mina:tcp://127.0.0.1:8888?sync=true&codec=#hl7codec")
    // we use the HL7 data format to unmarshal from HL7 stream to the HAPI Message model
    // this ensures that the camel message has been enriched with hl7 specific headers to
    // make the routing much easier (see below)
    .unmarshal(hl7)
    // using choice as the content base router
    .choice()
        // where we choose that A19 queries invoke the handleA19 method on our hl7service bean
        .when(header("CamelHL7TriggerEvent").isEqualTo("A19"))
            .beanRef("hl7service", "handleA19")
            .to("mock:a19")
        // and A01 should invoke the handleA01 method on our hl7service bean
        .when(header("CamelHL7TriggerEvent").isEqualTo("A01")).to("mock:a01")
            .beanRef("hl7service", "handleA01")
            .to("mock:a19")
        // other types should go to mock:unknown
        .otherwise()
            .to("mock:unknown")
    // end choice block
    .end()
    // marhsal response back
    .marshal(hl7);

Notice that we use the HL7 DataFormat to enrich our FUSE Mediation Router Message with the MSH fields preconfigued on the FUSE Mediation Router Message. This let us much more easily define our routes using the fluent builders. If we do not use the HL7 DataFormat then we do not gains these headers and we must resort to a different technique for computing the MSH trigger event (= what kind of HL7 message it is). This is a big advantage of the HL7 DataFormat over the plain HL7 type converters.

In this sample we use plain String objects as the data format, that we send, process and receive. As the sample is part of an unit test there is some code for assertions, but you should be able to understand what happens. First we send the plain String Hello World to the HL7MLLPCodec and receives the response that is also just plain string as we receive Bye World.

MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedBodiesReceived("Bye World");

// send plain hello world as String
Object out = template.requestBody("mina:tcp://127.0.0.1:8888?sync=true&codec=#hl7codec", "Hello World");

assertMockEndpointsSatisfied();

// and the response is also just plain String
assertEquals("Bye World", out);

Here we process the incoming data as plain String and send the response also as plain String:

from("mina:tcp://127.0.0.1:8888?sync=true&codec=#hl7codec")
    .process(new Processor() {
        public void process(Exchange exchange) throws Exception {
            // use plain String as message format
            String body = exchange.getIn().getBody(String.class);
            assertEquals("Hello World", body);

            // return the response as plain string
            exchange.getOut().setBody("Bye World");
        }
    })
    .to("mock:result");