@Override
    public void run() {
      while (!Thread.currentThread().isInterrupted()) {
        String r = null;
        try {
          r = _outputBuffer.poll(250, TimeUnit.MILLISECONDS);
        } catch (InterruptedException ie) {
          _log.error("SendThread interrupted", ie);
          return;
        }

        if (r != null) {
          if (_isPrimaryInferenceInstance) {
            _zmqSocket.send(_topicName, ZMQ.SNDMORE);
            _zmqSocket.send(r.getBytes(), 0);
          }
        }

        final String h = _heartbeatBuffer.poll();
        if (h != null) {
          if (_isPrimaryInferenceInstance) {
            _zmqSocket.send(HEARTBEAT_TOPIC.getBytes(), ZMQ.SNDMORE);
            _zmqSocket.send(h.getBytes(), 0);
            _log.debug("heartbeat=" + h);
          }
        }

        if (r == null) {
          // skip output message below if no queue content
          continue;
        }

        if (processedCount > 50) {
          _log.warn(
              "Inference output queue(primary="
                  + _isPrimaryInferenceInstance
                  + "): processed 50 messages in "
                  + (new Date().getTime() - markTimestamp.getTime()) / 1000
                  + " seconds; current queue length is "
                  + _outputBuffer.size());

          markTimestamp = new Date();
          processedCount = 0;
        }

        processedCount++;
      }
    }
 public void run() {
   while (!Thread.currentThread().isInterrupted()) {
     try {
       OrderBook ob = inboundQueue.take();
       String message = gson.toJson(ob);
       socket.send(message, 0);
     } catch (InterruptedException e) {
       log.severe(
           "ZeroMQPublisher interrupted waiting for next order book in queue: " + e.getMessage());
       e.printStackTrace();
     }
   }
   socket.close();
   context.term();
 }
Exemple #3
0
  /** @param args */
  public static void main(String[] args) {
    // output version
    System.out.println(String.format("0MQ %s", ZMQ.getVersionString()));

    // call TPS calculator every second
    Timer timer = new Timer();
    timer.schedule(new TPSCalculator(), 1000, 1000);

    ZMQ.Context context = ZMQ.context(1);
    // Socket to talk to server
    ZMQ.Socket requester = context.socket(ZMQ.PUSH);
    requester.connect("tcp://localhost:5555");

    // create the request body
    JSONObject obj = new JSONObject();
    obj.put("id", "telecell");
    obj.put("type", "telecell");
    obj.put("isPattern", false);
    obj.put("att", "value");

    // create xrap request
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json");
    String request =
        XRapLibrary.getInstance().createPOST("/updateContext/", headers, obj.toString());

    for (int requestNbr = 0; requestNbr != SAMPLE_SIZE; requestNbr++) {
      // send a request
      obj.put("id", "telecell" + requestNbr);
      obj.put("att", "value" + requestNbr);
      request = XRapLibrary.getInstance().createPOST("/updateContext/", headers, obj.toString());
      requester.send(request);

      // byte[] reply = requester.recv(0);

      // count the transaction
      transactionCount++;
    }

    // stop the timer
    timer.cancel();

    // printout results
    printResults();

    requester.close();
    context.term();
  }
Exemple #4
0
  public static void main(String[] args) throws Exception {
    ZMQ.Context context = ZMQ.context(1);
    //  Socket to talk to clients
    ZMQ.Socket socket = context.socket(ZMQ.REP);
    socket.bind("tcp://*:5555");

    while (!Thread.currentThread().isInterrupted()) {
      byte[] reply = socket.recv(0);
      System.out.println("Received Hello");
      Thread.sleep(1000); //  Do some 'work'
      String request = "World";
      socket.send(request.getBytes(), 0);
    }
    socket.close();
    context.term();
  }
Exemple #5
0
  @Subscribe
  public void handleJob(JobPacket jobPacket) throws Exception {
    JobPacket packetToClient = new JobPacket();
    if (jobPacket.type == JobPacket.JOB_REQ) {
      jobQueue.add(jobPacket.hash);
      packetToClient.type = JobPacket.JOB_ACCEPTED;
      packetToClient.result = "none";
    }
    if (jobPacket.type == JobPacket.JOB_STATUS) {

      // check under /result/<hash>
      try {
        if ((zooKeeper.exists(Joiner.on("/").join(ZK_JOBS, jobPacket.hash), false) != null)
            && (zooKeeper.exists(Joiner.on("/").join(ZK_RESULT, jobPacket.hash), false) == null)) {
          System.out.println("Job in progress, please wait!");
          packetToClient.type = JobPacket.JOB_PROGRESS;
          packetToClient.result = "none";
        }
        if ((zooKeeper.exists(Joiner.on("/").join(ZK_JOBS, jobPacket.hash), false) == null)
            && (zooKeeper.exists(Joiner.on("/").join(ZK_RESULT, jobPacket.hash), false) == null)) {
          System.out.println("No such Job, please enter your job again!");
          packetToClient.type = JobPacket.JOB_NOTFOUND;
          packetToClient.result = "none";
        }
        if (zooKeeper.exists(Joiner.on("/").join(ZK_RESULT, jobPacket.hash), false) != null) {
          byte[] data =
              zooKeeper.getData(Joiner.on("/").join(ZK_RESULT, jobPacket.hash), false, null);
          packetToClient.type = JobPacket.JOB_RESULT;
          if (data == null) {
            System.out.println("Result not found!");
            packetToClient.result = null;
          } else {
            String result = new String(data);
            packetToClient.result = result;
            System.out.println("Result found!");
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    socket.send(SerializationUtils.serialize(packetToClient), 0);
  }
Exemple #6
0
  @Override
  protected void run() throws Exception {
    while (!Thread.currentThread().isInterrupted() && isRunning()) {
      byte[] message = "".getBytes();
      poller.poll(200);
      if (poller.pollin(0)) {
        try {
          message = listener.recv(0);
        } catch (ZMQException e) {
          if (e.getErrorCode() == ZMQ.Error.ETERM.getCode()) {
            break;
          }
        }
        log.info("hi: {}", message);

        String response = "World";
        listener.send(response.getBytes(), 0);
      }
    }
  }
  public static void main(String... args) {
    ZMQ.Context context = ZMQ.context(1);

    System.out.println("Connecting to Server ... ");

    ZMQ.Socket requester = context.socket(ZMQ.REQ);
    requester.connect("tcp://localhost:5555");

    for (int i = 0; i < 10; i++) {
      String request = "hello";
      System.out.println("Sending msg to server " + request);
      requester.send(request.getBytes(), 0);

      byte[] recv = requester.recv(0);
      System.out.println("Receiving msg from server [" + new String(recv) + "]");
    }

    requester.close();
    context.term();
  }
  /**
   * Simple method to send & receive zeromq message
   *
   * @param method
   * @param uri
   * @param json
   * @return
   */
  private String sendAndReceive(String method, String uri, String json) {

    ZMQ.Socket socket = context.socket(ZMQ.DEALER);
    socket.connect(address);

    // Handshake
    try {
      Thread.sleep(100);
    } catch (Exception e) {
      Assert.fail("Handshake failed");
    }

    StringBuilder sb = new StringBuilder(method);
    sb.append(ZMQSocket.SEPARATOR).append(uri).append(ZMQSocket.SEPARATOR);

    if (json != null) {
      sb.append(json);
    }

    String result = null;
    try {
      socket.send(sb.toString().getBytes("UTF-8"), 0);

      byte[] response = socket.recv(0);
      result = new String(response, Charset.forName("UTF-8"));

    } catch (UnsupportedEncodingException e) {
      Assert.fail("Exception when sending/receiving message");
    } finally {
      try {
        socket.close();
      } catch (Exception e2) {
        // ignore
      }
    }
    return result;
  }
Exemple #9
0
  @Override
  public void run() {
    Message message = null;
    boolean stop = false;

    byte id[], msg[];

    ZMQ.Socket frontend = null;

    int numLoaders = Integer.parseInt(Configuration.getProperty(Configuration.Key.NUM_LOADERS));

    frontend = context.socket(ZMQ.ROUTER);
    frontend.bind("tcp://*:" + Configuration.getProperty(Configuration.Key.NODE_PORT));

    ZMQ.Poller poller = context.poller(3 + numLoaders);

    poller.register(frontend, ZMQ.Poller.POLLIN);

    commandWorkerManager.init(context, poller); // Registers two backends

    loadWorkerManager.init(context, poller, numLoaders);

    searchWorkerManagerV5.init(context);

    System.out.println("ZMQ Server started....");

    try {
      do {
        poller.poll();
        if (poller.pollin(0)) {

          boolean msgFromReq = false;

          id = frontend.recv(0);

          msg = frontend.recv(0);

          // In case we got a message from a REQ socket
          if (frontend.hasReceiveMore()) {
            msg = frontend.recv(0);
            msgFromReq = true;
          }

          message = Message.readFromBytes(msg);

          switch (message.getType()) {
            case CONFIG_CLUSTER_REQ:
              Message configResponse = new Message(MessageType.CONFIG_CLUSTER_REP);
              try {
                SteffiGraph.getInstance().initializeMemberIndexes();
                searchWorkerManagerV5.initializeClientThreads();

                configResponse.setBody("OK");
              } catch (Exception x) {
                configResponse.setBody("ERROR: " + x.getMessage());
                ImgLogger.logError(x, x.getMessage());
              }
              frontend.send(id, ZMQ.SNDMORE);
              frontend.send(Message.convertMessageToBytes(configResponse), 0);
              break;
            case CLEAR:
              break;

            case LOAD_REQ:
              loadWorkerManager.sendToLoader(id, msg);
              break;
            case SEARCH_REQ:
              searchMsgCounter++;
              searchWorkerManagerV5.sendToSearchWorker((SearchReqMsg) message);
              break;
            case STOP:
              loadWorkerManager.stop();
              commandWorkerManager.stop();
              searchWorkerManagerV5.stop();

              SteffiGraph.getInstance().closeGraphClients();

              stop = true;
              break;
            case END_SEARCH:
              searchWorkerManagerV5.endSearch(((SearchEndMsg) message).getSearchId());
              break;
            default:
              commandWorkerManager.sendToCommandWorker(id, msg, msgFromReq);
              break;
          }
        }

        if (poller.pollin(1)) {
          commandWorkerManager.sentToFrontend(frontend, true);
        }

        if (poller.pollin(2)) {
          commandWorkerManager.sentToFrontend(frontend, false);
        }

        for (int i = 0; i < numLoaders; i++) {
          if (poller.pollin(i + 3)) {
            loadWorkerManager.sendToFrontend(i, frontend);
          }
        }

      } while (!stop);
    } catch (Exception x) {
      ImgLogger.logError(x, "Error on node server");
    } finally {
      frontend.close();
      context.term();

      CacheContainer.getCellCache().stop();
      CacheContainer.getCacheContainer().stop();
    }
    System.out.println("Main node server closed...");
    System.out.println(CacheContainer.getCacheContainer().getStatus());
  }