use of com.google.pubsub.v1.Subscription 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.pubsub.v1.Subscription 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.pubsub.v1.Subscription in project google-cloud-java by GoogleCloudPlatform.
the class SubscriberSnippets method createSubscriber.
private void createSubscriber() throws Exception {
// [START pubsub_pull]
String projectId = "my-project-id";
String subscriptionId = "my-subscription-id";
SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
// Instantiate an asynchronous message receiver
MessageReceiver receiver = new MessageReceiver() {
@Override
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
// handle incoming message, then ack/nack the received message
System.out.println("Id : " + message.getMessageId());
System.out.println("Data : " + message.getData().toStringUtf8());
consumer.ack();
}
};
Subscriber subscriber = null;
try {
// Create a subscriber for "my-subscription-id" bound to the message receiver
subscriber = Subscriber.defaultBuilder(subscriptionName, receiver).build();
subscriber.startAsync();
// ...
} finally {
// stop receiving messages
if (subscriber != null) {
subscriber.stopAsync();
}
}
// [END pubsub_pull]
}
use of com.google.pubsub.v1.Subscription 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]
}
use of com.google.pubsub.v1.Subscription 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]
}
Aggregations