HL7 Component

The hl7 component is used for working with the HL7 MLLP protocol and HL7 v2 messages 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

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

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 mina2 component where we just use the HL7MLLPCodec as codec.

The HL7 MLLP codec has the following options:


Default Value




The start byte spanning the HL7 payload.



The first end byte spanning the HL7 payload.



The 2nd end byte spanning the HL7 payload.


JVM Default

The encoding (is a charset name) to use for the codec. If not provided, Camel will use the JVM default Charset.



Will convert \n to \r (0x0d, 13 decimal) as HL7 stipulates \r as segment terminators. The HAPI library requires the use of \r.

Exposing a HL7 listener

In our Spring XML file, we configure an endpoint to listen for HL7 requests using TCP:

Notice that we use TCP on 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:

Above we also configure the charset encoding to use (iso-8859-1).

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

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:

Another powerful feature of Camel is that we can have our business logic in POJO classes that is not tied to Camel as shown here:

Notice that this class uses just imports from the HAPI library and not from Camel.

HL7 Model using java.lang.String

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

See samples for such an example.

HL7v2 Model using HAPI

The HL7v2 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 HL7v2.
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 sample below is a request to lookup a patient with the patient ID 0101701234.

Using the HL7 model we can work with the data as a ca.uhn.hl7v2.model.Message object.
To retrieve the patient ID in the message above, you can do this in Java code:

If you know the message type in advance, you can be more type-safe:

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

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 HL7v2 model objects.

HL7 DataFormat

The HL7 component ships with a HL7 data format that can be used to format between String and HL7 model objects.

  • marshal = from Message to byte stream (can be used when returning as response 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:

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:

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

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.

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

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().

Message Headers

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


MSH field




































(Camel 2.14) contains the HapiContext that
was used to parse the message

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


The HL7 Data Format supports the following options:






Whether the HAPI Parser should validate using the default validation rules. Better use the parser or hapiContext option and initialize it with the desired HAPI ValidationContext



Use a custom parser. Must be of type ca.uhn.hl7v2.parser.Parser. Note that GenericParser also allows to parse XML-encoded HL7v2 messages

hapiContextca.uhn.hl7v2.DefaultHapiContextCamel 2.14: Use a custom HAPI context that can define a custom parser, custom ValidationContext etc. This gives you full control over the HL7 parsing and rendering process.


To use HL7 in your Camel routes you'll 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 structure libraries, one for each HL7v2 message version:

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

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

Terser language

HAPI provides a Terser class that provides access to fields using a commonly used terse location specification syntax. The Terser language allows to use this syntax to extract values from messages and to use them as expressions and predicates for filtering, content-based routing etc.


HL7 Validation predicate

Often it is preferable to parse a HL7v2 message and validate it against a HAPI ValidationContext in a separate step afterwards.


HL7 Validation predicate using the HapiContext (Camel 2.14)

The HAPI Context is always configured with a ValidationContext (or a ValidationRuleBuilder), so you can access the validation rules indirectly. Furthermore, when unmarshalling the HL7DataFormat forwards the configured  HAPI context  in the CamelHL7Context header, and the validation rules of this context can be easily reused:


HL7 Acknowledgement expression

A common task in HL7v2 processing is to generate an acknowledgement message as response to an incoming HL7v2 message, e.g. based on a validation result. The ack expression lets us accomplish this very elegantly:

More 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:

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:

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

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

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

See Also

© 2004-2014 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram