2012-12-29 40 views
0

IM使用ZeroMQ到客戶端/服務器應用程序, 現在在我的MT服務器我嘗試設置超時,我試圖在服務器上設置:的Java:zeromq,嘗試設置響應超時在MT服務器

socket.setReceiveTimeOut(2000); 
socket.setSendTimeOut(2000); 

沒有運氣,我如何設置響應超時。

,這是我的多線程服務器代碼,這是從zeromq例子採取MT服務器:

/* 
* Multithreaded Hello World server in Java 
* 
* @author Vadim Shalts 
* @email [email protected] 
* 
*/ 

import org.zeromq.ZMQ; 
import org.zeromq.ZMQQueue; 

class mtserver { 
    static void main(String[] args) { 

     final ZMQ.Context context = ZMQ.context(1); 

     ZMQ.Socket clients = context.socket(ZMQ.ROUTER); 
     clients.bind ("tcp://*:5555"); 

     ZMQ.Socket workers = context.socket(ZMQ.DEALER); 
     workers.bind ("inproc://workers"); 

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

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

       while (true) { 

        // Wait for next request from client (C string) 
        byte[] request = socket.recv (0); 
        System.out.println ("Received request: ["+new String(request,0,request.length-1)+"]"); 

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

        // Send reply back to client (C string) 
        byte[] reply = "World ".getBytes(); 
        reply[reply.length-1] = 0; //Sets the last byte of the reply to 0 
        socket.send(reply, 0); 
       } 
      } 
     }; 
     worker_routine.start(); 
     } 
     // Connect work threads to client threads via a queue 
     ZMQQueue zMQQueue = new ZMQQueue(context,clients, workers); 
     zMQQueue.run(); 

     // We never get here but clean up anyhow 
     clients.close(); 
     workers.close(); 
     context.term(); 
    } 
} 
+0

您確定超時時間是以毫秒而不是秒爲單位嗎? –

+0

我希望我能告訴你,但我找不到任何這個信息... 沒有Java文檔 – user63898

回答

0

爲什麼你需要超時?原始示例不使用它。

+0

這個例子是簡單的例子,顯示簡單的MT服務器,與此。 現在我需要在客戶端死機時實現超時。 – user63898

0

爲什麼你需要在服務器上超時?

在客戶端放置超時的自然場所。

socket.setReceiveTimeOut(int milliseconds);在客戶端工作。

相關問題