Case file

HL7 v2 to FHIR

A Spring Boot service that takes HL7 v2 admissions, discharges and lab results over MLLP or HTTP and writes them to a FHIR R4 server. Every write is keyed by a business identifier, so a resent message changes nothing. Messages are stored and acknowledged at once, then delivered in order from an outbox that waits out outages and sets rejected content aside for replay. 34 tests plus an integration test against a real HAPI FHIR server.

Author
Henry CartoMSc · Software, Medical & eHealth Engineer
Role
Sole engineer.
Stack
Java 21 · Spring Boot 3.3 · HAPI HL7v2 · HAPI FHIR · FHIR R4 · MLLP · JPA · H2 or PostgreSQL · Testcontainers · GitHub Actions
State
Working gateway with CI against HAPI FHIR.

Problem

Hospitals still run on HL7 v2. Admissions, discharges and lab results travel between systems as pipe-delimited messages over MLLP, and new applications want FHIR. Translating one into the other is the easy part. The hard part is what goes wrong in between, and in production four things do.

Senders resend. An interface engine that times out waiting for an acknowledgement sends the same message again, and that must not create a second patient or a second lab result. The FHIR server goes down, and the sender should still get a fast acknowledgement without anything being lost. Order matters: a discharge applied before its admission leaves the encounter in the wrong state. And some messages are simply wrong, so they need a clear rejection and a place where an engineer can see them and replay them.

The demo script against a running gateway and FHIR server: admit, lab result, resend, rejected message, discharge

The demo script in the repository, run against the gateway and a real HAPI FHIR server. The resent admission is acknowledged as a duplicate, the unsupported message is refused, and the server ends with one patient, one finished encounter and four results.

How it works

MLLP :2575 ----+
               +--> ingest ------> inbound message ------> delivery worker ----> FHIR server
POST /api/hl7 -+    parse, dedupe,  + outbox entry,          oldest first,         one transaction
                    map, ACK        one DB transaction       retry, dead letter    bundle per message

Messages arrive over MLLP or HTTP and are parsed with HAPI HL7v2. The sender and control id (MSH-3, MSH-4 and MSH-10) are checked against what has already been accepted. The message is mapped to one FHIR transaction bundle, and the raw message and the bundle are written to the database in one transaction before the sender gets AA. AA means stored and queued, not written to FHIR, so the sender never waits on the FHIR server. A scheduled worker then delivers the bundles, oldest first.

HL7 v2 FHIR R4
ADT^A01 admit, ADT^A04 register Patient and Encounter in-progress, both upserted
ADT^A03 discharge Patient and Encounter finished, period end set
ADT^A08 update patient Patient only
ORU^R01 lab result Patient, created if missing, and one Observation per OBX

Versions 2.3 to 2.5.1 are read into the v2.5 model. A supported message that is missing required data, such as PID-3 or PV1-19, is answered AE and not recorded, so the sender can fix it and resend with the same control id. An unsupported or unparseable message is answered AR.

Design decisions

Upserts keyed by business identifiers

Every write is keyed by an identifier the hospital already owns, never by a server id. Patient and Encounter use conditional update, PUT Patient?identifier=system|value, on the MRN and the visit number. Observations get the identifier facility, filler order, OBX set id and code, so a corrected result (OBX-11 = C) replaces the original instead of sitting next to it. A message processed twice writes the same resources twice, which changes nothing. Before that, a resend with a control id that was already accepted is acknowledged AA and nothing new is queued.

Conditional create for patients in lab results

The PID segment in a lab result is usually thinner than the one in an admission. If a lab result updated the Patient, it would wipe the address and the other demographics the admission had written. So in lab results the Patient is a conditional create, a POST with ifNoneExist: it is created if the lab result happens to arrive first, and left alone if the patient already exists.

Outbox with ordered delivery

The bundle is not sent at ingest. It is written to an outbox in the same transaction as the inbound message, so either both exist or neither does. The worker sends entries oldest first. On a 5xx, a 429, a timeout or a refused connection it backs off exponentially, from 5 seconds doubling up to 10 minutes, and the queue stops, so a later message can never overtake an earlier one. An outage costs a delay, not data, and a discharge never lands before its admission.

Dead letter with replay

A 4xx means the server rejected the content itself, and retrying it would only block everything behind it. Such an entry goes straight to DEAD with the server's OperationOutcome stored, and the queue moves on. An entry that keeps failing for a retryable reason also goes to DEAD after the configured maximum number of attempts. GET /api/outbox/dead lists dead entries with the server's error, and POST /api/outbox/{id}/retry puts one back in the queue once the cause is fixed.

Tests

34 unit and Spring tests with 0 failures, plus 1 integration test against a real HAPI FHIR 7.4 server in Docker, both run in CI on every push.

They cover timestamp conversion, including daylight saving for timestamps without an offset; ADT and ORU mapping field by field, including the stable identifiers that make replays safe; idempotency, where the same control id twice is one database row and one outbox entry; the ACK codes for accepted, malformed, unsupported and incomplete messages; retry with backoff, head-of-line blocking during an outage, dead letter on rejection, maximum attempts and replay through the API; and MLLP end to end over a real socket.

The integration test sends an admission, a lab result and a discharge to the real server, then sends all three again. The server ends up with exactly one Patient, one finished Encounter and four Observations, and the patient still has the address from the admission.

Limits

These are deliberate scope cuts. The delivery worker assumes a single gateway instance; running several would need row locking or a broker. The queue is strictly first in, first out, so one stuck message for one patient delays everyone, where per-patient queues would fix that at the cost of more complexity. Lab results become Observations only, without a DiagnosticReport per OBR. Patient merges (ADT^A40) are rejected, because merges need the FHIR $merge operation or link handling, which differs between servers. There is no authentication on the HTTP endpoints or the FHIR client. All patients in the samples are fictional.