Search code examples
springapache-camelhl7hapihl7-v2

How to read HL7 file and parse it using Apache Camel, Hapi & Spring (Java config)


I'm trying to read a hl7 file which contains the following message

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

using Apache camel, Hapi & Spring framework (Java config). I want to parse the above message and get segment details from it. I'm using HL7 version 2.3. Following is my RouteBuilder class;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
import example.springcamel.processors.Hl7MessageProcessor;

@Component
public class SimpleRouteBuilder extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("file://E:/projects/hl7/file_to_read/input/")
            .process(new Hl7MessageProcessor())
            .end();
        }
    }

E:/projects/hl7/file_to_read/input/ This is the location where i'm having a file named hl7_message.hl7 with above message.

Following is the Processor class;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import ca.uhn.hl7v2.model.Message;

public class Hl7MessageProcessor implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception {
       Message message = exchange.getIn().getBody(Message.class);
       System.out.println("Original message: " + message);
    }
}

From above code, i'm getting the original message as null. I'm following the documentation given at this link from Apache Camel http://camel.apache.org/hl7.html

Following are the configuration files and main application:

SpringConfiguration.java

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "example.springcamel")
public class SpringConfiguration {

}

RoutesConfiguration.java

import org.apache.camel.spring.javaconfig.CamelConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "example.springcamel.routes")
public class RoutesConfiguration extends CamelConfiguration {

}

MainApplication.java

import org.apache.camel.CamelContext;
import org.apache.camel.spring.SpringCamelContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import example.springcamel.configuration.SpringConfiguration;

public class MainApplication {
    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws Exception {
        AbstractApplicationContext springContext = new 
                AnnotationConfigApplicationContext(SpringConfiguration.class);
        CamelContext camelContext = SpringCamelContext.springCamelContext(springContext, false);
        try {
            camelContext.start();
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            camelContext.stop();
            springContext.close();
        }
    }   
}

I'm completely new to HL7, someone kindly help me on parsing the above HL7 message and getting segment details from it.


Solution

  • I think that you are missing some steps in your route. Try to convert your file message to String first and then unmarshall it to HL7:

    from("file:src/test/resources/hl7?noop=true")
        .convertBodyTo(String.class)
        .unmarshal()
        .hl7(false)
        .log("The Message body is: ${body}")
        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                final Message message = exchange.getIn().getBody(Message.class);
                System.out.println("Original message: " + message);
            }
        })
        .to("mock:result");
    

    That put, I've tried to process your output but I got this error:

    ca.uhn.hl7v2.HL7Exception: The HL7 version 2.3 QRD is not recognized
    

    I think I was missing the \r character at the end of the line. But I validate the test with this message:

    MSH|^~\&|HL7ABLAB|HNA500|HNAM|HNAM|20090911132151||ADT^A01|
    Q30235031T29347435X328970|A|2.3|123
    EVN|A01|20090911132100|||^DRONE_PM1^DRONE_PM^^^^^^^Personnel
    PID|1||1357920591||IntFace1101A^WinTask^^^^^Current||19801117|M||||||||||
    10000476524^^^FIN^FIN NBR|100000451||||||0
    PV1|1|Inpatient|CD:16067689^CD:16067691^CD:16067741^Uniontown Hospit^^Bed(s)
    ^Uniontown Hospit||||||||||||||501455^Orr^Maggi^^^^^^External ID^Personnel^^^
    External
    Identifier~25584^Orr^Maggi^^^^^^PERSONNEL PRIMARY
    IDENTIFIER^Personnel^^^Personnel Primary Identifier|Inpatient|||||||||||||||||||
    ||
    Uniontown Hospit||Active|||20090911132100
    

    The test:

    @Test
    public void test() throws InterruptedException {
        MockEndpoint mock = getMockEndpoint("mock:result");
        mock.expectedMessageCount(1);
        mock.expectedBodyReceived().body(Message.class);
    
        assertMockEndpointsSatisfied();
    }
    

    The Result:

    Original message: MSH|^~\&|HL7ABLAB|HNA500|HNAM|HNAM|20090911132151||ADT^A01|Q30235031T29347435X328970|A|2.3|123
    EVN|A01|20090911132100|||^DRONE_PM1^DRONE_PM^^^^^^^Personnel
    PID|1||1357920591||IntFace1101A^WinTask^^^^^Current||19801117|M||||||||||
    10000476524^^^FIN^FIN NBR|100000451||||||0
    PV1|1|Inpatient|CD:16067689^CD:16067691^CD:16067741^Uniontown Hospit^^Bed(s)
    ^Uniontown Hospit||||||||||||||501455^Orr^Maggi^^^^^^External ID^Personnel^^^
    External
    Identifier~25584^Orr^Maggi^^^^^^PERSONNEL PRIMARY
    IDENTIFIER^Personnel^^^Personnel Primary Identifier|Inpatient|||||||||||||||||||
    ||
    Uniontown Hospit||Active|||20090911132100
    

    Dependencies:

    <dependency>
        <groupId>ca.uhn.hapi</groupId> 
        <artifactId>hapi-structures-v23</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-hl7</artifactId>
    </dependency>
    

    You may access the complete test in this repo.

    Cheers!