2014-01-06 3 views
1

jeromq-0.3.2.jar 라이브러리가있는이 공식 Java 예제를 Eclipse에서 실행합니다. "실행"하면 작동하지 않으며 일부 설정 한 경우에만 작동합니다 중단 점 및 "디버그".zeromq route-req java 예제가 작동하지 않습니다.

메시지가 손실 된 것 같습니다. 경로 req 패턴을 사용하여 내 자신의 응용 프로그램 도이 문제가 있습니다.

이것이 작동하지 않는 경우 공식적인 예입니다. 무엇을 할 수 있습니까? 누군가 그것을 시험해보고 이유를 알아낼 수 있습니까?

http://zguide.zeromq.org/java:rtreq

또는 코드 현재 위치 :

import org.zeromq.ZMQ; 
import org.zeromq.ZMQ.Context; 
import org.zeromq.ZMQ.Socket; 

import java.util.Random; 

/** 
* ROUTER-TO-REQ example 
*/ 
public class rtreq 
{ 
    private static Random rand = new Random(); 
    private static final int NBR_WORKERS = 10; 

    private static class Worker extends Thread { 

     @Override 
     public void run() { 

      Context context = ZMQ.context(1); 
      Socket worker = context.socket(ZMQ.REQ); 
      ZHelper.setId (worker); // Set a printable identity 

      worker.connect("tcp://localhost:5671"); 

      int total = 0; 
      while (true) { 
       // Tell the broker we're ready for work 
       worker.send ("Hi Boss"); 

       // Get workload from broker, until finished 
       String workload = worker.recvStr(); 
       boolean finished = workload.equals ("Fired!"); 
       if (finished) { 
        System.out.printf ("Completed: %d tasks\n", total); 
        break; 
       } 
       total++; 

       // Do some random work 
       try { 
        Thread.sleep (rand.nextInt (500) + 1); 
       } catch (InterruptedException e) { 
       } 
      } 
      worker.close(); 
      context.term(); 
     } 
    } 

    /** 
    * While this example runs in a single process, that is just to make 
    * it easier to start and stop the example. Each thread has its own 
    * context and conceptually acts as a separate process. 
    */ 
    public static void main (String[] args) throws Exception { 
     Context context = ZMQ.context(1); 
     Socket broker = context.socket(ZMQ.ROUTER); 
     broker.bind("tcp://*:5671"); 

     for (int workerNbr = 0; workerNbr < NBR_WORKERS; workerNbr++) 
     { 
      Thread worker = new Worker(); 
      worker.start(); 
     } 

     // Run for five seconds and then tell workers to end 
     long endTime = System.currentTimeMillis() + 5000; 
     int workersFired = 0; 
     while (true) { 
      // Next message gives us least recently used worker 
      String identity = broker.recvStr(); 
      broker.sendMore (identity); 
      broker.recvStr();  // Envelope delimiter 
      broker.recvStr();  // Response from worker 
      broker.sendMore (""); 

      // Encourage workers until it's time to fire them 
      if (System.currentTimeMillis() < endTime) 
       broker.send ("Work harder"); 
      else { 
       broker.send ("Fired!"); 
       if (++workersFired == NBR_WORKERS) 
        break; 
      } 
     } 

     broker.close(); 
     context.term(); 
    } 
} 

답변

0

은 각 직원이 고유 한 ID를 가지고 있는지 확인해야,이 예제가 작동하려면.

는 예를 들어, 나는
ZHelper.setId (worker); // Set a printable identity 

을 제거하고 workerId 문자열로 작업자 클래스에 생성자를 추가합니다.

결과 :

import org.zeromq.ZMQ; 

import java.util.Random; 

/** 
* ROUTER-TO-REQ example 
*/ 
public class rtreq { 
    private static Random rand = new Random(); 
    private static final int NBR_WORKERS = 10; 

    private static class Worker extends Thread { 

     private String workerId; 

     Worker(String workerId) { 
      this.workerId = workerId; 
     } 

     @Override 
     public void run() { 
      ZMQ.Context context = ZMQ.context(1); 
      ZMQ.Socket worker = context.socket(ZMQ.REQ); 
      worker.setIdentity(workerId.getBytes()); 

      worker.connect("tcp://localhost:5671"); 

      int total = 0; 
      while (true) { 
       // Tell the broker we're ready for work 
       worker.send("Hi Boss"); 

       // Get workload from broker, until finished 
       String workload = worker.recvStr(); 
       boolean finished = workload.equals("Fired!"); 
       if (finished) { 
        System.out.printf(workerId + " completed: %d tasks\n", total); 
        break; 
       } 
       total++; 

       // Do some random work 
       try { 
        Thread.sleep(rand.nextInt(500) + 1); 
       } catch (InterruptedException e) { 
       } 
      } 

      worker.close(); 
      context.term(); 
     } 
    } 

    /** 
    * While this example runs in a single process, that is just to make 
    * it easier to start and stop the example. Each thread has its own 
    * context and conceptually acts as a separate process. 
    */ 
    public static void main(String[] args) throws Exception { 
     ZMQ.Context context = ZMQ.context(1); 
     ZMQ.Socket broker = context.socket(ZMQ.ROUTER); 
     broker.bind("tcp://*:5671"); 

     // starting all workers 
     for (int workerNbr = 0; workerNbr < NBR_WORKERS; workerNbr++) { 
      Thread worker = new Worker("worker-" + workerNbr); 
      worker.start(); 
     } 

     // Run for five seconds and then tell workers to end 
     long endTime = System.currentTimeMillis() + 5000; 
     int workersFired = 0; 
     while (true) { 
      // Next message gives us least recently used worker 
      String identity = broker.recvStr(); 
      broker.sendMore(identity); 
      broker.recvStr();  // Envelope delimiter 
      broker.recvStr();  // Response from worker 
      broker.sendMore(""); 

      // Encourage workers until it's time to fire them 
      if (System.currentTimeMillis() < endTime) 
       broker.send("Work harder"); 
      else { 
       broker.send("Fired!"); 
       if (++workersFired == NBR_WORKERS) 
        break; 
      } 
     } 

     broker.close(); 
     context.term(); 
    } 
} 

시스템 아웃 :

worker-2 completed: 27 tasks 
worker-3 completed: 20 tasks 
worker-8 completed: 24 tasks 
worker-6 completed: 23 tasks 
worker-0 completed: 20 tasks 
worker-9 completed: 21 tasks 
worker-7 completed: 20 tasks 
worker-5 completed: 21 tasks 
worker-4 completed: 19 tasks 
worker-1 completed: 25 tasks 
관련 문제