HAPI HL7v2

repository·master·Indexed 43 days ago

https://github.com/hapifhir/hapi-hl7v2

A Java-based library for working with HL7v2 messages, providing tools for parsing, encoding, and managing healthcare interoperability data. It includes support for MLLP (Minimum Lower Layer Protocol) via MllpEncoder and MllpDecoder, connection management through ConnectionFactory and ConnectionData, and message exchange handling using the Responder class.

Tokens
3.2K
Snippets
11
Records
19
Agent score
55%

What's inside HAPI HL7v2

  1. Overview of the HAPI HL7 distribution

    master

    HAPI is an open-source HL7 library distribution. The distribution includes the core HAPI libraries, source code, and example scripts for running sample code.

    Key components of the distribution:

    • lib/: Contains the HAPI libraries and dependencies required to run sample code.
    • sources/: Contains JAR files containing the HAPI source code.
    • examples/: Contains scripts to run HAPI examples.
  2. Understand the HAPI JDK 1.4 Distribution Contents

    master

    The JDK 1.4 distribution package is organized into the following directories:

    • lib/: Contains the HAPI libraries and the additional libraries required to run the provided sample code.
    • sources/: Contains JAR files containing the HAPI source code.
    • examples/: Contains scripts used to execute the HAPI examples.
  3. Important: Check JDK Compatibility for this Distribution

    master

    This specific distribution of HAPI is compiled for JDK 1.4.

    If you are using JDK 1.5 or newer, do not use this distribution. You should seek a more appropriate distribution for your Java version. (Note: The link provided in the source is historical; ensure you use a modern version of HAPI for modern JDKs).

  4. Use the Responder class to handle HL7v2 message exchanges

    master

    The Responder class performs the responding role in an HL7v2 message exchange (acting as a server). It receives an incoming message, identifies the appropriate application to handle it via an ApplicationRouter, and returns a response message.

    Key Characteristics:

    • It follows HL7's original mode processing rules.
    • It currently does not support enhanced mode, two-phase reply, continuation messages, or batch processing.
    • It requires an ApplicationRouter to be configured via setApplicationRouter to determine how messages are routed to specific software applications.
    // Conceptual usage pattern
    Socket inboundSocket = serverSocket.accept();
    Responder responder = new Responder(inboundSocket);
    responder.setApplicationRouter(myApplicationRouter);
    
    // The processMessage method is protected and typically called by the underlying connection logic
    String response = responder.processMessage(incomingMessageString);
  5. Configure connection metadata with ConnectionData

    master

    The ConnectionData class is used to encapsulate the configuration and metadata required for a TCP/IP connection to a remote HL7 server. It defines the host, ports, security settings (TLS), parsing logic, and the underlying protocol.

    Key configuration parameters include:

    • host: The remote server hostname or IP address.
    • port / outboundPort: The primary port for the connection.
    • port2 / inboundPort: An additional port (used in specific connection scenarios).
    • tls: A boolean indicating whether to use TLS encryption.
    • parser: The Parser instance used to handle HL7 messages.
    • protocol: The LowerLayerProtocol implementation (e.g., MLLP).
    • connectionTimeout: An optional integer specifying the connection timeout.
    • socketFactory: A custom SocketFactory for managing socket creation; if not provided, a StandardSocketFactory is used.
    // Example: Creating a basic connection configuration
    ConnectionData config = new ConnectionData(
        "localhost", 
        4556, 
        myParser, 
        myProtocol
    );
    
    // Example: Creating a TLS-enabled connection with a timeout
    ConnectionData tlsConfig = new ConnectionData(
        "remote-server.com", 
        4556, 
        myParser, 
        myProtocol, 
        true, 
        5000
    );
  6. Encode HL7 messages using MllpEncoder

    master

    The MllpEncoder class is used to wrap an HL7 message string into the Minimum Lower Layer Protocol (MLLP) format and write it to an OutputStream.

    An MLLP frame consists of a start byte (MllpConstants.START_BYTE), the message payload, and two end bytes (MllpConstants.END_BYTE1 and MllpConstants.END_BYTE2). The encoder ensures the entire message is sent in a single frame to prevent fragmentation issues.

    To use this, you must provide a Charset and a boolean flag omitBOM (to determine if the Byte Order Mark should be removed) during instantiation.

    // Note: MllpEncoder is package-private in the source, 
    // so usage assumes access within the ca.uhn.hl7v2.llp package
    
    MllpEncoder encoder = new MllpEncoder(StandardCharsets.UTF_8, true);
    String hl7Message = "MSH|^~\\|...";
    OutputStream outputStream = // your destination stream;
    
    encoder.putMessage(hl7Message, outputStream);