zeromq socket recv返回null(java warper)

时间:2013-04-14 22:35:27

标签: java multithreading zeromq

我有使用java zeromq的网络客户端/服务器应用程序 通信框架。 我有主服务器和N个客户端轮询服务器。当服务器上线时 客户连接他,并在他们之间进行一些短暂的按摩 直到现在我完成了单个客户端,它工作正常。 但是当添加另一个客户端时(那是2) 我收到请求null作为返回按摩: request = socket.recv(0); 基于这个例子: http://zguide.zeromq.org/java:mtserver

我的代码(部分代码很长) 所有上下文和ZeroMq设置都已设置,而不是null 我总是得到这个例外:

Exception in thread "Thread-1" org.zeromq.ZMQException: Operation cannot be accomplished in current state(0x9523dfb)
        at org.zeromq.ZMQ$Socket.recv(Native Method)
        at com.controller.core.Daemon$1.run(Daemon.java:127)



for(int thread_nbr = 0; thread_nbr < m_iThreadPoolCount; thread_nbr++) {
                Thread worker_routine = new Thread() {

                   @Override
                   public void run() {
                     //synchronized(OBJ_LOCK) {
                      ZMQ.Socket socket = m_pNetworkManager.getContext().socket(ZMQ.REP);//context.socket(ZMQ.REP);                    
                      socket.connect ("inproc://workers");

                      while (true) {

                        /** Wait for next request from client (C string) */
                        byte[] request = null;
                        try{  
                            if(m_pNetworkManager.getContext()!=null) // its never null
                            {
                                request = socket.recv (0);

                            }

                        }catch (Exception e)
                        {

                    // it allays gets null exception            
                        }

                        boolean bFoundInList = false;

                        if(request!=null)
                        {

                                        // multi frame sending 
                                        socket.send(m_UT.getbyteArray(
                                                    m_UT.getReplayStructure(aStateMap_replay)
                                        ),ZMQ.SNDMORE);                                      
                                        socket.send(new byte[0], ZMQ.SNDMORE);
                                        byte[] byteFileStruct =  null;                                      
                                        byteFileStruct =  m_UT.serialize(stateFilesStruct);
                                        boolean send = socket.send(byteFileStruct,0);



                      } // socket.recv end

                     }
                  // }// synchronized block 
                }
                }; //Thread worker_routine

                worker_routine.start();
             }
             //  Connect work threads to client threads via a queue
             ZMQQueue zMQQueue = new ZMQQueue( m_pNetworkManager.getContext(),
                                               m_pNetworkManager.getClients(),
                                               m_pNetworkManager.getWorkers());
             zMQQueue.run();

             //  We never get here but clean up anyhow
             m_pNetworkManager.getClients().close();
             m_pNetworkManager.getWorkers().close();
             m_pNetworkManager.getContext().term();
    }

还添加了NetworkManager类

public class NetworkManager {

    /** ZeroMQ context  */ 
    private ZMQ.Context m_context = null;
    /** ZeroMQ socket */
    private ZMQ.Socket m_socket = null;
    /** representation of the clients */
    ZMQ.Socket m_clients = null;
    /** representation of the workers threads */
    ZMQ.Socket m_workers = null;
    /**
    * NetworkManager constructor.
    */   
    public NetworkManager()
    {
        ;
    }

    /**
     * Setup the network ZeroMQ network layer 
     * @param sControllerDomain the Controller domain name and port 
     */
    public void Init(String sControllerDomain)
    {
         /**  Prepare our context and socket */
         m_context = ZMQ.context(1);

         m_clients = m_context.socket(ZMQ.ROUTER);
        // m_clients = m_context.socket(ZMQ.REP);
         m_clients.bind (sControllerDomain);

         m_workers = m_context.socket(ZMQ.DEALER);
         m_workers.bind ("inproc://workers");
    }

    /**
     * Get ZeroMQ context 
     * @return ZMQ.Context
     */
    public ZMQ.Context getContext() {
        return m_context;
    }

    /**
     * get ZeroMQ Socket
     * @return ZMQ.Socket
     */
    public ZMQ.Socket getSocket() {
        return m_socket;
    }
    /**
     * get the workers as ZMQ.Socket
     * @return ZMQ.Socket
     */
    public ZMQ.Socket getWorkers() {
        return m_workers;
    }
    /**
     * get the Clients as ZMQ.Socket
     * @return ZMQ.Socket
     */
    public ZMQ.Socket getClients() {
        return m_clients;
    }

}

1 个答案:

答案 0 :(得分:0)

你的操作系统是什么?如果您使用的是Windows,则不支持使用m_workers.bind ("inproc://workers")的操作。 IIRC。