Beispiel #1
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();
  }
Beispiel #2
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();
  }
Beispiel #3
0
  public clonesrv5() {
    port = 5556;
    ctx = new ZContext();
    kvmap = new HashMap<String, kvmsg>();
    loop = new ZLoop();
    loop.verbose(false);

    //  Set up our clone server sockets
    snapshot = ctx.createSocket(ZMQ.ROUTER);
    snapshot.bind(String.format("tcp://*:%d", port));
    publisher = ctx.createSocket(ZMQ.PUB);
    publisher.bind(String.format("tcp://*:%d", port + 1));
    collector = ctx.createSocket(ZMQ.PULL);
    collector.bind(String.format("tcp://*:%d", port + 2));
  }
Beispiel #4
0
    @Override
    public int handle(ZLoop loop, PollItem item, Object arg_) {

      LBBroker arg = (LBBroker) arg_;
      ZMsg msg = ZMsg.recvMsg(arg.frontend);
      if (msg != null) {
        msg.wrap(arg.workers.poll());
        msg.send(arg.backend);

        //  Cancel reader on frontend if we went from 1 to 0 workers
        if (arg.workers.size() == 0) {
          loop.removePoller(new PollItem(arg.frontend, 0));
        }
      }
      return 0;
    }
Beispiel #5
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;
    }