This tutorial guides you through an HL7 version 2 message processing example.

Subject of this tutorial

We will create an IPF application that reads an HL7 v2 message from a file endpoint, validates and transforms the message, and writes the transformation result to an output file. Message processing is done via IPF’s HL7 DSL and via HL7-specific extensions to the Camel DSL.


The message is an ADT A01 message, version 2.2, and we want to enforce that

  • all primitive type values have the correct format and
  • the message itself contains a defined sequence of segments


Now lets look the transformation we want to perform if we passed validation.

  • The room number and bed number from the PV1[3] field shall be dropped. This field is a composite field and we want to drop the second and the third component.
  • The birth date in PID[7] shall be reformatted by dropping the last 6 digits. This field is also a composite field with only a single component.
  • The gender code in PID[8] shall be mapped to a code from another code system. We will use a simple code mapping service for that.

Finally, we will derive the destination file name from the MSH[4] field (sending facility).

HL7 Message

Route Design

Here’s the order in which the message will be processed:

  • An HL7 message is received via a file endpoint.
  • The message is validated as described above.
  • The message is forwarded to a transformer that makes the changes to the HL7 message as described above.
  • The transformation result is placed into different files whose names are derived from the MSH[4] field.

Create the Project

Create an IPF example project using the basic IPF Maven archetype:

mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate

This will create a folder named hl7 in the current directory. Change to this directory and build the project:

mvn clean install

This will build the project and install the project artifacts into your local Maven cache. You can also import the project into your favorite IDE.

Extend the project descriptor

In order to enable HL7 processing we have to include further dependencies into the Maven project descriptor pom.xml.


Extend the application context

The components included in the previous section must also be configured in the Spring application context.xml file. This file is located under src/main/resources. In addition to the default configuration created by the archetype we need to

  • configure a mapping service that we will use for code mappings
  • add a ValidationRuleBuilder bean for HL7 validation
    <beans xmlns=""

        <camel:camelContext id="camelContext">
            <camel:jmxAgent id="agent" disabled="true" />
            <camel:routeBuilder ref="routeBuilder"/>

        <bean id="routeBuilder" class="org.openehealth.ipf.tutorials.hl7.route.SampleRouteBuilder"/>

        <!-- Registers this Spring context as the one to look up the mapping service -->
        <ipf:globalContext id="globalContext"/>

        <bean id="mappingService"
            <property name="mappingResource" value=""/>

        <bean id="myCustomRules" class="org.openehealth.ipf.tutorials.hl7.validation.SampleRulesBuilder"/>


Route Definition

Let’s start with an overview of the complete route. Open the SampleRouteBuilder.groovy file and replace the existing routes in the configure() method:

    package org.openehealth.ipf.tutorials.hl7.route

    import ca.uhn.hl7v2.DefaultHapiContext
    import ca.uhn.hl7v2.HapiContext
    import org.apache.camel.Exchange
    import org.apache.camel.component.hl7.HL7DataFormat
    import org.apache.camel.spring.SpringRouteBuilder
    import org.openehealth.ipf.tutorials.hl7.validation.SampleRulesBuilder

    import static org.apache.camel.component.hl7.HL7.messageConforms

    public class SampleRouteBuilder extends SpringRouteBuilder {

        void configure() {

            // Set up HL7 context with the custom validation rules and disable validation during parsing
            HapiContext context = new DefaultHapiContext(lookup(SampleRulesBuilder))

            // Set up Camel HL7 Data format using the HapiContext defined above
            HL7DataFormat hl7 = new HL7DataFormat()

            // Read file from directory and forward to direct endpoint

            // Main processing: parse message, validate, transform
                    // validate that the message conforms to the rules defined in the HapiContext
                    .transmogrify { msg ->
                        msg.PV1[3][2] = '' // clear room nr.
                        msg.PV1[3][3] = '' // clear bed nr.
                        msg.PID[7][1] = msg.PID[7][1].value.substring(0, 8) // format birth date
                        msg.PID[8] = msg.PID[8].mapGender()                 // map gender

                    // Set header so Camel knows the file to write into
                    .setHeader(Exchange.FILE_NAME) { exchange ->
              [4].value + '.hl7'

                    // Convert message to string and write to file

In the route we refer to a SampleRulesBuilder validation class that defines the allowed sequence and cardinalities of HL7 groups and segments for this message. The file must be added to the src/main/groovy directory in the subdirectory org/openehealth/tutorial:

package org.openehealth.ipf.tutorials.hl7.validation


class SampleRulesBuilder extends NoValidationBuilder{

    // We define only a subset of the segments defined in the HL7 2.2 spec
    protected void configure() {

            .message('ADT', 'A01').abstractSyntax(
                [  {  'NK1'  }  ],
                [  {  INSURANCE(
                        [  'IN2'  ] ,
                        [  'IN3'  ]

The sequence and cardinality of groups and segments is defined in a syntax that is very closely related to the HL7 Abstract Message Syntax. The message is required to contain a MSH, EVN, PID and PV1 segment; it may contain any number of NK1 segments and it may contain a repeatable INSURANCE group. For detail, please read the documentation on HL7 validation.

As our test message matches this definition, we expect the message to pass validation.

The transformation is implemented using a Transmogrifier closure. Inside the closure we use the HL7 DSL in order to manipulate the message context.

The HL7 DSL allows us to do this without dealing with low level HL7 API details. You immediately see how the message is processed by looking at the code.

    .transmogrify { msg ->
        msg.PV1[3][2] = '' // clear room nr.
        msg.PV1[3][3] = '' // clear bed nr.
        msg.PID[7][1] = msg.PID[7][1].value.substring(0, 8) // format birth date
        msg.PID[8] = msg.PID[8].mapGender()                 // map gender

For accessing a message segment, directly use the segment name like msg.PV1 or msg.PID. Fields and sub-fields (components of a composite field) are accessed by indices starting from 1. For example, PV1[3][2] denotes the second component of the third PV1 field. For a complete reference of the HL7 DSL refer to the HL7 DSL documentation.

Code Mapping

Code mapping is done with the mapGender() method. This method translates the English ‘F’ code (female) contained in PID[8] to a German ‘W’ code (weiblich).

The application context configures the code mapping service to use a mappingScript named In our example, we only need a single gender mapping table an entry that maps ‘F’ to ‘W’ and one entry that leaves all other codes unchanged.

    mappings = {
        F      : 'W',
        (ELSE) : { it }

This format is valid Groovy syntax and is understood by the default bi-directional mapping service provided by IPF. For a complete reference refer to the mapping service section of documentation.

Automated Route Testing

For automated testing we will use the following HL7 message:

PID|1||79471||Meier^Elfriede|Meier|19400101000000|F|||Hauptstrasse 23^^Essen^NW^11000^DE^H|||||S|||111-11-1111||||Essen
NK1|1|Meier^Elfriede|EMC|Hauptstrasse 23^^Essen^NW^11000^DE|333-4444~333-5555|

After processing we expect the following output:

PID|1||79471||Meier^Elfriede|Meier|19400101|W|||Hauptstrasse 23^^Essen^NW^11000^DE^H|||||S|||111-11-1111||||Essen
NK1|1|Meier^Elfriede|EMC|Hauptstrasse 23^^Essen^NW^11000^DE|333-4444~333-5555|

Create two files, msg-01.hl7 and msg-01.hl7.expected, with the above content under the src/test/resources folder. These files will be used inside our JUnit test. To implement the test open the file and replace its content with the following:

    package org.openehealth.ipf.tutorials.hl7.route;

    import java.util.Scanner;

    import ca.uhn.hl7v2.HL7Exception;
    import ca.uhn.hl7v2.model.Message;
    import ca.uhn.hl7v2.parser.PipeParser;
    import org.apache.camel.ProducerTemplate;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.TestExecutionListeners;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

    import static org.junit.Assert.assertEquals;

    @ContextConfiguration(locations = { "/context.xml" })
    public class SampleRouteTest {

        private ProducerTemplate producerTemplate;

        public void testRoute() throws Exception {
            producerTemplate.sendBody("direct:input", getClass().getResourceAsStream("/msg-01.hl7"));
            Resource result = new FileSystemResource("target/output/HZL.hl7");

        // Helper method to load and parse a HL7v2 message from a file
        protected static <T extends Message> T load(InputStream is) throws HL7Exception {
            return (T)new PipeParser().parse(
                    new Scanner(is).useDelimiter("\\A").next());

The testRoute method opens an InputStream on the input file and sends that stream in the in-message body to the direct:input endpoint. The processing result is loaded from the created HZL.hl7 file and compared with the expected processing result.

Execute the test in your IDE or run mvn test one the command line.

The test should successfully execute and you should also see an HZL.hl7 file in the target/output directory containing the processing result.

Assembly and Installation

We finally want to create a distribution package from our project, install (unzip) that package somewhere and start a standalone integration server (i.e. an integration server that runs outside your IDE). To create the package enter on the command line:

mvn assembly:assembly

The created package is written to the project’s target folder. Copy the package to a new location and unzip it. This will create a folder named hl7-1.0-SNAPSHOT with the following content:


  • The lib folder contains the project jar file (hl7-1.0-SNAPSHOT.jar) as well as all required runtime dependencies.
  • The conf folder contains a log4j.xml configuration file.
  • Startup scripts are located directly under the root folder.

Starting the server

The way how the project is packaged can of course be customized by changing the project’s src/main/assembly/bin.xml assembly descriptor. The script is currently empty i.e. for testing-purposes you have to run the server on Windows using startup.bat:


Finally, copy the test HL7v2 message msg-01.hl7 into the target/input folder and wait a few seconds. The file is consumed and the transformed result is written into the target/output folder.

Enter Ctrl-C in the console to stop the server.


In this tutorial, we:

  • created a Maven-based IPF project
  • added IPF dependencies relevant for HL7 processing
  • created a Camel route that reads, parses, validates, transforms, and renders a HL7 message
  • executed a unit test that verified the process
  • assembled a distribution containing a server executing the code
  • tested the running server with an example HL7 message

The source code for this tutorial is located in the ipf-tutorials-hl7 module.

Back to top

Version: 3.5-SNAPSHOT. Last Published: 2018-04-23.

Reflow Maven skin by Andrius Velykis.