Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PublisherClient/SubscriberClient snippets #1663

Merged
merged 11 commits into from
Mar 6, 2017
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

/*
* EDITING INSTRUCTIONS
* This file is referenced in Subscriber's javadoc. Any change to this file should be reflected in
* PubSub's javadoc.
* This file is referenced in MessageReceiver's javadoc.
* Any change to this file should be reflected in MessageReceiver's javadoc.
*/

package com.google.cloud.examples.pubsub.snippets;
Expand All @@ -28,6 +28,8 @@
import com.google.pubsub.v1.PubsubMessage;
import java.util.concurrent.BlockingQueue;

/** This class contains snippets for the {@link MessageReceiver} interface. */

public class MessageReceiverSnippets {
private final BlockingQueue<PubsubMessage> blockingQueue;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/*
* Copyright 2017 Google Inc. All Rights Reserved.

This comment was marked as spam.

*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.examples.pubsub.snippets;

import com.google.cloud.Identity;
import com.google.cloud.Role;
import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse;
import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse;
import com.google.cloud.pubsub.spi.v1.PublisherClient;
import com.google.iam.v1.Binding;
import com.google.iam.v1.Policy;
import com.google.iam.v1.TestIamPermissionsResponse;
import com.google.pubsub.v1.ListTopicSubscriptionsRequest;
import com.google.pubsub.v1.ListTopicsRequest;
import com.google.pubsub.v1.ProjectName;
import com.google.pubsub.v1.Topic;
import com.google.pubsub.v1.TopicName;
import java.util.LinkedList;
import java.util.List;

/** This class contains a number of snippets for the {@link PublisherClient} interface. */
public class PublisherClientSnippets {

private final String projectId;

public PublisherClientSnippets(String projectId) {
this.projectId = projectId;
}

/** Example of creating a topic. */
public Topic createTopic(String topicName) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
// [START createTopic]
TopicName formattedTopicName = TopicName.create(projectId, topicName);
Topic topic = publisherClient.createTopic(formattedTopicName);
// [END createTopic]
return topic;
}
}

/** Example of listing topics, specifying the page size. */
public ListTopicsPagedResponse listTopics() throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
// [START listTopics]
ListTopicsRequest listTopicsRequest =
ListTopicsRequest.newBuilder()
.setProjectWithProjectName(ProjectName.create(projectId))

This comment was marked as spam.

.setPageSize(100)

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

.build();
ListTopicsPagedResponse response =
publisherClient.listTopics(listTopicsRequest);
Iterable<Topic> topics = response.iterateAllElements();
for (Topic topic : topics) {
// do something with the topic
}
// [END listTopics]
return response;
}
}

/** Example of listing topics, specifying the page size and a page token. */
public ListTopicsPagedResponse listTopicsWithPageToken(String pageToken) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
// [START listTopicsWithPageToken]
ListTopicsRequest listTopicsRequest =
ListTopicsRequest.newBuilder()
.setProjectWithProjectName(ProjectName.create(projectId))
.setPageSize(100)
.setPageToken(pageToken)
.build();
ListTopicsPagedResponse response =
publisherClient.listTopics(listTopicsRequest);
Iterable<Topic> topics = response.iterateAllElements();
for (Topic topic : topics) {
// do something with the topic
}
// [END listTopicsWithPageToken]
return response;
}
}

/** Example of listing topics for a subscription, specifying the page size. */
public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicName) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
TopicName formattedTopicName = TopicName.create(projectId, topicName);
// [START listTopicSubscriptions]
ListTopicSubscriptionsRequest request =
ListTopicSubscriptionsRequest.newBuilder()
.setTopicWithTopicName(formattedTopicName)
.setPageSize(100)
.build();
ListTopicSubscriptionsPagedResponse response =
publisherClient.listTopicSubscriptions(request);
Iterable<String> subscriptions = response.iterateAllElements();
for (String subscription : subscriptions) {

This comment was marked as spam.

This comment was marked as spam.

// do something with the subscription name
}
// [END listTopicSubscriptions]
return response;
}
}

/** Example of listing topics for a subscription, specifying the page size and page token */
public ListTopicSubscriptionsPagedResponse listTopicSubscriptionsWithPageToken(
String topicName, String pageToken) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
TopicName formattedTopicName = TopicName.create(projectId, topicName);
// [START listTopicSubscriptionsWithPageToken]
ListTopicSubscriptionsRequest request =
ListTopicSubscriptionsRequest.newBuilder()
.setTopicWithTopicName(formattedTopicName)
.setPageSize(100)
.setPageToken(pageToken)
.build();
ListTopicSubscriptionsPagedResponse response =
publisherClient.listTopicSubscriptions(request);
Iterable<String> subscriptions = response.iterateAllElements();
for (String subscription : subscriptions) {
// do something with the subscription name
}

This comment was marked as spam.

This comment was marked as spam.

// [END listTopicSubscriptionsWithPageToken]
return response;
}
}

/** Example of deleting a topic. */
public TopicName deleteTopic(String topicName) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
TopicName formattedTopicName = TopicName.create(projectId, topicName);
// [START deleteTopic]
publisherClient.deleteTopic(formattedTopicName);

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

// [END deleteTopic]
return formattedTopicName;
}
}

/** Example of getting a topic policy. */
public Policy getTopicPolicy(String name) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
TopicName topicName = TopicName.create(projectId, name);
// [START getTopicPolicy]
Policy policy = publisherClient.getIamPolicy(topicName.toString());
if (policy == null) {
// topic iam policy was not found
}
// [END getTopicPolicy]
return policy;
}
}

/** Example of replacing a topic policy. */
public Policy replaceTopicPolicy(String name) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
String topicName = TopicName.create(projectId, name).toString();
// [START replaceTopicPolicy]
Policy policy = publisherClient.getIamPolicy(topicName);
// add role -> members binding
Binding binding =
Binding.newBuilder()
.setRole(Role.viewer().toString())
.addMembers(Identity.allAuthenticatedUsers().toString())
.build();
// create updated policy
Policy updatedPolicy = Policy.newBuilder(policy).addBindings(binding).build();
updatedPolicy = publisherClient.setIamPolicy(topicName, updatedPolicy);
// [END replaceTopicPolicy]
return updatedPolicy;
}
}

/** Example of testing whether the caller has the provided permissions on a topic.
* Only viewer, editor or admin/owner can view results of pubsub.topics.get */
public TestIamPermissionsResponse testTopicPermissions(String topicName) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
TopicName formattedTopicName = TopicName.create(projectId, topicName);
// [START testTopicPermissions]
List<String> permissions = new LinkedList<>();
permissions.add("pubsub.topics.get");
TestIamPermissionsResponse testedPermissions =
publisherClient.testIamPermissions(formattedTopicName.toString(), permissions);
// [END testTopicPermissions]
return testedPermissions;
}
}

/** Example of getting a topic. */
public Topic getTopic(String topicName) throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
// [START getTopic]
TopicName formattedTopicName = TopicName.create(projectId, topicName);
Topic topic = publisherClient.getTopic(formattedTopicName);
// [END createTopic]
return topic;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
* limitations under the License.
*/

package com.google.cloud.examples.pubsub;
/*
* EDITING INSTRUCTIONS
* This file is referenced in Publisher's javadoc. Any change to this file should be reflected in
* Publisher's javadoc.
*/

package com.google.cloud.examples.pubsub.snippets;

import com.google.api.gax.core.RpcFuture;
import com.google.api.gax.core.RpcFutureCallback;
Expand All @@ -23,38 +29,36 @@
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.TopicName;

/** This class contains snippets for the {@link Publisher} interface. */
public class PublisherSnippets {
private final Publisher publisher;

public PublisherSnippets(Publisher publisher) {
this.publisher = publisher;
}

/**
* Example of publishing a message.
*/
/** Example of publishing a message. */
// [TARGET publish(PubsubMessage)]
// [VARIABLE "my_message"]
public void publish(String message) {
// [START publish]
ByteString data = ByteString.copyFromUtf8(message);
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
RpcFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
messageIdFuture.addCallback(new RpcFutureCallback<String>() {
public void onSuccess(String messageId) {
System.out.println("published with message id: " + messageId);
}
messageIdFuture.addCallback(
new RpcFutureCallback<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);
}
});
public void onFailure(Throwable t) {
System.out.println("failed to publish: " + t);
}
});
// [END publish]
}

/**
* Example of creating a {@code Publisher}.
*/
/** Example of creating a {@code Publisher}. */
// [TARGET newBuilder(TopicName)]
// [VARIABLE "my_project"]
// [VARIABLE "my_topic"]
Expand Down
Loading