Search in sources :

Example 46 with SubscriptionName

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

the class SubscriptionAdminClientTest method getSubscriptionTest.

@Test
@SuppressWarnings("all")
public void getSubscriptionTest() {
    SubscriptionName name = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
    TopicNameOneof topic = TopicNameOneof.from(TopicName.create("[PROJECT]", "[TOPIC]"));
    int ackDeadlineSeconds = 2135351438;
    boolean retainAckedMessages = false;
    Subscription expectedResponse = Subscription.newBuilder().setNameWithSubscriptionName(name).setTopicWithTopicNameOneof(topic).setAckDeadlineSeconds(ackDeadlineSeconds).setRetainAckedMessages(retainAckedMessages).build();
    mockSubscriber.addResponse(expectedResponse);
    SubscriptionName subscription = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
    Subscription actualResponse = client.getSubscription(subscription);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockSubscriber.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GetSubscriptionRequest actualRequest = (GetSubscriptionRequest) actualRequests.get(0);
    Assert.assertEquals(subscription, actualRequest.getSubscriptionAsSubscriptionName());
}
Also used : TopicNameOneof(com.google.pubsub.v1.TopicNameOneof) GetSubscriptionRequest(com.google.pubsub.v1.GetSubscriptionRequest) SubscriptionName(com.google.pubsub.v1.SubscriptionName) Subscription(com.google.pubsub.v1.Subscription) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Example 47 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName 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());
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) SubscriptionAdminClient(com.google.cloud.pubsub.v1.SubscriptionAdminClient) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) Subscription(com.google.pubsub.v1.Subscription) ApiException(com.google.api.gax.rpc.ApiException)

Example 48 with SubscriptionName

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

the class SubscriberExample method main.

/**
 * Receive messages over a subscription.
 */
public static void main(String... args) throws Exception {
    // set subscriber id, eg. my-sub
    String subscriptionId = args[0];
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(PROJECT_ID, subscriptionId);
    Subscriber subscriber = null;
    try {
        // create a subscriber bound to the asynchronous message receiver
        subscriber = Subscriber.newBuilder(subscriptionName, new MessageReceiverExample()).build();
        subscriber.startAsync().awaitRunning();
        // Continue to listen to messages
        while (true) {
            PubsubMessage message = messages.take();
            System.out.println("Message Id: " + message.getMessageId());
            System.out.println("Data: " + message.getData().toStringUtf8());
        }
    } finally {
        if (subscriber != null) {
            subscriber.stopAsync();
        }
    }
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) Subscriber(com.google.cloud.pubsub.v1.Subscriber) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Example 49 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName in project flink by apache.

the class PubsubHelper method createSubscription.

public void createSubscription(String subscriptionProject, String subscription, String topicProject, String topic) throws IOException {
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.newBuilder().setProject(subscriptionProject).setSubscription(subscription).build();
    deleteSubscription(subscriptionName);
    TopicName topicName = TopicName.of(topicProject, topic);
    PushConfig pushConfig = PushConfig.getDefaultInstance();
    LOG.info("CreateSubscription {}", subscriptionName);
    getSubscriptionAdminClient().createSubscription(subscriptionName, topicName, pushConfig, 1).isInitialized();
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) PushConfig(com.google.pubsub.v1.PushConfig) TopicName(com.google.pubsub.v1.TopicName)

Example 50 with SubscriptionName

use of com.google.pubsub.v1.SubscriptionName in project flink by apache.

the class PubsubHelper method pullMessages.

// 
// Mostly copied from the example on https://cloud.google.com/pubsub/docs/pull
// Licensed under the Apache 2.0 License to "Google LLC" from
// https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberSnippets.java.
// 
public List<ReceivedMessage> pullMessages(String projectId, String subscriptionId, int maxNumberOfMessages) throws Exception {
    SubscriberStubSettings subscriberStubSettings = SubscriberStubSettings.newBuilder().setTransportChannelProvider(channelProvider).setCredentialsProvider(EmulatorCredentialsProvider.create()).build();
    try (SubscriberStub subscriber = GrpcSubscriberStub.create(subscriberStubSettings)) {
        String subscriptionName = ProjectSubscriptionName.format(projectId, subscriptionId);
        PullRequest pullRequest = PullRequest.newBuilder().setMaxMessages(maxNumberOfMessages).setSubscription(subscriptionName).build();
        List<ReceivedMessage> receivedMessages = subscriber.pullCallable().call(pullRequest).getReceivedMessagesList();
        acknowledgeIds(subscriber, subscriptionName, receivedMessages);
        return receivedMessages;
    }
}
Also used : PullRequest(com.google.pubsub.v1.PullRequest) SubscriberStub(com.google.cloud.pubsub.v1.stub.SubscriberStub) GrpcSubscriberStub(com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub) ReceivedMessage(com.google.pubsub.v1.ReceivedMessage) SubscriberStubSettings(com.google.cloud.pubsub.v1.stub.SubscriberStubSettings)

Aggregations

SubscriptionName (com.google.pubsub.v1.SubscriptionName)31 Test (org.junit.Test)27 Subscription (com.google.pubsub.v1.Subscription)13 TopicName (com.google.pubsub.v1.TopicName)11 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)10 SubscriptionAdminClient (com.google.cloud.pubsub.spi.v1.SubscriptionAdminClient)9 ByteString (com.google.protobuf.ByteString)9 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)9 PushConfig (com.google.pubsub.v1.PushConfig)9 StatusRuntimeException (io.grpc.StatusRuntimeException)9 ApiException (com.google.api.gax.grpc.ApiException)8 PubsubMessage (com.google.pubsub.v1.PubsubMessage)7 Subscriber (com.google.cloud.pubsub.v1.Subscriber)5 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)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