Skip to content

Commit b283ea1

Browse files
committed
Merge branch 'master' into subscriber-snippet
2 parents 3717f02 + e61ca31 commit b283ea1

9 files changed

Lines changed: 123 additions & 39 deletions

File tree

google-cloud-core/src/main/java/com/google/cloud/GrpcServiceOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ protected ChannelProvider getChannelProvider() {
313313
InstantiatingChannelProvider.Builder builder = InstantiatingChannelProvider.newBuilder()
314314
.setServiceAddress(hostAndPort.getHostText())
315315
.setPort(hostAndPort.getPort())
316-
.setClientLibHeader(getLibraryName(), firstNonNull(getLibraryVersion(), ""));
316+
.setClientLibHeader(getGoogApiClientLibName(), firstNonNull(getLibraryVersion(), ""));
317317
Credentials scopedCredentials = getScopedCredentials();
318318
if (scopedCredentials != null && scopedCredentials != NoCredentials.getInstance()) {
319319
builder.setCredentialsProvider(FixedCredentialsProvider.create(scopedCredentials));

google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public abstract class ServiceOptions<ServiceT extends Service<OptionsT>, Service
7474
private static final String MANIFEST_VERSION_KEY = "Implementation-Version";
7575
private static final String ARTIFACT_ID = "google-cloud-core";
7676
private static final String LIBRARY_NAME = "gcloud-java";
77+
private static final String X_GOOGLE_CLIENT_HEADER_NAME = "gccl";
7778
private static final String LIBRARY_VERSION = defaultLibraryVersion();
7879
private static final String APPLICATION_NAME =
7980
LIBRARY_VERSION == null ? LIBRARY_NAME : LIBRARY_NAME + "/" + LIBRARY_VERSION;
@@ -601,6 +602,13 @@ public String getLibraryName() {
601602
return LIBRARY_NAME;
602603
}
603604

605+
/**
606+
* Returns the library's name used by x-goog-api-client header as a string.
607+
*/
608+
public String getGoogApiClientLibName() {
609+
return X_GOOGLE_CLIENT_HEADER_NAME;
610+
}
611+
604612
/**
605613
* Returns the library's version as a string.
606614
*/
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2017 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.examples.pubsub;
18+
19+
import com.google.api.gax.core.RpcFuture;
20+
import com.google.api.gax.core.RpcFutureCallback;
21+
import com.google.cloud.pubsub.spi.v1.Publisher;
22+
import com.google.protobuf.ByteString;
23+
import com.google.pubsub.v1.PubsubMessage;
24+
import com.google.pubsub.v1.TopicName;
25+
26+
public class PublisherSnippets {
27+
private final Publisher publisher;
28+
29+
public PublisherSnippets(Publisher publisher) {
30+
this.publisher = publisher;
31+
}
32+
33+
/**
34+
* Example of publishing a message.
35+
*/
36+
// [TARGET publish(PubsubMessage)]
37+
// [VARIABLE "my_message"]
38+
public void publish(String message) {
39+
// [START publish]
40+
ByteString data = ByteString.copyFromUtf8(message);
41+
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
42+
RpcFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
43+
messageIdFuture.addCallback(new RpcFutureCallback<String>() {
44+
public void onSuccess(String messageId) {
45+
System.out.println("published with message id: " + messageId);
46+
}
47+
48+
public void onFailure(Throwable t) {
49+
System.out.println("failed to publish: " + t);
50+
}
51+
});
52+
// [END publish]
53+
}
54+
55+
/**
56+
* Example of creating a {@code Publisher}.
57+
*/
58+
// [TARGET newBuilder(TopicName)]
59+
// [VARIABLE "my_project"]
60+
// [VARIABLE "my_topic"]
61+
public static void newBuilder(String projectName, String topicName) throws Exception {
62+
// [START newBuilder]
63+
TopicName topic = TopicName.create(projectName, topicName);
64+
Publisher publisher = Publisher.newBuilder(topic).build();
65+
try {
66+
// ...
67+
} finally {
68+
// When finished with the publisher, make sure to shutdown to free up resources.
69+
publisher.shutdown();
70+
}
71+
// [END newBuilder]
72+
}
73+
}

google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/testing/LocalPubSubHelper.java renamed to google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/testing/LocalPubSubHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.cloud.pubsub.testing;
17+
package com.google.cloud.pubsub.deprecated.testing;
1818

1919
import com.google.cloud.NoCredentials;
2020
import com.google.cloud.RetryParams;

google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/testing/package-info.java renamed to google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/testing/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@
3333
* @see <a href="https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/TESTING.md#testing-code-that-uses-pubsub">
3434
* Google Cloud Java tools for testing</a>
3535
*/
36-
package com.google.cloud.pubsub.testing;
36+
package com.google.cloud.pubsub.deprecated.testing;

google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/Publisher.java

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -76,37 +76,6 @@
7676
*
7777
* <p>If no credentials are provided, the {@link Publisher} will use application default credentials
7878
* through {@link GoogleCredentials#getApplicationDefault}.
79-
*
80-
* <p>For example, a {@link Publisher} can be constructed and used to publish a list of messages as
81-
* follows:
82-
*
83-
* <pre><code>
84-
* Publisher publisher =
85-
* Publisher.newBuilder(MY_TOPIC)
86-
* .setMaxBundleDuration(new Duration(10 * 1000))
87-
* .build();
88-
* List&lt;RpcFuture&lt;String&gt;&gt; results = new ArrayList&lt;&gt;();
89-
*
90-
* for (PubsubMessage messages : messagesToPublish) {
91-
* results.add(publisher.publish(message));
92-
* }
93-
*
94-
* Futures.addCallback(
95-
* Futures.allAsList(results),
96-
* new FutureCallback&lt;List&lt;String&gt;&gt;() {
97-
* &#64;Override
98-
* public void onSuccess(List&lt;String&gt; messageIds) {
99-
* // ... process the acknowledgement of publish ...
100-
* }
101-
* &#64;Override
102-
* public void onFailure(Throwable t) {
103-
* // .. handle the failure ...
104-
* }
105-
* });
106-
*
107-
* // Ensure all the outstanding messages have been published before shutting down your process.
108-
* publisher.shutdown();
109-
* </code></pre>
11079
*/
11180
public class Publisher {
11281
private static final Logger logger = Logger.getLogger(Publisher.class.getName());
@@ -208,6 +177,23 @@ public TopicName getTopicName() {
208177
* future might immediately fail with a {@link FlowController.FlowControlException} or block the
209178
* current thread until there are more resources available to publish.
210179
*
180+
* <p>Example of publishing a message.
181+
* <pre> {@code
182+
* String message = "my_message";
183+
* ByteString data = ByteString.copyFromUtf8(message);
184+
* PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
185+
* RpcFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
186+
* messageIdFuture.addCallback(new RpcFutureCallback<String>() {
187+
* public void onSuccess(String messageId) {
188+
* System.out.println("published with message id: " + messageId);
189+
* }
190+
*
191+
* public void onFailure(Throwable t) {
192+
* System.out.println("failed to publish: " + t);
193+
* }
194+
* });
195+
* }</pre>
196+
*
211197
* @param message the message to publish.
212198
* @return the message ID wrapped in a future.
213199
*/
@@ -329,7 +315,7 @@ public V apply(X input) {
329315
return callback.apply(input);
330316
}
331317
}));
332-
}
318+
}
333319
}
334320

335321
private void setupDurationBasedPublishAlarm() {
@@ -563,7 +549,24 @@ interface LongRandom {
563549
long nextLong(long least, long bound);
564550
}
565551

566-
/** Constructs a new {@link Builder} using the given topic. */
552+
/**
553+
* Constructs a new {@link Builder} using the given topic.
554+
*
555+
* <p>Example of creating a {@code Publisher}.
556+
* <pre> {@code
557+
* String projectName = "my_project";
558+
* String topicName = "my_topic";
559+
* TopicName topic = TopicName.create(projectName, topicName);
560+
* Publisher publisher = Publisher.newBuilder(topic).build();
561+
* try {
562+
* // ...
563+
* } finally {
564+
* // When finished with the publisher, make sure to shutdown to free up resources.
565+
* publisher.shutdown();
566+
* }
567+
* }</pre>
568+
*
569+
*/
567570
public static Builder newBuilder(TopicName topicName) {
568571
return new Builder(topicName);
569572
}

google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/deprecated/LocalSystemTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.google.cloud.pubsub.deprecated;
1818

19-
import com.google.cloud.pubsub.testing.LocalPubSubHelper;
19+
import com.google.cloud.pubsub.deprecated.testing.LocalPubSubHelper;
2020

2121
import org.joda.time.Duration;
2222
import org.junit.AfterClass;

google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/testing/LocalPubSubHelperTest.java renamed to google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/deprecated/testing/LocalPubSubHelperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.cloud.pubsub.testing;
17+
package com.google.cloud.pubsub.deprecated.testing;
1818

1919
import static org.junit.Assert.assertNotNull;
2020
import static org.junit.Assert.assertNull;

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<github.global.server>github</github.global.server>
9393
<google.auth.version>0.6.0</google.auth.version>
9494
<grpc.version>1.0.3</grpc.version>
95-
<gax.version>0.1.1</gax.version>
95+
<gax.version>0.1.2</gax.version>
9696
<generatedProto.version>0.1.5</generatedProto.version>
9797
<core.version>0.9.3-alpha-SNAPSHOT</core.version>
9898
<beta.version>0.9.3-beta-SNAPSHOT</beta.version>

0 commit comments

Comments
 (0)