如何读取HL7文件并使用Apache Camel,Hapi和Spring(Java配置)解析它

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何读取HL7文件并使用Apache Camel,Hapi和Spring(Java配置)解析它相关的知识,希望对你有一定的参考价值。

我正在尝试读取包含以下消息的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的文件的位置,上面有上述消息。

以下是Processor类;

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);
    }
}

从上面的代码,我得到原始消息为null。我正在关注Apache Camel http://camel.apache.org/hl7.html在此链接中提供的文档

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

spring configuration.Java

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

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

}

routes configuration.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 {

}

main application.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

我想我错过了最后一行的 角色。但我用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

干杯!

以上是关于如何读取HL7文件并使用Apache Camel,Hapi和Spring(Java配置)解析它的主要内容,如果未能解决你的问题,请参考以下文章

Apache Camel读取MongoDB Collection - 不处理任何行

Apache Camel:文件到 BeanIO 并基于 id 合并 beanIO 对象

如何在Apache Camel Aggregator关联中加入多个标头

RabbitMQ - Apache Camel 读取消息如何处理失败的消息

在 karaf 中启动包时出错:“通过两个依赖链”

如何使用 Apache Camel 来监控文件更改?