@@ -90,12 +90,12 @@ publishers. Add the following imports at the top of your file:
9090
9191``` java
9292import com.google.cloud.pubsub.v1.TopicAdminClient ;
93- import com.google.pubsub.v1.TopicName ;
93+ import com.google.pubsub.v1.ProjectTopicName ;
9494```
9595Then, to create the topic, use the following code:
9696
9797``` java
98- TopicName topic = TopicName . create (" test-project" , " test-topic" );
98+ ProjectTopicName topic = ProjectTopicName . of (" test-project" , " test-topic" );
9999try (TopicAdminClient topicAdminClient = TopicAdminClient . create()) {
100100 topicAdminClient. createTopic(topic);
101101}
@@ -115,13 +115,13 @@ Then, to publish messages asynchronously, use the following code:
115115``` java
116116Publisher publisher = null ;
117117try {
118- publisher = Publisher . defaultBuilder (topic). build();
118+ publisher = Publisher . newBuilder (topic). build();
119119 ByteString data = ByteString . copyFromUtf8(" my-message" );
120120 PubsubMessage pubsubMessage = PubsubMessage . newBuilder(). setData(data). build();
121121 ApiFuture<String > messageIdFuture = publisher. publish(pubsubMessage);
122122} finally {
123123 if (publisher != null ) {
124- publisher. shutdown();
124+ publisher. shutdown();
125125 }
126126}
127127```
@@ -133,14 +133,14 @@ single, specific topic. Add the following imports at the top of your file:
133133``` java
134134import com.google.cloud.pubsub.v1.SubscriptionAdminClient ;
135135import com.google.pubsub.v1.PushConfig ;
136- import com.google.pubsub.v1.SubscriptionName ;
137- import com.google.pubsub.v1.TopicName ;
136+ import com.google.pubsub.v1.ProjectSubscriptionName ;
137+ import com.google.pubsub.v1.ProjectTopicName ;
138138```
139139Then, to create the subscription, use the following code:
140140
141141``` java
142- TopicName topic = TopicName . create (" test-project" , " test-topic" );
143- SubscriptionName subscription = SubscriptionName . create (" test-project" , " test-subscription" );
142+ ProjectTopicName topic = ProjectTopicName . of (" test-project" , " test-topic" );
143+ ProjectSubscriptionName subscription = ProjectSubscriptionName . of (" test-project" , " test-subscription" );
144144
145145try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient . create()) {
146146 subscriptionAdminClient. createSubscription(subscription, topic, PushConfig . getDefaultInstance(), 0 );
@@ -157,32 +157,35 @@ import com.google.cloud.pubsub.v1.MessageReceiver;
157157import com.google.cloud.pubsub.v1.Subscriber ;
158158import com.google.common.util.concurrent.MoreExecutors ;
159159import com.google.pubsub.v1.PubsubMessage ;
160- import com.google.pubsub.v1.SubscriptionName ;
161- import com.google.pubsub.v1.TopicName ;
160+ import com.google.pubsub.v1.ProjectSubscriptionName ;
161+ import com.google.pubsub.v1.ProjectTopicName ;
162162```
163163Then, to pull messages asynchronously, use the following code:
164164
165165``` java
166+ ProjectSubscriptionName subscription = ProjectSubscriptionName . of(" test-project" , " test-subscription" );
167+
166168MessageReceiver receiver =
167- new MessageReceiver () {
168- @Override
169- public void receiveMessage (PubsubMessage message , AckReplyConsumer consumer ) {
170- System . out. println(" got message: " + message. getData(). toStringUtf8());
171- consumer. ack();
172- }
173- };
169+ new MessageReceiver () {
170+ @Override
171+ public void receiveMessage (PubsubMessage message , AckReplyConsumer consumer ) {
172+ System . out. println(" got message: " + message. getData(). toStringUtf8());
173+ consumer. ack();
174+ }
175+ };
176+
174177Subscriber subscriber = null ;
175178try {
176- subscriber = Subscriber . defaultBuilder(subscriptionName , receiver). build();
179+ subscriber = Subscriber . newBuilder(subscription , receiver). build();
177180 subscriber. addListener(
178- new Subscriber .Listener () {
179- @Override
180- public void failed (Subscriber .State from , Throwable failure ) {
181- // Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down.
182- System . err. println(failure);
183- }
184- },
185- MoreExecutors . directExecutor());
181+ new Subscriber .Listener () {
182+ @Override
183+ public void failed (Subscriber .State from , Throwable failure ) {
184+ // Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down.
185+ System . err. println(failure);
186+ }
187+ },
188+ MoreExecutors . directExecutor());
186189 subscriber. startAsync(). awaitRunning();
187190 // ...
188191} finally {
@@ -193,12 +196,7 @@ try {
193196```
194197#### Complete source code
195198
196- In
197- [ CreateTopicAndPublishMessages.java] ( ../google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java )
198- and
199- [ CreateSubscriptionAndConsumeMessages.java] ( ../google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateSubscriptionAndConsumeMessages.java )
200- we put together all the code shown above into two programs. The programs assume that you are
201- running on Compute Engine, App Engine Flexible or from your own desktop.
199+ In [ CreateTopicAndPublishMessages.java] ( ../google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateTopicAndPublishMessages.java ) and [ CreateSubscriptionAndConsumeMessages.java] ( ../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.
202200
203201Transport
204202---------
0 commit comments