Search code examples
javagoogle-cloud-platformfuturegoogle-cloud-pubsubgoogle-api-java-client

How to publish messages to Cloud Pub/Sub via the JAVA Client using a dependency?


I have written the following method inside a maven package:

public static void publishMessage(Publisher publisher, String message) throws ExecutionException, InterruptedException, TimeoutException {
    ByteString data = ByteString.copyFromUtf8(message);
    PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
    ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
    String messageId = messageIdFuture.get(10L, TimeUnit.SECONDS);
    LOG.info("Message Published! ID: {} Message: {}", messageId, message);
}

When I call this method from a Runner main class within the maven package, I am able to publish messages to the Pub/Sub without any issues. When I load this maven project as a dependency within an SBT project and try calling this method, the execution gets timed out at the line: messageIdFuture.get(10L, TimeUnit.SECONDS); or gets stuck indefinitely if I use the statement without a timeout config.

I am building the publisher in this fashion:

public static Publisher getPublisher(String projectId, String topicId) throws IOException {
    TopicName topicName = TopicName.of(projectId, topicId);
    return Publisher.newBuilder(topicName).build();
}

I have a hunch that the issue is related to executors and thread pools, wherein the control is not flowing in a way that I intend it to.

Other Experiments: I've tried avoiding the future.get() by running publisher.publish(pubsubMessage); followed by publisher.shutdown(); as the shutdown publishes all locally queued messages. Even then, the message is published via the Runner class and the SBT application execution gets stuck indefinitely at publisher.shutdown(); which internally uses a wait() inside the messagesWaiter.waitComplete();

Please note:

  • I am using authentication via end-user credentials locally, but even in my deployed application (SBT, Play) which uses a service account, the code gets timed out.
  • I am shutting down the publisher (as mentioned in the documentation) separately after these ops (in any case that shouldn't be an issue, since it's working with the Runner class).

JAVA Client Version being used: 1.108.1

I have majorly followed the JAVA Client documentation itself. Link: https://cloud.google.com/pubsub/docs/samples/pubsub-quickstart-publisher


Solution

  • Upgrading to the latest version of the Java Client Library for Cloud Pub/Sub worked.