如何读取 HL7 文件并使用 Apache Camel、Hapi 和 Spring(Java 配置)对其进行解析

How to read HL7 file and parse it using Apache Camel, Hapi & Spring (Java config)

我正在尝试读取包含以下消息的 hl7 文件

MSH|^~\&|MYSENDER|MYRECEIVER|MYAPPLICATION||200612211200||QRY^A19|1234|P|2.3
QRD|200612211200|R|I|GetPatient|||1^RD|0101701234|DEM||

使用 Apache camel、Hapi 和 Spring 框架(Java 配置)。我想解析上面的消息并从中获取段详细信息。我正在使用 HL7 2.3 版。以下是我的 RouteBuilder class;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
import example.springcamel.processors.Hl7MessageProcessor;

@Component
public class SimpleRouteBuilder extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("file://E:/projects/hl7/file_to_read/input/")
            .process(new Hl7MessageProcessor())
            .end();
        }
    }

E:/projects/hl7/file_to_read/input/ 这是我有一个名为 hl7_message.hl7 的文件的位置,其中包含上述消息。

以下是处理器 class;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import ca.uhn.hl7v2.model.Message;

public class Hl7MessageProcessor implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception {
       Message message = exchange.getIn().getBody(Message.class);
       System.out.println("Original message: " + message);
    }
}

从上面的代码中,我得到的原始消息为空。我正在关注来自 Apache Camel http://camel.apache.org/hl7.html

的 link 中给出的文档

配置文件和主要应用如下:

SpringConfiguration.java

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "example.springcamel")
public class SpringConfiguration {

}

RoutesConfiguration.java

import org.apache.camel.spring.javaconfig.CamelConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "example.springcamel.routes")
public class RoutesConfiguration extends CamelConfiguration {

}

MainApplication.java

import org.apache.camel.CamelContext;
import org.apache.camel.spring.SpringCamelContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import example.springcamel.configuration.SpringConfiguration;

public class MainApplication {
    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws Exception {
        AbstractApplicationContext springContext = new 
                AnnotationConfigApplicationContext(SpringConfiguration.class);
        CamelContext camelContext = SpringCamelContext.springCamelContext(springContext, false);
        try {
            camelContext.start();
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            camelContext.stop();
            springContext.close();
        }
    }   
}

我是 HL7 的新手,有人好心帮我解析上面的 HL7 消息并从中获取段详细信息。

我认为您在路线中遗漏了一些步骤。尝试先将您的文件消息转换为 String,然后将其解组为 HL7:

from("file:src/test/resources/hl7?noop=true")
    .convertBodyTo(String.class)
    .unmarshal()
    .hl7(false)
    .log("The Message body is: ${body}")
    .process(new Processor() {
        public void process(Exchange exchange) throws Exception {
            final Message message = exchange.getIn().getBody(Message.class);
            System.out.println("Original message: " + message);
        }
    })
    .to("mock:result");

也就是说,我已经尝试处理您的输出,但出现此错误:

ca.uhn.hl7v2.HL7Exception: The HL7 version 2.3 QRD is not recognized

我想我漏掉了行尾的 \r 字符。但是我用 this message:

验证了测试
MSH|^~\&|HL7ABLAB|HNA500|HNAM|HNAM|20090911132151||ADT^A01|
Q30235031T29347435X328970|A|2.3|123
EVN|A01|20090911132100|||^DRONE_PM1^DRONE_PM^^^^^^^Personnel
PID|1||1357920591||IntFace1101A^WinTask^^^^^Current||19801117|M||||||||||
10000476524^^^FIN^FIN NBR|100000451||||||0
PV1|1|Inpatient|CD:16067689^CD:16067691^CD:16067741^Uniontown Hospit^^Bed(s)
^Uniontown Hospit||||||||||||||501455^Orr^Maggi^^^^^^External ID^Personnel^^^
External
Identifier~25584^Orr^Maggi^^^^^^PERSONNEL PRIMARY
IDENTIFIER^Personnel^^^Personnel Primary Identifier|Inpatient|||||||||||||||||||
||
Uniontown Hospit||Active|||20090911132100

测试:

@Test
public void test() throws InterruptedException {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    mock.expectedBodyReceived().body(Message.class);

    assertMockEndpointsSatisfied();
}

结果:

Original message: MSH|^~\&|HL7ABLAB|HNA500|HNAM|HNAM|20090911132151||ADT^A01|Q30235031T29347435X328970|A|2.3|123
EVN|A01|20090911132100|||^DRONE_PM1^DRONE_PM^^^^^^^Personnel
PID|1||1357920591||IntFace1101A^WinTask^^^^^Current||19801117|M||||||||||
10000476524^^^FIN^FIN NBR|100000451||||||0
PV1|1|Inpatient|CD:16067689^CD:16067691^CD:16067741^Uniontown Hospit^^Bed(s)
^Uniontown Hospit||||||||||||||501455^Orr^Maggi^^^^^^External ID^Personnel^^^
External
Identifier~25584^Orr^Maggi^^^^^^PERSONNEL PRIMARY
IDENTIFIER^Personnel^^^Personnel Primary Identifier|Inpatient|||||||||||||||||||
||
Uniontown Hospit||Active|||20090911132100

依赖关系:

<dependency>
    <groupId>ca.uhn.hapi</groupId> 
    <artifactId>hapi-structures-v23</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-hl7</artifactId>
</dependency>

您可以访问完整的测试 in this repo

干杯!