在同一个Socket连接中发送多个HL7消息

时间:2016-08-01 23:00:22

标签: java sockets hl7

我在询问之前搜索了这个问题,但我找不到类似的东西。我开发了一个客户端/服务器解决方案来发送/接收HL7消息。我使用套接字将客户端连接到服务器,通过此连接,我只能使用OutputSteam对象发送1个HL7消息。如何在同一个套接字连接中发送多个HL7?我尝试了不同的方法,但它们没有正常工作。

以下是来自客户端的代码:

//Create socket that is connected to server on specified port
        System.out.println("Connecting to Server....");
        Socket socket  = new Socket(ipServer, serverPort);
        System.out.println("Connected to Server");

        StringBuffer HL7Message1 = new StringBuffer();

        //Message 1
        HL7Message1
        .append(START_BLOCK)
        .append("MSH|^~\\&|NES|NINTENDO|TESTSYSTEM|TESTFACILITY|20010101000000||ADT^A04|Q000000000000000001|P|2.3")
        .append(CARRIAGE_RETURN)
        .append("EVN|A04|20010101000000|||^KOOPA^BOWSER^^^^^^^CURRENT")
        .append(CARRIAGE_RETURN)
        .append("PID|1||123456789|0123456789^AA^^JP|BROS^MARIO^HELLO^WORLD^ONE^||19850101000000|M|||123 FAKE STREET^MARIO \\T\\ LUIGI BROS PLACE^TOADSTOOL KINGDOM^NES^A1B2C3^JP^HOME^^1234|1234|(555)555-0123^HOME^JP:1234567|||S|MSH|12345678|||||||0|||||N")
        .append(CARRIAGE_RETURN)
        .append("NK1|1|PEACH^PRINCESS^^^^|SO|ANOTHER CASTLE^^TOADSTOOL KINGDOM^NES^^JP|(123)555-1234|(123)555-2345|NOK|||||||||||||")
        .append(CARRIAGE_RETURN)
        .append("NK1|2|TOADSTOOL^PRINCESS^^^^|SO|YET ANOTHER CASTLE^^TOADSTOOL KINGDOM^NES^^JP|(123)555-3456|(123)555-4567|EMC|||||||||||||")
        .append(CARRIAGE_RETURN)
        .append("PV1|1|O|ABCD^EFGH^|||^^|123456^DINO^YOSHI^^^^^^MSRM^CURRENT^^^NEIGHBOURHOOD DR NBR^|^DOG^DUCKHUNT^^^^^^^CURRENT||CRD|||||||123456^DINO^YOSHI^^^^^^MSRM^CURRENT^^^NEIGHBOURHOOD DR NBR^|AO|0123456789|1|||||||||||||||||||MSH||A|||20010101000000"
                )
        .append(CARRIAGE_RETURN)
        .append("IN1|1|PAR^PARENT||||LUIGI")
        .append(CARRIAGE_RETURN)
        .append("IN1|2|FRI^FRIEND||||PRINCESS")
        .append(CARRIAGE_RETURN)
        .append(END_BLOCK)
        .append(CARRIAGE_RETURN);

        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();

        //Send the MLLP-wrapped HL7 message to the server
        out.write(HL7Message1.toString().getBytes());

        byte[] byteBuffer =  new byte[200];
        in.read(byteBuffer);
        System.out.println("Received from Server: " + new String(byteBuffer));

从服务器端

public String getMessage(InputStream anInputStream) throws IOException  {

            boolean end_of_message = false;
            StringBuffer parsedMessage = new StringBuffer();

            int characterReceived = 0;

            try {
                characterReceived = anInputStream.read();
            } catch (SocketException e) {
                System.out
                .println("Unable to read from socket stream. "
                        + "Connection may have been closed: " + e.getMessage());
                return null;
            }

            if (characterReceived == END_OF_TRANSMISSION) {
                return null;
            }

            if (characterReceived != START_OF_BLOCK) {
                throw new RuntimeException(
                        "Start of block character has not been received");
            }

            while (!end_of_message) {
                characterReceived = anInputStream.read();

                if (characterReceived == END_OF_TRANSMISSION) {
                    throw new RuntimeException(
                            "Message terminated without end of message character");
                }

                if (characterReceived == END_OF_BLOCK) {
                    characterReceived = anInputStream.read();

                    if (characterReceived != CARRIAGE_RETURN) {
                        throw new RuntimeException(
                                "End of message character must be followed by a carriage return character");
                    }
                    end_of_message = true;
                } else {
                    parsedMessage.append((char) characterReceived);
                }
            }

如何在同一套接字连接中发送更多HL7消息?

1 个答案:

答案 0 :(得分:2)

您真正想要做的是将您的套接字与数据分开。我建议像这样使用TCP Client Server来管理你的套接字。然后,您只需向您的客户写一条消息。我有一项为我服务的服务。 基本上,我打包了HL7的任何字符串,看起来你已经知道该怎么做了,然后调用TCP服务器的Send方法。

我看起来像这样,但我认为链接中的那个看起来很相似:

public int Send(string data)
{
    TcpClient sender = new TcpClient();
    IPEndPoint localEP = new IPEndPoint(IPAddress.Any, Port);
    try
        {
            TcpServerConnection cn;

            if (connections.Count == 0)
            {
                //if there are no connections then we end up here.
                ConnectSocket(sender, localEP);
            }

            cn = connections[0];

            if (cn.Socket.Connected && cn.verifyConnected())
            {
                cn.sendData(data);
                return connections.Count;
            }
            cn.Socket.Close();

            ConnectSocket(sender, localEP);
            if (cn.Socket.Connected && cn.verifyConnected())
            {
                cn.sendData(data);
                return connections.Count;
            }
            cn.Socket.Close();
            return -1;
        }
        catch (Exception e)
        {                   
        }
    }

重要的是,您可以看到TCP服务器确定是否有可用的套接字并在必要时打开一个套接字。然后它将数据发送到该行。如果你不将传输/编码与HL7消息分开,那么我认为你会遇到很多困难。如果你想等待MLLP版本2的ACK,我个人在ACK中提取出站消息ID和返回的消息ID并比较它们:

if (lastOutMessageID == lastOutAckID || lastOutMessageID == "")
{
    lastOutMessageID = SendNextHL7Message;
}

然后我听取了ACK,并对进入的数据提出了一个事件:

void _contractManager_OnAckReceived(string msgID, string ackStatus)
{
    lastOutAckID = msgID;
    lastOutStatus = ackStatus;
    lastAckTime = DateTime.Now;
    outRetries = 0;
    DoNextHL7();
}

但是短版本是在他们自己的类中管理传输和套接字,只是从它们发送和等待数据。