java解析HL7协议报文工具 因为项目需要解析HL7协议报文,网上找到的工具都是解析成带位置信息的xml格式或者json格式,然后需要自己根据需要获取的位置来获取信息。而在生成的HL7协议报文的时候也是需要先生成xml或json格式再进行转换。想着希望找到一个直接解析生成类然后直接用的工具。 后来我找到了这个ca.uhn.hapi,能将HL7报文直接解析成相应的类,通过调用:PipeParser.parse(message, hl7str)来解析报文,将数据填充到message类里面,其中message是工具里面的继承Message类的子类,例如:QBP_Q11、RSP_K23等。而生成HL7报文的时候又可以调用message.encode()来生成,不过需要先设置MSH头
一、依赖:
<dependency>
<groupId>ca.uhn.hapi</groupId>
<artifactId>hapi-base</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi</groupId>
<artifactId>hapi-structures-v24</artifactId>
<version>2.3</version>
</dependency>
因为我们是通过socket对接实现的,先实现socket代码
1.启动类
@SpringBootApplication
@EnableCaching
@Configuration
@EnableScheduling
@ServletComponentScan
@EnableTransactionManagement
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(Application.class, args);
SpringContextUtil.setApplicationContext(context);
SocketServer demoServer = new SocketServer();
try {
demoServer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.SocketServer(服务端)
@Component
public class SocketServer{
private static final Logger logger = LoggerFactory.getLogger(SocketServer.class);
public void start() throws IOException {
ExecutorService newCacheThreadPool = new ThreadPoolExecutor(
2,
5,
3,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.DiscardOldestPolicy()
);
ServerSocket server = new ServerSocket(SocketProperties.getSocketPort());
logger.info("socket服务端启动....端口:{}",SocketProperties.getSocketPort());
while (true) {
final Socket socket = server.accept();
logger.info("开始处理请求");
newCacheThreadPool.execute(new Runnable() {
@Override
public void run() {
try {
handler(socket);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
private void handler(Socket socket) throws Exception {
byte[] bytes = null;
InputStream inputStream = null;
try {
inputStream = socket.getInputStream();
int bufflenth = inputStream.available();
while (bufflenth != 0) {
bytes = new byte[bufflenth];
//读取数据(阻塞)
inputStream.read(bytes);
bufflenth = inputStream.available();
}
String result = new String(bytes);
result = new String(bytes).replaceAll("\n","\r")
.replaceAll("\u000B","").replaceAll("amp;","").replaceAll("\u001c","");
logger.info("请求:{}" ,result);
TestHL7.hl7Text2Obj(result,socket);
} catch (IOException e) {
e.printStackTrace();
} finally {
logger.info("socket关闭");
try {
inputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.SocketClient
@Slf4j
public class SocketClient {
/**
* 发送socket消息
*/
public static String sendSocket(String message){
Socket socket = null;
String result = "";
InputStream in = null;
OutputStream out = null;
try {
socket = new Socket(SocketProperties.getSocketSendIp(),SocketProperties.getSocketSendPort());
in = socket.getInputStream();
out = socket.getOutputStream();
log.info("----------开始发送 MESSAGE-----------{}",message);
out.write(message.getBytes(SocketConstants.UTF_8));
out.flush();
socket.shutdownOutput();
byte[] byteBuffer = new byte[8*1024];
in.read(byteBuffer);
result = new String(byteBuffer);
log.info("Received from Server message: " + result);
result = result.replaceAll("\n","\r")
.replaceAll("\u000B","").replaceAll("amp;","").replaceAll("\u001c","");
return result;
}catch (Exception e){
log.error("发送socket消息失败",e);
}finally {
if(socket!=null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(in!=null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(out!=null) {