use of com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class Cleanup method deleteTestSubscriptions.
private static void deleteTestSubscriptions(String projectId, String[] subscriptions) throws Exception {
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
for (String subscriptionId : subscriptions) {
try {
subscriptionAdminClient.deleteSubscription(SubscriptionName.create(projectId, subscriptionId));
System.out.println("Subscription deleted : " + subscriptionId);
} catch (Exception e) {
//do nothing catch clause
}
}
}
}
use of com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class CreateSubscriptionAndConsumeMessages method main.
public static void main(String... args) throws Exception {
TopicName topic = TopicName.create("my-project-id", "my-topic-id");
SubscriptionName subscription = SubscriptionName.create("my-project-id", "my-topic-id");
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
subscriptionAdminClient.createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 0);
}
MessageReceiver receiver = new MessageReceiver() {
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
System.out.println("Received message: " + message.getData().toStringUtf8());
consumer.ack();
}
};
Subscriber subscriber = null;
try {
subscriber = Subscriber.defaultBuilder(subscription, receiver).build();
subscriber.addListener(new Subscriber.Listener() {
@Override
public void failed(Subscriber.State from, Throwable failure) {
// Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down.
System.err.println(failure);
}
}, MoreExecutors.directExecutor());
subscriber.startAsync().awaitRunning();
Thread.sleep(60000);
} finally {
if (subscriber != null) {
subscriber.stopAsync();
}
}
}
use of com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientSnippets method getSubscriptionPolicy.
/** Example of getting a subscription policy. */
public Policy getSubscriptionPolicy(String subscriptionId) throws Exception {
// [START pubsub_get_subscription_policy]
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
Policy policy = subscriptionAdminClient.getIamPolicy(subscriptionName.toString());
if (policy == null) {
// subscription was not found
}
return policy;
}
// [END pubsub_get_subscription_policy]
}
use of com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientSnippets method listSubscriptions.
/** Example of listing subscriptions. */
public ListSubscriptionsPagedResponse listSubscriptions() throws Exception {
// [START pubsub_list_subscriptions]
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
ListSubscriptionsRequest listSubscriptionsRequest = ListSubscriptionsRequest.newBuilder().setProjectWithProjectName(ProjectName.create(projectId)).build();
ListSubscriptionsPagedResponse response = subscriptionAdminClient.listSubscriptions(listSubscriptionsRequest);
Iterable<Subscription> subscriptions = response.iterateAll();
for (Subscription subscription : subscriptions) {
// do something with the subscription
}
return response;
}
// [END pubsub_list_subscriptions]
}
use of com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient in project google-cloud-java by GoogleCloudPlatform.
the class SubscriptionAdminClientSnippets method replacePushConfig.
/** Example of replacing the push configuration of a subscription, setting the push endpoint. */
public void replacePushConfig(String subscriptionId, String endpoint) throws Exception {
// [START pubsub_update_push_configuration]
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build();
subscriptionAdminClient.modifyPushConfig(subscriptionName, pushConfig);
}
// [END pubsub_update_push_configuration]
}
Aggregations