Search in sources :

Example 26 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName in project google-cloud-java by GoogleCloudPlatform.

the class PubsubBenchWrapperImpl method recv.

public void recv(PubsubRecv request, StreamObserver<EmptyResponse> responseObserver) {
    System.out.println("recv has been called");
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of("some-project", request.getSubName());
    Subscriber subscriber = null;
    try {
        InstantiatingExecutorProvider executorProvider = InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(1).build();
        subscriber = Subscriber.newBuilder(subscriptionName, new SimpleReceiver()).setExecutorProvider(executorProvider).build();
        subscriber.startAsync().awaitRunning();
        // Allow the subscriber to run indefinitely unless an unrecoverable error occurs.
        subscriber.awaitTerminated();
    } catch (IllegalStateException e) {
        System.out.println("Subscriber unexpectedly stopped: " + e);
    }
    EmptyResponse reply = EmptyResponse.newBuilder().build();
    responseObserver.onNext(reply);
    responseObserver.onCompleted();
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) Subscriber(com.google.cloud.pubsub.v1.Subscriber) InstantiatingExecutorProvider(com.google.api.gax.core.InstantiatingExecutorProvider)

Example 27 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName 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]
}
Also used : PushConfig(com.google.pubsub.v1.PushConfig) SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient) SubscriptionName(com.google.pubsub.v1.SubscriptionName) Subscription(com.google.pubsub.v1.Subscription) TopicName(com.google.pubsub.v1.TopicName)

Example 28 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName 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]
}
Also used : MessageReceiver(com.google.cloud.pubsub.spi.v1.MessageReceiver) Subscriber(com.google.cloud.pubsub.spi.v1.Subscriber) SubscriptionName(com.google.pubsub.v1.SubscriptionName) AckReplyConsumer(com.google.cloud.pubsub.spi.v1.AckReplyConsumer) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Example 29 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName 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]
}
Also used : SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient) SubscriptionName(com.google.pubsub.v1.SubscriptionName) Subscription(com.google.pubsub.v1.Subscription)

Example 30 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName 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]
}
Also used : SubscriptionAdminClient(com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient) SubscriptionName(com.google.pubsub.v1.SubscriptionName)

Aggregations

SubscriptionName (com.google.pubsub.v1.SubscriptionName)31 Test (org.junit.Test)24 TopicName (com.google.pubsub.v1.TopicName)11 Subscription (com.google.pubsub.v1.Subscription)10 SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)9 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)9 StatusRuntimeException (io.grpc.StatusRuntimeException)9 ApiException (com.google.api.gax.grpc.ApiException)8 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)8 PushConfig (com.google.pubsub.v1.PushConfig)8 ByteString (com.google.protobuf.ByteString)7 Subscriber (com.google.cloud.pubsub.v1.Subscriber)5 PubsubMessage (com.google.pubsub.v1.PubsubMessage)5 Empty (com.google.protobuf.Empty)4 ArrayList (java.util.ArrayList)4 AckReplyConsumer (com.google.cloud.pubsub.spi.v1.AckReplyConsumer)3 MessageReceiver (com.google.cloud.pubsub.spi.v1.MessageReceiver)3 Subscriber (com.google.cloud.pubsub.spi.v1.Subscriber)3 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)3 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)3