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.
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:That put, I've tried to process your output but I got this error:
I think I was missing the
\r
character at the end of the line. But I validate the test with this message:The test:
The Result:
Dependencies:
You may access the complete test in this repo.
Cheers!