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);
}