use of com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientSnippets method replaceSubscriptionPolicy.
/** Example of replacing a subscription policy. */
public Policy replaceSubscriptionPolicy(String subscriptionId) throws Exception {
// [START pubsub_set_subscription_policy]
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
Policy policy = subscriptionAdminClient.getIamPolicy(subscriptionName.toString());
// Create a role => members binding
Binding binding = Binding.newBuilder().setRole(Role.viewer().toString()).addMembers(Identity.allAuthenticatedUsers().toString()).build();
//Update policy
Policy updatedPolicy = policy.toBuilder().addBindings(binding).build();
updatedPolicy = subscriptionAdminClient.setIamPolicy(subscriptionName.toString(), updatedPolicy);
return updatedPolicy;
}
// [END pubsub_set_subscription_policy]
}
use of com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient in project java-docs-samples by GoogleCloudPlatform.
the class Samples method createOccurrenceSubscription.
/**
* Creates and returns a pubsub subscription object listening to the occurrence topic
* @param subscriptionId the identifier you want to associate with the subscription
* @param projectId the project's unique identifier
* @throws Exception on errors with the subscription client
*/
public static Subscription createOccurrenceSubscription(String subscriptionId, String projectId) throws Exception {
String topicId = "resource-notes-occurrences-v1alpha1";
try (SubscriptionAdminClient client = SubscriptionAdminClient.create()) {
PushConfig config = PushConfig.getDefaultInstance();
ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
Subscription sub = client.createSubscription(subscriptionName, topicName, config, 0);
return sub;
}
}
use of com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient in project java-docs-samples by GoogleCloudPlatform.
the class SamplesTests method testPubSub.
@Test
public void testPubSub() throws Exception {
int newCount;
int tries;
String subscriptionId = "drydockOccurrences";
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(PROJECT_ID, subscriptionId);
Samples.createOccurrenceSubscription(subscriptionId, PROJECT_ID);
Subscriber subscriber = null;
Samples.MessageReceiverExample receiver = new Samples.MessageReceiverExample();
try {
subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
subscriber.startAsync().awaitRunning();
// sleep so any messages in the queue can go through and be counted before we start the test
sleep(SLEEP_TIME);
// set the initial state of our counter
int startVal = receiver.messageCount + 1;
// now, we can test adding 3 more occurrences
int endVal = startVal + 3;
for (int i = startVal; i <= endVal; i++) {
Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
System.out.println("CREATED: " + o.getName());
tries = 0;
do {
newCount = receiver.messageCount;
sleep(SLEEP_TIME);
tries += 1;
} while (newCount != i && tries < TRY_LIMIT);
System.out.println(receiver.messageCount + " : " + i);
assertEquals(i, receiver.messageCount);
Samples.deleteOccurrence(o.getName());
}
} catch (Exception e) {
fail("exception thrown");
} finally {
if (subscriber != null) {
subscriber.stopAsync();
}
// delete subscription now that we're done with it
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
subscriptionAdminClient.deleteSubscription(subscriptionName);
}
}
}
use of com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientSnippets method createSubscriptionWithPushEndpoint.
/** Example of creating a subscription with a push endpoint. */
public Subscription createSubscriptionWithPushEndpoint(String topicId, String subscriptionId, String endpoint) throws Exception {
// [START pubsub_create_push_subscription]
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
TopicName topicName = TopicName.create(projectId, topicId);
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
// eg. endpoint = "https://my-test-project.appspot.com/push"
PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build();
// acknowledgement deadline in seconds for the message received over the push endpoint
int ackDeadlineInSeconds = 10;
Subscription subscription = subscriptionAdminClient.createSubscription(subscriptionName, topicName, pushConfig, ackDeadlineInSeconds);
return subscription;
}
// [END pubsub_create_push_subscription]
}
use of com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientSnippets method getSubscription.
/** Example of getting a subscription. */
public Subscription getSubscription(String subscriptionId) throws Exception {
// [START pubsub_get_subscription]
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
Subscription subscription = subscriptionAdminClient.getSubscription(subscriptionName);
return subscription;
}
// [END pubsub_get_subscription]
}
Aggregations