Your first IPF application
In this tutorial you build a small Spring Boot application that implements the server side of an IHE transaction. You run it, send it a message, and write a test that plays the client side. It takes about 15 minutes.
The transaction is ITI-8 Patient Identity Feed from the IHE PIX profile. A Patient Identity Source, for
example a hospital admission system, informs a Patient Identifier Cross-reference Manager about new or updated
patients. It sends HL7v2 ADT messages over MLLP, a simple TCP framing for HL7v2. The manager replies with an
acknowledgement (ACK). Your application plays the manager.
ITI-8 is a good first transaction because it has few moving parts: one TCP port, one message in, one message out. The concepts are the same for XDS, FHIR and HL7v3 transactions, which differ in transport and message model.
The complete code is in the IPF repository under tutorials/first-app.
Prerequisites
- Java 17 or later
- Maven 3.9 or later
- Python 3, only for the optional step that sends a message by hand
Create the project
Create a Maven project with this pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- Use the Spring Boot version that your IPF release is built with -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.1.3</version>
<relativePath/>
</parent>
<groupId>org.example</groupId>
<artifactId>first-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
<ipf.version>6.1.0</ipf.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Aligns the versions of IPF, Camel, HAPI, CXF, ... -->
<dependency>
<groupId>org.openehealth.ipf</groupId>
<artifactId>ipf-dependencies</artifactId>
<version>${ipf.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.openehealth.ipf.boot</groupId>
<artifactId>ipf-hl7-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
There are two IPF-specific parts:
- The
ipf-dependenciesBOM pins IPF and the libraries it is built on (Camel, HAPI, CXF, …) to matching versions. That is why the dependencies need no version numbers. ipf-hl7-spring-boot-starterbrings in the HL7v2/MLLP transactions and auto-configures Camel, a HAPI context and the MLLP codec. The other starters do the same for XDS, FHIR and HL7v3.
IPF does not depend on Spring Boot. The starter only saves you from setting up this infrastructure yourself. Without
Spring Boot, you would add the ipf-platform-camel-ihe-mllp module instead and register the same objects in your
Camel context. The route itself stays the same.
Write the application
The application class is plain Spring Boot:
package org.openehealth.ipf.tutorials.firstapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FirstApplication {
public static void main(String[] args) {
SpringApplication.run(FirstApplication.class, args);
}
}
The interesting part is the Camel route. Any RouteBuilder that is a Spring bean is picked up automatically:
package org.openehealth.ipf.tutorials.firstapp;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
import static org.openehealth.ipf.platform.camel.hl7.HL7v2.ack;
import static org.openehealth.ipf.platform.camel.hl7.HL7v2.get;
/**
* Server side (Patient Identifier Cross-reference Manager) of the IHE PIX Patient Identity Feed (ITI-8).
*/
@Component
public class PatientFeedRouteBuilder extends RouteBuilder {
@Override
public void configure() {
from("pix-iti8://0.0.0.0:{{first-app.mllp-port}}")
.routeId("patient-feed")
.setHeader("event", get("MSH-9-2"))
.setHeader("patientId", get("PID-3-1"))
.setHeader("patientName", get("PID-5-1"))
.log("Received ${header.event} for patient ${header.patientId} (${header.patientName})")
.transform(ack());
}
}
Step by step:
from("pix-iti8://0.0.0.0:...")creates a consumer endpoint of thepix-iti8component. A consumer endpoint is always the server side of a transaction: it opens the MLLP port, receives each message, parses it into a HAPIMessageobject, and checks that it is a message type ITI-8 allows. Then it passes the message to the rest of the route. Camel replaces the{{first-app.mllp-port}}placeholder with the value fromapplication.yml.get("PID-3-1")reads a field from the message using a HAPI Terser path: segmentPID, field 3 (patient identifier list), component 1 (the ID).log(...)writes a line to the application log.transform(ack())replaces the message body with an acknowledgement to the received message. Whatever is in the body at the end of the route becomes the response, so the endpoint sends thisACKback to the client.
Finally, src/main/resources/application.yml:
spring:
application:
name: first-app
first-app:
mllp-port: 2575 # the registered port for HL7 over MLLP
server:
port: 8080 # pulled in by ipf-spring-boot-starter; not used by this tutorial
Run it
mvn spring-boot:run
Among the startup messages you should see the route listening on port 2575:
Started patient-feed (pix-iti8://0.0.0.0:2575)
Started FirstApplication in 1.3 seconds
Send a message
To see the exchange on the wire, send a message with any HL7 tool, or with this small Python script. It is also
in the repository as send-adt.py:
import socket
# ITI-8 uses HL7 v2.3.1. Segments are separated by carriage returns.
message = "\r".join([
"MSH|^~\\&|SOURCE_APP|SOURCE_FAC|PIX_MGR|PIX_FAC|20261128120000||ADT^A01|MSG0001|P|2.3.1",
"EVN|A01|20261128120000",
"PID|||4711^^^&1.2.3.4&ISO||Doe^John||19700101|M",
"PV1||O",
]) + "\r"
# MLLP framing: <VT> message <FS><CR>
START, END = b"\x0b", b"\x1c\r"
with socket.create_connection(("localhost", 2575), timeout=10) as connection:
connection.sendall(START + message.encode() + END)
response = b""
while not response.endswith(END):
chunk = connection.recv(4096)
if not chunk:
break
response += chunk
print(response.strip(START + END).decode().replace("\r", "\n"))
The script prints the acknowledgement. MSA|AA means application accept, followed by the ID of the message being
acknowledged:
MSH|^~\&|PIX_MGR|PIX_FAC|SOURCE_APP|SOURCE_FAC|20261128140104.101+0100||ACK^A01|2|P|2.3.1
MSA|AA|MSG0001
The application logs:
patient-feed : Received A01 for patient 4711 (Doe)
HAPI numbers the messages it generates, such as the ACK, and keeps a counter in a file named id_file in the
working directory.
Test it
Now write the client side. In IPF, the client is the producer side of the same component: sending a message to
a pix-iti8://host:port endpoint makes your code the Patient Identity Source. This test starts the application
on a separate port and sends it the same message through Camel’s ProducerTemplate:
package org.openehealth.ipf.tutorials.firstapp;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.util.Terser;
import org.apache.camel.ProducerTemplate;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest(properties = "first-app.mllp-port=12575")
class PatientFeedRouteTest {
// ITI-8 uses HL7 v2.3.1. Segments are separated by carriage returns.
private static final String ADT_A01 = String.join("\r",
"MSH|^~\\&|SOURCE_APP|SOURCE_FAC|PIX_MGR|PIX_FAC|20261128120000||ADT^A01|MSG0001|P|2.3.1",
"EVN|A01|20261128120000",
"PID|||4711^^^&1.2.3.4&ISO||Doe^John||19700101|M",
"PV1||O");
@Autowired
ProducerTemplate producerTemplate;
@Value("${first-app.mllp-port}")
int port;
@Test
void acceptsPatientFeed() throws Exception {
// The producer side of pix-iti8 is the Patient Identity Source (the client actor)
Message ack = producerTemplate.requestBody("pix-iti8://localhost:" + port, ADT_A01, Message.class);
Terser terser = new Terser(ack);
assertEquals("AA", terser.get("MSA-1"));
assertEquals("MSG0001", terser.get("MSA-2"));
}
}
Run it with mvn test.
The producer endpoint parses the response into a HAPI Message and checks it, just as the consumer checks the
request. The same pattern works for every IPF component. A Document Consumer sends a QueryRegistry object to an
xds-iti18://host:port/path endpoint and gets back a QueryResponse.
What IPF did for you
Your route has four lines of logic. The pix-iti8 component handled the rest:
- Transport: opening the port and handling MLLP framing (on top of Netty).
- Parsing: turning the text into a HAPI message, using the HL7 version that ITI-8 prescribes (2.3.1).
- Acceptance checks: rejecting messages that are not part of the transaction. Try sending
ORU^R01instead ofADT^A01. Even the client side refuses: Invalid message type ORU, must be one of ADT. - Error responses: if the route throws an exception, the endpoint sends a negative acknowledgement
(
MSA|AEorMSA|ARwith anERRsegment) instead of dropping the connection.
Next steps
Try these extensions to the route. Each needs only a few lines:
- Validate the message against the IHE specification. Add
.process(PixPdqCamelValidators.iti8RequestValidator())as the first step of the route. Invalid messages are then answered withMSA|AEand a description of the problem. The validator uses the IHE conformance profiles and is strict. The sample message above already fails becausePID-5lacks the name type code (Doe^John^^^^^Lpasses). See Message validation. - Enable ATNA auditing. IHE requires audit records for most transactions. Set
ipf.atna.audit-enabled: true, and the endpoint sends an audit event for every message to the syslog server configured withipf.atna.audit-repository-hostand-port. See ATNA auditing and Spring Boot ATNA support. - Use TLS. See Secure MLLP transport.
Then continue with the area you need. The Getting Started page suggests a path for XDS, FHIR, HL7v2 and HL7v3. For ITI-8 itself, see the pix-iti8 component reference.