|
| 1 | +custom_content: | |
| 2 | + #### Creating a topic |
| 3 | +
|
| 4 | + With Pub/Sub you can create topics. A topic is a named resource to which messages are sent by |
| 5 | + publishers. Add the following imports at the top of your file: |
| 6 | +
|
| 7 | + ```java |
| 8 | + import com.google.cloud.pubsub.v1.TopicAdminClient; |
| 9 | + import com.google.pubsub.v1.ProjectTopicName; |
| 10 | + ``` |
| 11 | + Then, to create the topic, use the following code: |
| 12 | +
|
| 13 | + ```java |
| 14 | + ProjectTopicName topic = ProjectTopicName.of("test-project", "test-topic"); |
| 15 | + try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { |
| 16 | + topicAdminClient.createTopic(topic); |
| 17 | + } |
| 18 | + ``` |
| 19 | +
|
| 20 | + #### Publishing messages |
| 21 | +
|
| 22 | + With Pub/Sub you can publish messages to a topic. Add the following import at the top of your file: |
| 23 | +
|
| 24 | + ```java |
| 25 | + import com.google.api.core.ApiFuture; |
| 26 | + import com.google.api.core.ApiFutureCallback; |
| 27 | + import com.google.api.core.ApiFutures; |
| 28 | + import com.google.cloud.pubsub.v1.Publisher; |
| 29 | + import com.google.common.util.concurrent.MoreExecutors; |
| 30 | + import com.google.protobuf.ByteString; |
| 31 | + import com.google.pubsub.v1.PubsubMessage; |
| 32 | + ``` |
| 33 | + Then, to publish messages asynchronously, use the following code: |
| 34 | +
|
| 35 | + ```java |
| 36 | + Publisher publisher = null; |
| 37 | + try { |
| 38 | + publisher = Publisher.newBuilder(topic).build(); |
| 39 | + ByteString data = ByteString.copyFromUtf8("my-message"); |
| 40 | + PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); |
| 41 | + ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage); |
| 42 | + ApiFutures.addCallback(messageIdFuture, new ApiFutureCallback<String>() { |
| 43 | + public void onSuccess(String messageId) { |
| 44 | + System.out.println("published with message id: " + messageId); |
| 45 | + } |
| 46 | +
|
| 47 | + public void onFailure(Throwable t) { |
| 48 | + System.out.println("failed to publish: " + t); |
| 49 | + } |
| 50 | + }, MoreExecutors.directExecutor()); |
| 51 | + //... |
| 52 | + } finally { |
| 53 | + if (publisher != null) { |
| 54 | + publisher.shutdown(); |
| 55 | + publisher.awaitTermination(1, TimeUnit.MINUTES); |
| 56 | + } |
| 57 | + } |
| 58 | + ``` |
| 59 | +
|
| 60 | + #### Creating a subscription |
| 61 | +
|
| 62 | + With Pub/Sub you can create subscriptions. A subscription represents the stream of messages from a |
| 63 | + single, specific topic. Add the following imports at the top of your file: |
| 64 | +
|
| 65 | + ```java |
| 66 | + import com.google.cloud.pubsub.v1.SubscriptionAdminClient; |
| 67 | + import com.google.pubsub.v1.PushConfig; |
| 68 | + import com.google.pubsub.v1.ProjectSubscriptionName; |
| 69 | + import com.google.pubsub.v1.ProjectTopicName; |
| 70 | + ``` |
| 71 | + Then, to create the subscription, use the following code: |
| 72 | +
|
| 73 | + ```java |
| 74 | + ProjectTopicName topic = ProjectTopicName.of("test-project", "test-topic"); |
| 75 | + ProjectSubscriptionName subscription = ProjectSubscriptionName.of("test-project", "test-subscription"); |
| 76 | +
|
| 77 | + try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) { |
| 78 | + subscriptionAdminClient.createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 0); |
| 79 | + } |
| 80 | + ``` |
| 81 | +
|
| 82 | + #### Pulling messages |
| 83 | +
|
| 84 | + With Pub/Sub you can pull messages from a subscription. Add the following imports at the top of your |
| 85 | + file: |
| 86 | +
|
| 87 | + ```java |
| 88 | + import com.google.cloud.pubsub.v1.AckReplyConsumer; |
| 89 | + import com.google.cloud.pubsub.v1.MessageReceiver; |
| 90 | + import com.google.cloud.pubsub.v1.Subscriber; |
| 91 | + import com.google.common.util.concurrent.MoreExecutors; |
| 92 | + import com.google.pubsub.v1.PubsubMessage; |
| 93 | + import com.google.pubsub.v1.ProjectSubscriptionName; |
| 94 | + import com.google.pubsub.v1.ProjectTopicName; |
| 95 | + ``` |
| 96 | + Then, to pull messages asynchronously, use the following code: |
| 97 | +
|
| 98 | + ```java |
| 99 | + ProjectSubscriptionName subscription = ProjectSubscriptionName.of("test-project", "test-subscription"); |
| 100 | +
|
| 101 | + MessageReceiver receiver = |
| 102 | + new MessageReceiver() { |
| 103 | + @Override |
| 104 | + public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) { |
| 105 | + System.out.println("got message: " + message.getData().toStringUtf8()); |
| 106 | + consumer.ack(); |
| 107 | + } |
| 108 | + }; |
| 109 | +
|
| 110 | + Subscriber subscriber = null; |
| 111 | + try { |
| 112 | + subscriber = Subscriber.newBuilder(subscription, receiver).build(); |
| 113 | + subscriber.addListener( |
| 114 | + new Subscriber.Listener() { |
| 115 | + @Override |
| 116 | + public void failed(Subscriber.State from, Throwable failure) { |
| 117 | + // Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down. |
| 118 | + System.err.println(failure); |
| 119 | + } |
| 120 | + }, |
| 121 | + MoreExecutors.directExecutor()); |
| 122 | + subscriber.startAsync().awaitRunning(); |
| 123 | + //... |
| 124 | + } finally { |
| 125 | + if (subscriber != null) { |
| 126 | + subscriber.stopAsync(); |
| 127 | + } |
| 128 | + } |
| 129 | + ``` |
| 130 | + #### Complete source code |
| 131 | +
|
| 132 | + 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. |
0 commit comments