Blog

    Mirth Connect Kafka Producer and Consumer Classloader Solution: Isolate the Client from the Engine

    By GauravAugust 12, 20269 mins read
    On this page

    A Mirth Connect Kafka producer or consumer often fails because the client's libraries and the engine's libraries load in the same classloader. The classloader solution is isolation: the client runs in its own loader, and the channel hands it a payload.

    Hospital CTOs and digital health founders who run Mirth Connect get the error signs, the loader design, the types allowed across the boundary and a repeatable proof that messages still leave.

    What is the Mirth Connect Kafka classloader solution?

    The Mirth Connect Kafka classloader solution keeps the Kafka client's jars out of the engine's classloader. The client runs in an isolated loader parented on the Java platform, and the channel passes it a byte payload, so engine libraries and Kafka libraries never resolve each other's classes.

    Kafka from a Mirth channel throwing linkage errors?

    We isolate the client classloader and hand it a payload. The channel does not share the engine classpath with Kafka.

    Agnotic Technologies builds this isolation for Mirth Connect estates that publish to or read from Kafka. Nothing about the channel's transforms changes; only the loader that resolves Kafka classes does.

    NextGen publishes Connect, including a Docker image, as the interface engine formerly known as Mirth Connect.

    Why does a Kafka client fail inside a Mirth channel?

    A Kafka client inside a Mirth channel fails when the client's jars and the engine's jars load as one set of classes. Whichever copy of a shared library loads first wins, and the other side then calls methods or casts types that do not match.

    • Clash: two copies of a logging or JSON library, or a Kafka client compiled against a different Java API than the engine.
    • Isolation: a child classloader that sees the Kafka jars, not the engine's overlapping jars.
    • Payload: the channel serialises the message, then the client sends or reads bytes. It does not import engine types.
    • Proof: one test message leaves, and the engine log still starts. A channel that will not start is not isolated.

    Decide first which hop belongs in the engine and which belongs in Kafka. Isolation does not change that design; it only lets the hop you chose run without breaking the engine.

    What are the common mistakes with Kafka in Mirth Connect?

    The most common mistake is dropping Kafka jars onto the engine's shared library path. Agnotic treats that path as engine-only, and the other mistakes below usually follow from the same shortcut.

    • Shared path: Kafka jars copied beside the engine's libraries, so one restart later the engine will not start.
    • Single-jar fixes: replacing one conflicting jar by hand instead of rebuilding the client folder from its dependency list.
    • Producer per message: a new client for every message, which is slow and leaves connections open.
    • Shared logger: the client writing PHI into the engine log through a logger both sides can see.
    • No restart test: declaring success after one message, without a cold start and a redeploy.

    Which errors point to a classloader clash?

    A classloader clash shows up as a linkage error, a missing class or method that is plainly on disk, or a cast between two classes with the same name. Agnotic reads the full stack trace for the loader names before touching any jar.

    • NoClassDefFoundError or ClassNotFoundException: the class exists, but not in the loader that asked for it.
    • NoSuchMethodError: an older copy of a library won the race, and the client expected a newer method.
    • ClassCastException naming the same class twice: two loaders each defined their own copy.
    • LinkageError at deploy: a constraint failed because two loaders disagree on one type.

    The clue is the loader, not the class. If the trace shows the engine's loader resolving a class the Kafka client needs, the jar placement is wrong, whatever the dependency list says.

    Why does a plain child loader not fix the clash?

    A plain child loader does not fix the clash because it asks its parent first, so the engine's copy of a shared library still wins. Isolation needs a loader whose parent cannot see the engine's overlapping jars.

    Java class loaders use a delegation model. Each class loader has a parent, and by default it asks the parent for a class or resource before trying to find it itself.

    • Parent-first child under the engine: the engine's logging or JSON library loads first. Same clash, new location.
    • Child-first loader: the Kafka jars load first, but every exception to that rule must be listed and tested.
    • Loader parented on the Java platform: only JDK classes are shared, so the engine's libraries are invisible to the client.

    Agnotic prefers the third option because it has no exception list to maintain. The client sees the JDK and its own jars, nothing else.

    What should the isolated loader's parent be?

    The isolated loader's parent should be the Java platform loader, so the only classes the engine and the client share are JDK classes. The engine's own loader stays out of the chain entirely.

    Classloader isolation for Kafka in Mirth Connect: the channel in the engine loader serialises the message to bytes, a bridge hands the bytes to an isolated loader that holds the Kafka client and its own dependencies, and the client sends to a Kafka topic, with the isolated loader parented on the Java platform loader rather than the engine loader
    • Engine side: the channel, its scripts and the engine's own libraries.
    • Bridge: a small class that uses only JDK types and calls into the isolated loader.
    • Isolated side: the Kafka client, its dependencies and one producer or consumer per deployment.

    The bridge is the only code that knows both sides exist. Keep it short enough to review in one sitting, because every type it touches becomes part of the boundary.

    Which types may cross the boundary?

    Only JDK types may cross the boundary: byte arrays, strings, longs and maps of strings. An engine type or a Kafka type passed across will be a different class on the other side, even when the name matches.

    • Into the client: topic name, key as a string, payload as bytes, headers as a map of strings.
    • Out of the client: partition, offset and an error message as a string.
    • Never across: engine message objects, Kafka record objects or a logger instance.

    If a developer needs a richer type, the answer is to serialise it, not to widen the boundary. A wider boundary brings the clash back one class at a time.

    How should producer and consumer lifetimes work in a channel?

    A producer should be created once when the channel deploys, shared by the channel's threads, and closed when the channel undeploys. Creating a producer per message is slow and leaves connections behind.

    Apache Kafka documents that KafkaProducer is thread safe, and that sharing a single producer instance across threads will generally be faster than having multiple instances.

    • Producer: one instance per deployed channel, held by the isolated side, closed on undeploy.
    • Consumer: one polling thread owned by the isolated side. It is not shared across channels.
    • Redeploy: close the client, drop every reference to the old loader, then build a fresh one.

    A thread left running after undeploy keeps the old loader alive, and its classes stay in memory. Repeated redeploys then grow memory until the engine restarts.

    A worked example: a lab result channel that publishes to a topic

    A worked example: a lab result channel already transforms HL7 v2 results for the EHR, and a second destination must publish each result to a Kafka topic. The first attempt drops the Kafka jars beside the engine's libraries, and the engine fails to start after restart.

    Agnotic removes those jars and builds a separate folder holding the Kafka client and its dependencies. The bridge creates a loader over that folder with the platform loader as parent, and the destination passes the serialised result as bytes.

    The engine starts cleanly, the destination sends one test result, and the bridge returns the partition and offset. Those two values go into the channel's message metadata as proof of delivery.

    How do you prove one message still leaves?

    Prove it with a single test message that reaches the topic and a restart that still succeeds afterwards. A message that leaves once while the engine fails on its next start is not a working fix.

    • Step 1, restart: start the engine from cold with the bridge deployed. No linkage error in the startup log.
    • Step 2, send: push one non-PHI test message through the destination.
    • Step 3, read: confirm the record on the topic with a separate consumer, by partition and offset.
    • Step 4, redeploy: undeploy and deploy the channel twice, then send again.

    Keep the startup log and the partition and offset from the proof with the channel's change record. The next person to touch the isolated folder can then see what a passing run looked like.

    What should be logged at the boundary?

    The boundary should log what was sent and where it landed, never what the message said. Agnotic keeps the client's own logging inside the isolated loader and writes only delivery facts to the engine side.

    A delivery log without message content stays out of the PHI inventory, so the HIPAA Security Rule safeguards already applied to the engine do not have to stretch to a second log store.

    • Log: channel name, engine message id, topic, partition, offset and the result.
    • Log on failure: the error class and message from the client, as a string.
    • Never log: payload bytes, keys built from patient identifiers or full record headers.

    How do engine or client upgrades affect the isolation?

    An engine upgrade or a Kafka client upgrade can change which libraries overlap, so either one re-runs the full proof. Agnotic pins the client version in the isolated folder and records it with the channel.

    • Engine upgrade: repeat the cold start and the test message before production traffic resumes.
    • Client upgrade: rebuild the isolated folder from its dependency list, never by copying single jars.
    • Java upgrade: check the platform loader still exposes only the JDK classes the bridge uses.

    Keep a list of every jar in the isolated folder, with its version, next to the channel export. Without that list, a hurried fix tends to put an old library copy back on the engine path.

    What does classloader isolation mean for your build plan?

    For the build plan, classloader isolation adds one owned component: the bridge and its isolated folder, held by the integration team, while the platform team keeps the engine's own library path. Keeping those two owners separate stops a quick fix from landing in the wrong folder.

    • Integration team: bridge code, isolated folder, client version and the proof steps.
    • Platform team: engine version, shared library path and restart windows.
    • Kafka owner: topic, credentials and the consumer that confirms delivery.

    How can Agnotic help with Mirth Connect and Kafka?

    Agnotic builds the isolated loader, the bridge and the payload hand-off for the client's Mirth Connect channels, then runs the cold-start and redeploy proof with the client's integration team before production traffic moves.

    Keep Kafka out of the engine loader

    A child classloader, a byte payload, and a test message that still leaves.

    Sources

    Frequently Asked Questions

    Any team whose Mirth Connect channels produce to or consume from Kafka inside the engine. It matters most when the engine already runs other plugins or custom libraries that overlap with the Kafka client.

    Talk to an engineer about your project

    Show us the channel that talks to Kafka. We will isolate the client classloader and prove one message still leaves.