logger.debug("Calling a sync publish for topic: {}, msg: {}.",
topic.toStringUtf8(), msg);
}
PubSubData pubSubData = new PubSubData(topic, msg, null, OperationType.PUBLISH, null, null, null);
synchronized (pubSubData) {
PubSubCallback pubSubCallback = new PubSubCallback(pubSubData);
asyncPublishWithResponseImpl(topic, msg, pubSubCallback, null);
try {
while (!pubSubData.isDone)
pubSubData.wait();
} catch (InterruptedException e) {
throw new ServiceDownException("Interrupted Exception while waiting for async publish call");
}
// Check from the PubSubCallback if it was successful or not.
if (!pubSubCallback.getIsCallSuccessful()) {
// See what the exception was that was thrown when the operation
// failed.
PubSubException failureException = pubSubCallback.getFailureException();
if (failureException == null) {
// This should not happen as the operation failed but a null
// PubSubException was passed. Log a warning message but
// throw a generic ServiceDownException.
logger.error("Sync Publish operation failed but no PubSubException was passed!");
throw new ServiceDownException("Server ack response to publish request is not successful");
}
// For the expected exceptions that could occur, just rethrow
// them.
else if (failureException instanceof CouldNotConnectException) {
throw (CouldNotConnectException) failureException;
} else if (failureException instanceof ServiceDownException) {
throw (ServiceDownException) failureException;
} else {
// For other types of PubSubExceptions, just throw a generic
// ServiceDownException but log a warning message.
logger.error("Unexpected exception type when a sync publish operation failed: ",
failureException);
throw new ServiceDownException("Server ack response to publish request is not successful");
}
}
ResponseBody respBody = pubSubCallback.getResponseBody();
if (null == respBody) {
return null;
}
return respBody.hasPublishResponse() ? respBody.getPublishResponse() : null;
}