zeromq SYNCHRONOUS REQUEST / RESPONSE fifo

时间:2015-11-20 15:54:32

标签: java zeromq

我有一个zeromq回复服务器,如下所示:

public static void runDocRequirementServer() throws Exception {
    ZMQ.Socket.bind("tcp://localhost:5555");

    //boolean serverBusy = false;

    while (!Thread.currentThread().isInterrupted()) {
        // Wait for next request from the client
        //byte[] request = ZeroMqServerFactory.ZMQ_SERVER.recv(0);
        //System.out.println("Received Hello");


        //boolean hasRecieveMore = ZeroMqServerFactory.ZMQ_SERVER.hasReceiveMore();



        byte[] recv = ZeroMqServerFactory.ZMQ_SERVER.recv();



        //serverBusy = true;

        System.out.println("RECV MESSAGE!");

        Object receivedObject = null;

        //DocRequirement docRequirementRecv = null;
        try {
            receivedObject = ByteObjectManipulator.deserialize(recv);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        if(!(receivedObject instanceof DocRequirement)) {
            continue;
        }


        DocRequirement docRequirement = (DocRequirement) receivedObject;


        System.out.println("Received: " + docRequirement.getId());

        //docRequirementRecv.setId(99);

        DocProduced docProduced = new DocProduced();
        docProduced.setId(docRequirement.getId() + 1);

        byte[] objectSerialized = ByteObjectManipulator.serialize(docProduced);

        // Do some 'work'
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Send reply back to client
        //String reply = "World";
        ZeroMqServerFactory.ZMQ_SERVER.send(objectSerialized);

        //serverBusy = false;
    }



    try {
        ZeroMqServerFactory.restartServer();
    } catch (Exception e) {
        ZeroMqServerFactory.stopServer();
    }

}

我有这样的客户:

private DocProduced sendMessage(String protocol, String ip, String port, int receiveTimeout, DocRequirement docRequirement) throws MessageSendingException {

    String connectString = null;

    try {
        connectString = ConnectStringProducer.produceConnectString(protocol, ip, port);
    } catch (ConnectionPropertyException e) {
        throw new MessageSendingException(e);
    }

    String uniqueIdentity = identity.getUser().getLogin() + "_" + System.nanoTime();

    byte[] objectSerialized = new byte[0];
    try {
        objectSerialized = ByteObjectManipulator.serialize(docRequirement);
    } catch (IOException e) {
        throw new MessageSendingException(e);
    }


    //init context
    ZMQ.Context context = ZMQ.context(1);

    // create request socket
    ZMQ.Socket client = context.socket(ZMQ.REQ);
    client.setIdentity(uniqueIdentity.getBytes());
    client.setReceiveTimeOut(receiveTimeout);
    client.connect(connectString);

    //send object
    client.send(objectSerialized);


    //wait to receive

    byte[] receivedByteArray;

    try{
        receivedByteArray = client.recv();
    } catch (Exception e) {
        client.close();
        context.term();

        throw new MessageSendingException(e);
    }


    if(receivedByteArray == null) {
        client.close();
        context.term();

        throw new MessageSendingException("Received data null");
    }



    Object receivedObject;

    try {
        receivedObject = ByteObjectManipulator.deserialize(receivedByteArray);
    } catch (IOException e) {
        client.close();
        context.term();

        throw new MessageSendingException(e);
    } catch (ClassNotFoundException e) {
        client.close();
        context.term();

        throw new MessageSendingException(e);
    }


    if(!(receivedObject instanceof DocProduced)) {
        client.close();
        context.term();

        throw new MessageSendingException("Wrong reply received from server");
    }


    //cast
    DocProduced docProduced = (DocProduced) receivedObject;


    client.close();
    context.term();

    return docProduced;
}

现在如果我连续发送三个请求,它们不会被连续回复套接字接收。大部分时间我得到1,3,2而不是1,2,3。有没有办法指示回复套接字以fifo方式回复?请帮忙。

提前致谢

0 个答案:

没有答案