【问题标题】:How to read HL7 file and parse it using Apache Camel, Hapi & Spring (Java config)如何读取 HL7 文件并使用 Apache Camel、Hapi 和 Spring(Java 配置)对其进行解析
【发布时间】:2017-12-06 12:37:33
【问题描述】:

我正在尝试读取包含以下消息的 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 类;

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 的文件的位置,其中包含上述消息。

以下是处理器类;

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

的此链接中给出的文档

以下是配置文件和主要应用程序:

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 消息并从中获取分段详细信息。

【问题讨论】:

  • 以前从未使用过此组件,但您是否尝试过使用HL7DataFormat,如下面的 JUnit 测试所示? HL7ValidateTest.java
  • 你能告诉我在上面的代码中在哪里使用它以及它的作用吗?
  • 真的我从来没有用过这个,所以这只是猜测:在你的SimpleRouteBuilder实例化HL7DataFormat使用DataFormat hl7 = new HL7DataFormat();。然后在您的路线中使用它,在process() 方法之前添加.unmarshal(hl7)。见HL7 DataFormat
  • 我试过了,但还是不行,谢谢你的帮助。

标签: spring apache-camel hl7 hapi hl7-v2


【解决方案1】:

我认为您的路线中缺少一些步骤。尝试先将您的文件消息转换为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

干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多