0.2.4
Features
Pub/Sub
gcloud-java-pubsub, a new client library to interact with Google Cloud Pub/Sub, is released and is in alpha. See the docs for more information.
gcloud-java-pubsubuses gRPC as transport layer, which is not (yet) supported by App Engine Standard.gcloud-java-pubsubwill work on App Engine Flexible.
See PubSubExample for a complete example or API Documentation forgcloud-java-pubsubjavadoc.
The following snippet shows how to create a Pub/Sub topic and asynchronously publish messages to it. See CreateTopicAndPublishMessages.java for the full source code.
try (PubSub pubsub = PubSubOptions.defaultInstance().service()) {
Topic topic = pubsub.create(TopicInfo.of("test-topic"));
Message message1 = Message.of("First message");
Message message2 = Message.of("Second message");
topic.publishAsync(message1, message2);
}The following snippet, instead, shows how to create a Pub/Sub pull subscription and asynchronously pull messages from it. See CreateSubscriptionAndPullMessages.java for the full source code.
try (PubSub pubsub = PubSubOptions.defaultInstance().service()) {
Subscription subscription =
pubsub.create(SubscriptionInfo.of("test-topic", "test-subscription"));
MessageProcessor callback = new MessageProcessor() {
@Override
public void process(Message message) throws Exception {
System.out.printf("Received message \"%s\"%n", message.payloadAsString());
}
};
// Create a message consumer and pull messages (for 60 seconds)
try (MessageConsumer consumer = subscription.pullAsync(callback)) {
Thread.sleep(60_000);
}
}