Exemplo n.º 1
0
    @Override
    public boolean runCmd(String[] args) throws Exception {
      if (args.length < 3) {
        return false;
      }
      ByteString topic = ByteString.copyFromUtf8(args[1]);

      StringBuilder sb = new StringBuilder();
      for (int i = 2; i < args.length; i++) {
        sb.append(args[i]);
        if (i != args.length - 1) {
          sb.append(' ');
        }
      }
      ByteString msgBody = ByteString.copyFromUtf8(sb.toString());
      Message msg = Message.newBuilder().setBody(msgBody).build();
      try {
        publisher.publish(topic, msg);
        System.out.println("PUB DONE");
      } catch (Exception e) {
        System.err.println("PUB FAILED");
        e.printStackTrace();
      }
      return true;
    }
Exemplo n.º 2
0
 protected String getPrompt() {
   StringBuilder sb = new StringBuilder();
   sb.append("[hedwig: (").append(myRegion).append(") ").append(commandCount).append("] ");
   return sb.toString();
 }
Exemplo n.º 3
0
    @Override
    public boolean runCmd(String[] args) throws Exception {
      if (args.length < 5) {
        return false;
      }
      final long startTime = MathUtils.now();

      final ByteString topic = ByteString.copyFromUtf8(args[1]);
      final ByteString subId = ByteString.copyFromUtf8(args[2] + "-" + startTime);
      int timeoutSecs = 60;
      try {
        timeoutSecs = Integer.parseInt(args[3]);
      } catch (NumberFormatException nfe) {
      }

      StringBuilder sb = new StringBuilder();
      for (int i = 4; i < args.length; i++) {
        sb.append(args[i]);
        if (i != args.length - 1) {
          sb.append(' ');
        }
      }
      // append a timestamp tag
      ByteString msgBody = ByteString.copyFromUtf8(sb.toString() + "-" + startTime);
      final Message msg = Message.newBuilder().setBody(msgBody).build();

      boolean subscribed = false;
      boolean success = false;
      final CountDownLatch isDone = new CountDownLatch(1);
      long elapsedTime = 0L;

      System.out.println("Starting PUBSUB test ...");
      try {
        // sub the topic
        subscriber.subscribe(topic, subId, CreateOrAttach.CREATE_OR_ATTACH);
        subscribed = true;

        System.out.println(
            "Sub topic " + topic.toStringUtf8() + ", subscriber id " + subId.toStringUtf8());

        // pub topic
        publisher.publish(topic, msg);
        System.out.println(
            "Pub topic " + topic.toStringUtf8() + " : " + msg.getBody().toStringUtf8());

        // ensure subscriber first, publish next, then we start delivery to receive message
        // if start delivery first before publish, isDone may notify before wait
        subscriber.startDelivery(
            topic,
            subId,
            new MessageHandler() {

              @Override
              public void deliver(
                  ByteString thisTopic,
                  ByteString subscriberId,
                  Message message,
                  Callback<Void> callback,
                  Object context) {
                if (thisTopic.equals(topic)
                    && subscriberId.equals(subId)
                    && msg.getBody().equals(message.getBody())) {
                  System.out.println("Received message : " + message.getBody().toStringUtf8());
                  isDone.countDown();
                }
                callback.operationFinished(context, null);
              }
            });

        // wait for the message
        success = isDone.await(timeoutSecs, TimeUnit.SECONDS);
        elapsedTime = MathUtils.now() - startTime;
      } finally {
        try {
          if (subscribed) {
            subscriber.stopDelivery(topic, subId);
            subscriber.unsubscribe(topic, subId);
          }
        } finally {
          if (success) {
            System.out.println("PUBSUB SUCCESS. TIME: " + elapsedTime + " MS");
          } else {
            System.out.println("PUBSUB FAILED. ");
          }
          return success;
        }
      }
    }