How to create a Camel HL7 Listener

This is an example of an HL7 listener. This application listens for Hl7 messages in a specific IP and port and returns an HL7 ACK message.

I am going to explain step by step how to develop an HL7 listener using camel and HAPI. These are the steps that I have followed:

  1. Define a pom.xml
  2. Create an Application Context file
  3. Create a Route Builder
  4. Create a Bean to process the message
  5. Run the application
  6. Optional: Create a Unit Test and check that everything works fine

  • Define a pom.xml

This project has been developed using Maven, so first of all your need to create you pom.xml with the camel and HAPI dependencies. Also you will need to add the Camel Maven plugin. The Camel Maven Plugin allows you to run your Enterprise Integration Patterns using Spring for Dependency Injection.

By default, the Camel Maven Plugin gets from META-INF/spring/*.xml target any application config file. If you would like to set a different location you will need to add the applicationContextUri tag:

<plugin>

<groupId>org.apache.camel</groupId>

<artifactId>camel-maven-plugin</artifactId>

<configuration>

<applicationContextUri>NEW LOCATION</applicationContextUri>

</configuration>

</plugin>

  • Create an Application Context file

In the Applicatoin Contex file, I have defined:

– The auto-detecting components property. This way Spring will register all the beans in the base-package: hl7Integration.camel. Including the Route Builder where I have set my camel route.

–  Properties file which includes the listener IP and port. This properties file is optional but it is handy if you don’t want to have thins information hard coded.

– The Camel Context. The Camel context includes the listener endpoint (hl7listener) and also the contextScan tag which will look for Route Builder instances . Note that I have added two properties: sync= true because every time that we receive a message we send back and ACK and the other property is the HL7 codec.

    <!-- enable Spring @Component scan -->
    <context:component-scan base-package="hl7Integration.camel"/>

    <bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
        <property name="location" value="classpath:hl7integration/configuration/endpoint.properties"/>
    </bean>

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

    <camelContext xmlns="http://camel.apache.org/schema/spring"  id="camelContext">

        <contextScan/>
        <!-- You need to configure your socket in the endpoint.properties file  -->
        <camel:endpoint id="hl7listener"
                        uri="mina:tcp://{{endpoint.server}}:{{endpoint.port}}?sync=true&codec=#hl7codec" />

    </camelContext>
  • Create a Route Builder

Create a route in the Route Builder file with the same name that you have previously defined in the Application Context. In this case, all the messages received in “hl7listener” will be send to the ACKResponse bean.

@Component
public class InboundRouteBuilder extends SpringRouteBuilder
{
    private static final Logger log = LoggerFactory.getLogger(InboundRouteBuilder.class);

    @Override
    public void configure() throws Exception {

        from("hl7listener")
          .routeId("route_hl7listener")
            .startupOrder(997)
              .unmarshal()
               .hl7(false)
                .to("bean:respondACK?method=process")
                .end();
    }
}
  • Create a Bean to process the message

Implement a bean where we are going to process the message.

@Component
public class RespondACK {

    public Message process(Message in) throws Exception {
        System.out.println(in.toString());
        Message out =  in.generateACK();
        System.out.println(out.toString());
        return out;
    }
}
  • Run the application

To run this application you only need to try: mvn camel:run

  • Create a unit Test (optional)

If you would like to test that everything works fine, create a unit test like this:

public class TestListener extends CamelTestSupport {


    /**
     * This method sends an HL7 message to the Listener and
     * receives and ACK confirmation message
     * @throws Exception
     */
    @Test
    public void testHl7Codec() throws Exception {

        String inMessage = "MSH|^~\\&|hl7Integration|hl7Integration|||||ADT^A01|||2.5|\r" +
                "EVN|A01|20130617154644\r" +
                "PID|1|465 306 5961||407623|Wood^Patrick^^^MR||19700101|1|||High Street^^Oxford^^Ox1 4DP~George St^^Oxford^^Ox1 5AP|||||||";

        String out = (String) template.requestBody("mina:tcp://localhost:4444?sync=true&codec=#hl7codec", inMessage);

        System.out.println(out);

    }

    protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry jndi = super.createRegistry();
        jndi.bind("hl7codec", new HL7MLLPCodec());
        return jndi;
    }
}
  • Get the code

You can find all the sources code in github.

3 thoughts on “How to create a Camel HL7 Listener

  1. Just interested in your thoughts regarding multiple message types being received and processed by a single channel. To explain I have two applications; one processes ADT the other ORU. It occurred to me that these could be combined into a single application that listens on a single port. Is this reasonable or is there an advantage keeping the processing separate and hence running in (in my case) different threads? In fact I am currently loading the messages into a FIFO buffer to manage any bursting, so I have two FIFO buffers; one for ORU and one for ADT

    • If I were you I would use one single application. If you have more than one application that will be more difficult maintain. In this case, I would use Apache Camel + ActimeMQ. Using Camel you could have different business logic for different messages, I have written a couple of post about camel, but I strongly recommend you read “Camel in Action” it is a very good book, and very easy to follow. On the other hand, you could use ActiveMQ to manage your queues, it is very useful to manage any bursting and provides reliable messaging.
      I hope it helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>