4040import com .google .common .collect .ImmutableMap ;
4141import com .google .common .util .concurrent .FutureCallback ;
4242import com .google .common .util .concurrent .Futures ;
43- import com .google .pubsub .v1 .ProjectTopicName ;
4443import com .google .pubsub .v1 .PublishRequest ;
4544import com .google .pubsub .v1 .PublishResponse ;
4645import com .google .pubsub .v1 .PublisherGrpc ;
4746import com .google .pubsub .v1 .PublisherGrpc .PublisherFutureStub ;
4847import com .google .pubsub .v1 .PubsubMessage ;
48+ import com .google .pubsub .v1 .TopicName ;
49+ import com .google .pubsub .v1 .TopicNames ;
4950import io .grpc .CallCredentials ;
5051import io .grpc .Channel ;
5152import io .grpc .Status ;
5253import io .grpc .auth .MoreCallCredentials ;
53- import org .threeten .bp .Duration ;
54-
55- import javax .annotation .Nullable ;
5654import java .io .IOException ;
5755import java .util .ArrayList ;
5856import java .util .Iterator ;
6866import java .util .concurrent .locks .ReentrantLock ;
6967import java .util .logging .Level ;
7068import java .util .logging .Logger ;
69+ import javax .annotation .Nullable ;
70+ import org .threeten .bp .Duration ;
7171
7272/**
7373 * A Cloud Pub/Sub <a href="https://cloud.google.com/pubsub/docs/publisher">publisher</a>, that is
9090public class Publisher {
9191 private static final Logger logger = Logger .getLogger (Publisher .class .getName ());
9292
93- private final ProjectTopicName topicName ;
94- private final String cachedTopicNameString ;
93+ private final String topicName ;
9594
9695 private final BatchingSettings batchingSettings ;
9796 private final RetrySettings retrySettings ;
@@ -124,7 +123,6 @@ public static long getApiMaxRequestBytes() {
124123
125124 private Publisher (Builder builder ) throws IOException {
126125 topicName = builder .topicName ;
127- cachedTopicNameString = topicName .toString ();
128126
129127 this .batchingSettings = builder .batchingSettings ;
130128 this .retrySettings = builder .retrySettings ;
@@ -167,7 +165,12 @@ private Publisher(Builder builder) throws IOException {
167165 }
168166
169167 /** Topic which the publisher publishes to. */
170- public ProjectTopicName getTopicName () {
168+ public TopicName getTopicName () {
169+ return TopicNames .parse (topicName );
170+ }
171+
172+ /** Topic which the publisher publishes to. */
173+ public String getTopicNameString () {
171174 return topicName ;
172175 }
173176
@@ -312,7 +315,7 @@ private void publishAllOutstanding() {
312315
313316 private void publishOutstandingBatch (final OutstandingBatch outstandingBatch ) {
314317 PublishRequest .Builder publishRequest = PublishRequest .newBuilder ();
315- publishRequest .setTopic (cachedTopicNameString );
318+ publishRequest .setTopic (topicName );
316319 for (OutstandingPublish outstandingPublish : outstandingBatch .outstandingPublishes ) {
317320 publishRequest .addMessages (outstandingPublish .message );
318321 }
@@ -497,6 +500,7 @@ interface LongRandom {
497500 * Constructs a new {@link Builder} using the given topic.
498501 *
499502 * <p>Example of creating a {@code Publisher}.
503+ *
500504 * <pre>{@code
501505 * String projectName = "my_project";
502506 * String topicName = "my_topic";
@@ -509,9 +513,28 @@ interface LongRandom {
509513 * publisher.shutdown();
510514 * }
511515 * }</pre>
516+ */
517+ public static Builder newBuilder (TopicName topicName ) {
518+ return newBuilder (topicName .toString ());
519+ }
520+
521+ /**
522+ * Constructs a new {@link Builder} using the given topic.
523+ *
524+ * <p>Example of creating a {@code Publisher}.
512525 *
526+ * <pre>{@code
527+ * String topic = "projects/my_project/topics/my_topic";
528+ * Publisher publisher = Publisher.newBuilder(topic).build();
529+ * try {
530+ * // ...
531+ * } finally {
532+ * // When finished with the publisher, make sure to shutdown to free up resources.
533+ * publisher.shutdown();
534+ * }
535+ * }</pre>
513536 */
514- public static Builder newBuilder (ProjectTopicName topicName ) {
537+ public static Builder newBuilder (String topicName ) {
515538 return new Builder (topicName );
516539 }
517540
@@ -556,7 +579,7 @@ public long nextLong(long least, long bound) {
556579 .setExecutorThreadCount (THREADS_PER_CPU * Runtime .getRuntime ().availableProcessors ())
557580 .build ();
558581
559- ProjectTopicName topicName ;
582+ String topicName ;
560583
561584 // Batching options
562585 BatchingSettings batchingSettings = DEFAULT_BATCHING_SETTINGS ;
@@ -574,7 +597,7 @@ public long nextLong(long least, long bound) {
574597 CredentialsProvider credentialsProvider =
575598 TopicAdminSettings .defaultCredentialsProviderBuilder ().build ();
576599
577- private Builder (ProjectTopicName topic ) {
600+ private Builder (String topic ) {
578601 this .topicName = Preconditions .checkNotNull (topic );
579602 }
580603
0 commit comments