Example #1
0
  public void run() {
    //  Register our handlers with reactor
    PollItem poller = new PollItem(snapshot, ZMQ.Poller.POLLIN);
    loop.addPoller(poller, new Snapshots(), this);
    poller = new PollItem(collector, ZMQ.Poller.POLLIN);
    loop.addPoller(poller, new Collector(), this);
    loop.addTimer(1000, 0, new FlushTTL(), this);

    loop.start();
    loop.destroy();
    ctx.destroy();
  }
Example #2
0
  /**
   * And the main task now sets-up child tasks, then starts its reactor. If you press Ctrl-C, the
   * reactor exits and the main task shuts down.
   */
  public static void main(String[] args) {
    ZContext context = new ZContext();
    LBBroker arg = new LBBroker();
    //  Prepare our context and sockets
    arg.frontend = context.createSocket(ZMQ.ROUTER);
    arg.backend = context.createSocket(ZMQ.ROUTER);
    arg.frontend.bind("ipc://frontend.ipc");
    arg.backend.bind("ipc://backend.ipc");

    int clientNbr;
    for (clientNbr = 0; clientNbr < NBR_CLIENTS; clientNbr++) ZThread.start(new ClientTask());

    for (int workerNbr = 0; workerNbr < NBR_WORKERS; workerNbr++) ZThread.start(new WorkerTask());

    //  Queue of available workers
    arg.workers = new LinkedList<ZFrame>();

    //  Prepare reactor and fire it up
    ZLoop reactor = new ZLoop();
    PollItem item = new PollItem(arg.backend, ZMQ.Poller.POLLIN);
    reactor.addPoller(item, backendHandler, arg);
    reactor.start();

    context.destroy();
  }
Example #3
0
    @Override
    public int handle(ZLoop loop, PollItem item, Object arg_) {

      LBBroker arg = (LBBroker) arg_;
      ZMsg msg = ZMsg.recvMsg(arg.backend);
      if (msg != null) {
        ZFrame address = msg.unwrap();
        //  Queue worker address for load-balancing
        arg.workers.add(address);

        //  Enable reader on frontend if we went from 0 to 1 workers
        if (arg.workers.size() == 1) {
          PollItem newItem = new PollItem(arg.frontend, ZMQ.Poller.POLLIN);
          loop.addPoller(newItem, frontendHandler, arg);
        }

        //  Forward message to client if it's not a READY
        ZFrame frame = msg.getFirst();
        if (Arrays.equals(frame.getData(), WORKER_READY)) msg.destroy();
        else msg.send(arg.frontend);
      }
      return 0;
    }