You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 23 Next »

HL7 Component

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 it's integrated well with the camel-mina component.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-hl7</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

HL7 MLLP protocol

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:

Unknown macro: {div}

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, Camel 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 the use of \r.

validate

true

Whether HAPI Parser should validate or not.

Exposing a HL7 listener

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 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 set up 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 Camel is that we can have our busines logic in POJO classes that is not at all tied to Camel 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 Camel.

HL7 Model using java.lang.String

The HL7MLLP codec uses plain String as data format. And Camel uses Type Converter to convert from/to strings to the HAPI HL7 model objects. However, you can use the plain String objects if you prefer, for instance if you need to parse the data yourself.

See samples for such an example.

HL7 Model using HAPI

The HL7 model uses 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();

Camel has built-in type converters, so when this operation is invoked:

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

Camel will convert the received HL7 data from String to Message. 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.

HL7 DataFormat

The HL7 component ships with a HL7 data format that can be used to marshal or unmarshal HL7 model objects.

  • marshal = from Message to byte stream (can be used when responding using the HL7 MLLP codec)
  • unmarshal = from byte stream to Message (can be used when receiving streamed data from the HL7 MLLP

To use the data format, simply instantiate an instance and invoke the marshal or unmarshal operation in the route builder:

  DataFormat hl7 = new HL7DataFormat();
  ...
  from("direct:hl7in").marshal(hl7).to("jms:queue:hl7out");

In the sample above, the HL7 is marshalled from a HAPI Message object to a byte stream and put on a JMS queue.
The next example is the opposite:

  DataFormat hl7 = new HL7DataFormat();
  ...
  from("jms:queue:hl7out").unmarshal(hl7).to("patientLookupService");

Here we unmarshal the byte stream into a HAPI Message object that is passed to our patient lookup service.

Serializable messages

As of HAPI 2.0 (used by Camel 2.11), the HL7v2 model classes are fully serializable. So you can put HL7v2 messages directly into a JMS queue (i.e. without calling marshal() and read them again directly from the queue (i.e. without calling unmarshal().

Segment separators

As of Camel 2.11, unmarshal does not automatically fix segment separators anymore by converting \n to \r. If you
need this conversion, org.apache.camel.component.hl7.HL7#convertLFToCR provides a handy Expression for this purpose.

Charset

As of Camel 2.14.1, both marshal and unmarshal evaluate the charset provided in the field MSH-18. If this field is empty, by default the charset contained in the corresponding Camel charset property/header is assumed. You can even change this default behavior by overriding the guessCharsetName method when inheriting from the HL7DataFormat class.

 

There is a shorthand syntax in Camel for well-known data formats that are commonly used.
Then you don't need to create an instance of the HL7DataFormat object:

  from("direct:hl7in").marshal().hl7().to("jms:queue:hl7out");
  from("jms:queue:hl7out").unmarshal().hl7().to("patientLookupService");

 

 

Message Headers

The unmarshal operation adds these MSH fields as headers on the Camel message:

Unknown macro: {div}

Key

MSH field

Example

CamelHL7SendingApplication

MSH-3

MYSERVER

CamelHL7SendingFacility

MSH-4

MYSERVERAPP

CamelHL7ReceivingApplication

MSH-5

MYCLIENT

CamelHL7ReceivingFacility

MSH-6

MYCLIENTAPP

CamelHL7Timestamp

MSH-7

20071231235900

CamelHL7Security

MSH-8

null

CamelHL7MessageType

MSH-9-1

ADT

CamelHL7TriggerEvent

MSH-9-2

A01

CamelHL7MessageControl

MSH-10

1234

CamelHL7ProcessingId

MSH-11

P

CamelHL7VersionId

MSH-12

2.4

All headers are String types. If a header value is missing, its value is null.

Options

The HL7 Data Format supports the following options:

Unknown macro: {div}

Option

Default

Description

validate

true

Whether the HAPI Parser should validate.

Dependencies

To use HL7 in your camel routes you need to add a dependency on camel-hl7 listed above, which implements this data format.

The HAPI library since Version 0.6 has been split into a base library and several structures libraries, one for each HL7v2 message version:

By default camel-hl7 only references the HAPI base library. Applications are responsible for including structures libraries themselves. For example, if a application works with HL7v2 message versions 2.4 and 2.5 then the following dependencies must be added:

<dependency>
    <groupId>ca.uhn.hapi</groupId>
    <artifactId>hapi-structures-v24</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>ca.uhn.hapi</groupId>
    <artifactId>hapi-structures-v25</artifactId>
    <version>1.0</version>
</dependency>

Alternatively, an OSGi bundle containing the base library, all structures libraries and required dependencies (on the bundle classpath) can be downloaded from the HAPI Maven repository.

<dependency>
    <groupId>ca.uhn.hapi</groupId>
    <artifactId>hapi-osgi-base</artifactId>
    <version>1.0.1</version>
</dependency>

Samples

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:

Error formatting macro: snippet: java.lang.NullPointerException

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:

Error formatting macro: snippet: java.lang.NullPointerException

Then we set up the Camel routes using the RouteBuilder as follows:

Error formatting macro: snippet: java.lang.NullPointerException

Notice that we use the HL7 DataFormat to enrich our Camel Message with the MSH fields preconfigued on the Camel Message. This lets 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.

Sample using plain String objects

In this sample we use plain String objects as the data format, that we send, process and receive. As the sample is part of a 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 receive the response as a plain string, Bye World.

Error formatting macro: snippet: java.lang.NullPointerException

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

Error formatting macro: snippet: java.lang.NullPointerException

See Also

  • No labels