Apache Camel - HL7
Apache Camel - HL7
Apache Camel - HL7
HL7 Component
The hl7 component is used for working with the HL7 MLLP protocol and the HL7 model 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 Even more easy-of-use as its integrated w ell w ith the camel-mina component.
Overview
Home Download Getting Started FAQ
Documentation
User Guide Manual Books Tutorials Examples Cookbook Enterprise Integration Patterns Architecture Components Data Format Languages Security
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-hl7</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>
Search
Search
Community
Support Contributing Discussion Forums Mailing Lists User Stories News Articles Site Team Camel Extra
Developers
Developer Guide Source Building JavaDoc IRC Room
Notice we configure it to use camel-mina with TCP on the localhost on port 8888. W e 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:
<bean id="hl7codec" class="org.apache.camel.component.hl7.HL7MLLPCodec"> <property name="charset" value="iso-8859-1"/> </bean>
And here we configure the charset encoding to use, and iso-8859-1 is commonly used. The endpoint hl7listener can then be used in a route as a consumer, as this java DSL example illustrates:
from("hl7socket").to("patientLookupService");
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:
<bean id="patientLookupService" class="com.mycompany.healtcare.service.PatientLookupService"/>
And another powerful feature of Camel is that we can have our busines logic in POJO classes that is not at all tied to Camel as shown here:
public class PatientLookupService { public Message lookupPatient(Message input) throws HL7Exception { QRD qrd = (QRD)input.get("QRD"); String patientId = qrd.getWhoSubjectFilter(0).getIDNumber().getValue(); // find patient data based on the patient id and create a HL7 model object with the response Message response = ... create and set response data return response
Notice that this class is just using imports from the HAPI library and none from Camel.
Using the HL7 model we can work with the data as a ca.uhn.hl7v2.model.Message.Message object. To retrieve the patient ID for the patient in the ER7 above, you can do this in java code:
Message msg = exchange.getIn().getBody(Message.class); QRD qrd = (QRD)msg.get("QRD"); String patientId = qrd.getWhoSubjectFilter(0).getIDNumber().getValue();
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 HL7 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 w hen returning as response using the HL7 MLLP codec) unmarshal = from byte stream to Message (can be used w hen receiving streamed data from the HL7 MLLP
To use the data format, simply instantiate an instance and invoke the marhsal or unmarshl operation in the route builder:
DataFormat hl7 = new HL7DataFormat(); ... from("direct:hl7in").marshal(hl7).to("jms:queue:hl7out");
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:
DataFormat hl7 = new HL7DataFormat(); ... from("jms:queue:hl7out").unmarshal(hl7).to("patientLookupService");
Here we unmarshal the byte stream into a HAPI Message object that is passed to our patient lookup service. 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:
from("direct:hl7in").marshal().hl7().to("jms:queue:hl7out"); from("jms:queue:hl7out").unmarshal().hl7().to("patientLookupService");
Message Headers
The unmarshal operation adds these MSH fields as headers on the Camel message:
Camel 1.x
Key hl7.msh.sendingApplication hl7.msh.sendingFacility MSH field MSH-3 MSH-4 Example MYSERVER MYSERVERAPP MYCLIENT MYCLIENTAPP 20071231235900 null ADT A01 1234 P
hl7.msh.receivingApplication MSH-5 hl7.msh.receivingFacility hl7.msh.timestamp hl7.msh.security hl7.msh.messageType hl7.msh.triggerEvent hl7.msh.messageControl hl7.msh.processingId MSH-6 MSH-7 MSH-8 MSH-9-1 MSH-9-2 MSH-10 MSH-11
hl7.msh.versionId
MSH-12
2.4
Camel 2.0
Key CamelHL7SendingApplication CamelHL7SendingFacility MSH field MSH-3 MSH-4 Example MYSERVER MYSERVERAPP MYCLIENT MYCLIENTAPP 20071231235900 null ADT A01 1234 P 2.4
CamelHL7ReceivingApplication MSH-5 CamelHL7ReceivingFacility CamelHL7Timestamp CamelHL7Security CamelHL7MessageType CamelHL7TriggerEvent CamelHL7MessageControl CamelHL7ProcessingId CamelHL7VersionId MSH-6 MSH-7 MSH-8 MSH-9-1 MSH-9-2 MSH-10 MSH-11 MSH-12
All headers are String types. If a header value is missing, its value is null.
Options
The HL7 Data Format supports the following options:
Option Default Description Camel 2.0: Whether the HAP I Parser should va lidate. va lidate true
Dependencies
To use HL7 in your camel routes you need to add a dependency on camel-hl7, which implements this data format. If you use Maven, you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions).
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-hl7</artifactId> <version>2.2.0</version> </dependency>
Since HAPI 0.6, the library has been split into a base library and several structures libraries, one for each HL7v2 message version:
v2.1 structures library v2.2 structures library v2.3 structures library v2.3.1 structures library v2.4 structures library v2.5 structures library v2.5.1 structures library v2.6 structures library
By default camel-hl7 only references the HAPI base library. Applications are responsible for including structures libraries themselves. For example, if a application works with HL7v2 message versions 2.4 and 2.5 then the following dependencies must be added:
<dependency> <groupId>ca.uhn.hapi</groupId> <artifactId>hapi-structures-v24</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>ca.uhn.hapi</groupId> <artifactId>hapi-structures-v25</artifactId> <version>1.0</version> </dependency>
OSGi
An OSGi bundle containing the base library, all structures libraries and required dependencies (on the bundle classpath) can be downloaded from the HAPI Maven repository as well.
<dependency> <groupId>ca.uhn.hapi</groupId> <artifactId>hapi-osgi-base</artifactId> <version>1.0.1</version> </dependency>
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:
String line1 = "MSH|^~\\&|MYSENDER|MYRECEIVER|MYAPPLICATION||200612211200||QRY^A19|1234|P|2.4"; String line2 = "QRD|200612211200|R|I|GetPatient|||1^RD|0101701234|DEM||"; StringBuilder in = new StringBuilder(); in.append(line1); in.append("\n"); in.append(line2);
In the next sample, we want to route HL7 requests from our HL7 listener to our business logic. W e 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:
public class MyHL7BusinessLogic { // This is a plain POJO that has NO imports whatsoever on Apache Camel. // its a plain POJO only importing the HAPI library so we can much easier work with the HL7 format. public Message handleA19(Message msg) throws Exception { // here you can have your business logic for A19 messages assertTrue(msg instanceof QRY_A19); // just return the same dummy response return createADR19Message(); } public Message handleA01(Message msg) throws Exception { // here you can have your business logic for A01 messages assertTrue(msg instanceof ADT_A01); // just return the same dummy response return createADT01Message(); } }
Notice that we use the HL7 DataFormat to enrich our Camel Message with the MSH fields preconfigued 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.
Here we process the incoming data as plain String and send the response also as plain String:
from("mina:tcp://127.0.0.1:8888?sync=true&codec=#hl7codec") .process(new Processor() { public void process(Exchange exchange) throws Exception { // use plain String as message format String body = exchange.getIn().getBody(String.class); assertEquals("Hello World", body); // return the response as plain string exchange.getOut().setBody("Bye World"); } }) .to("mock:result");
See Also
Configuring Camel Component Endpoint
Getting Started
2004-2011 The Apache Software Foundation. - P rivacy P olicy - (edit page) Graphic Design By Hiram