use of com.google.cloud.pubsub.v1.SubscriptionAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientSnippets method deleteSubscription.
/** Example of deleting a subscription. */
public SubscriptionName deleteSubscription(String subscriptionId) throws Exception {
// [START pubsub_delete_subscription]
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
subscriptionAdminClient.deleteSubscription(subscriptionName);
return subscriptionName;
}
// [END pubsub_delete_subscription]
}
use of com.google.cloud.pubsub.v1.SubscriptionAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientSnippets method createSubscription.
/** Example of creating a pull subscription for a topic. */
public Subscription createSubscription(String topicId, String subscriptionId) throws Exception {
// [START pubsub_create_pull_subscription]
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
// eg. projectId = "my-test-project", topicId = "my-test-topic"
TopicName topicName = TopicName.create(projectId, topicId);
// eg. subscriptionId = "my-test-subscription"
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
// create a pull subscription with default acknowledgement deadline
Subscription subscription = subscriptionAdminClient.createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
return subscription;
}
// [END pubsub_create_pull_subscription]
}
use of com.google.cloud.pubsub.v1.SubscriptionAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class ITPubSubSnippets method testPublisherSubscriber.
@Test
public void testPublisherSubscriber() throws Exception {
TopicName topicName = TopicName.create(ServiceOptions.getDefaultProjectId(), formatForTest("test-topic"));
SubscriptionName subscriptionName = SubscriptionName.create(ServiceOptions.getDefaultProjectId(), formatForTest("test-subscription"));
try (TopicAdminClient publisherClient = TopicAdminClient.create();
SubscriptionAdminClient subscriberClient = SubscriptionAdminClient.create()) {
publisherClient.createTopic(topicName);
subscriberClient.createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
testPublisherSubscriberHelper(topicName, subscriptionName);
subscriberClient.deleteSubscription(subscriptionName);
publisherClient.deleteTopic(topicName);
}
}
use of com.google.cloud.pubsub.v1.SubscriptionAdminClient in project java-docs-samples by GoogleCloudPlatform.
the class CreatePullSubscriptionExample method main.
/**
* Create a pull subscription.
*
* @param args topic subscriptionId
* @throws Exception exception thrown if operation is unsuccessful
*/
public static void main(String... args) throws Exception {
// Your Google Cloud Platform project ID
String projectId = ServiceOptions.getDefaultProjectId();
// Your topic ID, eg. "my-topic"
String topicId = args[0];
// Your subscription ID eg. "my-sub"
String subscriptionId = args[1];
ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
// Create a new subscription
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
// create a pull subscription with default acknowledgement deadline (= 10 seconds)
Subscription subscription = subscriptionAdminClient.createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
} catch (ApiException e) {
// example : code = ALREADY_EXISTS(409) implies subscription already exists
System.out.print(e.getStatusCode().getCode());
System.out.print(e.isRetryable());
}
System.out.printf("Subscription %s:%s created.\n", subscriptionName.getProject(), subscriptionName.getSubscription());
}
Aggregations