Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update common templates, start generating README (#157)
* chore: update common templates, start generating README * chore: fix link * chore: fix libraries-bom install section
- Loading branch information
Showing
15 changed files
with
593 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
custom_content: | | ||
#### Creating a topic | ||
With Pub/Sub you can create topics. A topic is a named resource to which messages are sent by | ||
publishers. Add the following imports at the top of your file: | ||
```java | ||
import com.google.cloud.pubsub.v1.TopicAdminClient; | ||
import com.google.pubsub.v1.ProjectTopicName; | ||
``` | ||
Then, to create the topic, use the following code: | ||
```java | ||
ProjectTopicName topic = ProjectTopicName.of("test-project", "test-topic"); | ||
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { | ||
topicAdminClient.createTopic(topic); | ||
} | ||
``` | ||
#### Publishing messages | ||
With Pub/Sub you can publish messages to a topic. Add the following import at the top of your file: | ||
```java | ||
import com.google.api.core.ApiFuture; | ||
import com.google.api.core.ApiFutureCallback; | ||
import com.google.api.core.ApiFutures; | ||
import com.google.cloud.pubsub.v1.Publisher; | ||
import com.google.common.util.concurrent.MoreExecutors; | ||
import com.google.protobuf.ByteString; | ||
import com.google.pubsub.v1.PubsubMessage; | ||
``` | ||
Then, to publish messages asynchronously, use the following code: | ||
```java | ||
Publisher publisher = null; | ||
try { | ||
publisher = Publisher.newBuilder(topic).build(); | ||
ByteString data = ByteString.copyFromUtf8("my-message"); | ||
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); | ||
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage); | ||
ApiFutures.addCallback(messageIdFuture, new ApiFutureCallback<String>() { | ||
public void onSuccess(String messageId) { | ||
System.out.println("published with message id: " + messageId); | ||
} | ||
public void onFailure(Throwable t) { | ||
System.out.println("failed to publish: " + t); | ||
} | ||
}, MoreExecutors.directExecutor()); | ||
//... | ||
} finally { | ||
if (publisher != null) { | ||
publisher.shutdown(); | ||
publisher.awaitTermination(1, TimeUnit.MINUTES); | ||
} | ||
} | ||
``` | ||
#### Creating a subscription | ||
With Pub/Sub you can create subscriptions. A subscription represents the stream of messages from a | ||
single, specific topic. Add the following imports at the top of your file: | ||
```java | ||
import com.google.cloud.pubsub.v1.SubscriptionAdminClient; | ||
import com.google.pubsub.v1.PushConfig; | ||
import com.google.pubsub.v1.ProjectSubscriptionName; | ||
import com.google.pubsub.v1.ProjectTopicName; | ||
``` | ||
Then, to create the subscription, use the following code: | ||
```java | ||
ProjectTopicName topic = ProjectTopicName.of("test-project", "test-topic"); | ||
ProjectSubscriptionName subscription = ProjectSubscriptionName.of("test-project", "test-subscription"); | ||
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) { | ||
subscriptionAdminClient.createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 0); | ||
} | ||
``` | ||
#### Pulling messages | ||
With Pub/Sub you can pull messages from a subscription. Add the following imports at the top of your | ||
file: | ||
```java | ||
import com.google.cloud.pubsub.v1.AckReplyConsumer; | ||
import com.google.cloud.pubsub.v1.MessageReceiver; | ||
import com.google.cloud.pubsub.v1.Subscriber; | ||
import com.google.common.util.concurrent.MoreExecutors; | ||
import com.google.pubsub.v1.PubsubMessage; | ||
import com.google.pubsub.v1.ProjectSubscriptionName; | ||
import com.google.pubsub.v1.ProjectTopicName; | ||
``` | ||
Then, to pull messages asynchronously, use the following code: | ||
```java | ||
ProjectSubscriptionName subscription = ProjectSubscriptionName.of("test-project", "test-subscription"); | ||
MessageReceiver receiver = | ||
new MessageReceiver() { | ||
@Override | ||
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) { | ||
System.out.println("got message: " + message.getData().toStringUtf8()); | ||
consumer.ack(); | ||
} | ||
}; | ||
Subscriber subscriber = null; | ||
try { | ||
subscriber = Subscriber.newBuilder(subscription, receiver).build(); | ||
subscriber.addListener( | ||
new Subscriber.Listener() { | ||
@Override | ||
public void failed(Subscriber.State from, Throwable failure) { | ||
// Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down. | ||
System.err.println(failure); | ||
} | ||
}, | ||
MoreExecutors.directExecutor()); | ||
subscriber.startAsync().awaitRunning(); | ||
//... | ||
} finally { | ||
if (subscriber != null) { | ||
subscriber.stopAsync(); | ||
} | ||
} | ||
``` | ||
#### Complete source code | ||
In [CreateTopicAndPublishMessages.java](https://github.com/googleapis/google-cloud-java/tree/master/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java) and [CreateSubscriptionAndConsumeMessages.java](https://github.com/googleapis/google-cloud-java/tree/master/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateSubscriptionAndConsumeMessages.java) we put together all the code shown above into two programs. The programs assume that you are running on Compute Engine, App Engine Flexible or from your own desktop. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
{ | ||
"name": "pubsub", | ||
"name_pretty": "Google Cloud Pub/Sub", | ||
"name_pretty": "Cloud Pub/Sub", | ||
"api_reference": "https://cloud.google.com/pubsub/", | ||
"product_documentation": "https://cloud.google.com/pubsub/docs/", | ||
"client_documentation": "https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/pubsub/v1/package-summary.html", | ||
"client_documentation": "https://googleapis.dev/java/google-cloud-pubusb/latest/index.html", | ||
"api_description": "is designed to provide reliable, many-to-many, asynchronous messaging between applications. Publisher applications can send messages to a topic and other applications can subscribe to that topic to receive the messages. By decoupling senders and receivers, Google Cloud Pub/Sub allows developers to communicate between independently written applications.", | ||
"issue_tracker": "https://issuetracker.google.com/savedsearches/559741", | ||
"release_level": "ga", | ||
"language": "java", | ||
"repo": "googleapis/java-pubsub", | ||
"repo_short": "java-pubsub", | ||
"distribution_name": "com.google.cloud:google-cloud-pubsub", | ||
"api_id": "pubsub.googleapis.com" | ||
"api_id": "pubsub.googleapis.com", | ||
"requires_billing": true | ||
} |
Oops, something went wrong.