Search in sources :

Example 1 with SubscriptionPath

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath in project beam by apache.

the class FhirIOReadIT method setup.

@Before
public void setup() throws Exception {
    healthcareDataset = String.format(HEALTHCARE_DATASET_TEMPLATE, project);
    if (client == null) {
        this.client = new HttpHealthcareApiClient();
    }
    pubsub = PubsubGrpcClient.FACTORY.newClient(null, null, pipelineOptions);
    TopicPath topicPath = PubsubClient.topicPathFromPath(pubsubTopic);
    pubsub.createTopic(topicPath);
    SubscriptionPath subscriptionPath = PubsubClient.subscriptionPathFromPath(pubsubSubscription);
    pubsub.createSubscription(topicPath, subscriptionPath, 60);
    client.createFhirStore(healthcareDataset, fhirStoreName, version, pubsubTopic);
    // Execute bundles to trigger FHIR notificiations to input topic
    FhirIOTestUtil.executeFhirBundles(client, healthcareDataset + "/fhirStores/" + fhirStoreName, FhirIOTestUtil.BUNDLES.get(version));
}
Also used : TopicPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath) SubscriptionPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath) Before(org.junit.Before)

Example 2 with SubscriptionPath

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath in project beam by apache.

the class PubsubHelper method subscriptionExists.

/**
 * Does subscription corresponding to short name exist?
 */
public boolean subscriptionExists(String shortTopic, String shortSubscription) throws IOException {
    TopicPath topic = PubsubClient.topicPathFromName(project, shortTopic);
    SubscriptionPath subscription = PubsubClient.subscriptionPathFromName(project, shortSubscription);
    return pubsubClient.listSubscriptions(PubsubClient.projectPathFromId(project), topic).stream().anyMatch(subscription::equals);
}
Also used : TopicPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath) SubscriptionPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath)

Example 3 with SubscriptionPath

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath in project beam by apache.

the class TestPubsubSignal method waitForStart.

/**
 * Future that waits for a start signal for {@code duration}.
 *
 * <p>This future must be created before running the pipeline. A subscription must exist prior to
 * the start signal being published, which occurs immediately upon pipeline startup.
 */
public Supplier<Void> waitForStart(Duration duration) throws IOException {
    SubscriptionPath startSubscriptionPath = PubsubClient.subscriptionPathFromName(pipelineOptions.getProject(), "start-subscription-" + String.valueOf(ThreadLocalRandom.current().nextLong()));
    subscriptionAdmin.createSubscription(startSubscriptionPath.getPath(), startTopicPath.getPath(), PushConfig.getDefaultInstance(), (int) duration.getStandardSeconds());
    return Suppliers.memoize(() -> {
        try {
            String result = pollForResultForDuration(startSubscriptionPath, duration);
            checkState(START_SIGNAL_MESSAGE.equals(result));
            return null;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                subscriptionAdmin.deleteSubscription(startSubscriptionPath.getPath());
            } catch (ApiException e) {
                LOG.error(String.format("Leaked PubSub subscription '%s'", startSubscriptionPath));
            }
        }
    });
}
Also used : SubscriptionPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath) IOException(java.io.IOException) ApiException(com.google.api.gax.rpc.ApiException)

Example 4 with SubscriptionPath

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath in project beam by apache.

the class TestPubsubSignal method waitForSuccess.

/**
 * Wait for a success signal for {@code duration}.
 */
public void waitForSuccess(Duration duration) throws IOException {
    SubscriptionPath resultSubscriptionPath = PubsubClient.subscriptionPathFromName(pipelineOptions.getProject(), "result-subscription-" + String.valueOf(ThreadLocalRandom.current().nextLong()));
    subscriptionAdmin.createSubscription(resultSubscriptionPath.getPath(), resultTopicPath.getPath(), PushConfig.getDefaultInstance(), (int) duration.getStandardSeconds());
    String result = pollForResultForDuration(resultSubscriptionPath, duration);
    try {
        subscriptionAdmin.deleteSubscription(resultSubscriptionPath.getPath());
    } catch (ApiException e) {
        LOG.error(String.format("Leaked PubSub subscription '%s'", resultSubscriptionPath));
    }
    if (!RESULT_SUCCESS_MESSAGE.equals(result)) {
        throw new AssertionError(result);
    }
}
Also used : SubscriptionPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath) ApiException(com.google.api.gax.rpc.ApiException)

Example 5 with SubscriptionPath

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath in project beam by apache.

the class PubsubUnboundedSource method createRandomSubscription.

private SubscriptionPath createRandomSubscription(PipelineOptions options) {
    TopicPath topicPath = topic.get();
    ProjectPath projectPath;
    if (project != null) {
        projectPath = project.get();
    } else {
        String projectId = options.as(GcpOptions.class).getProject();
        checkState(projectId != null, "Cannot create subscription to topic %s because pipeline option 'project' not specified", topicPath);
        projectPath = PubsubClient.projectPathFromId(options.as(GcpOptions.class).getProject());
    }
    try {
        try (PubsubClient pubsubClient = pubsubFactory.newClient(timestampAttribute, idAttribute, options.as(PubsubOptions.class))) {
            SubscriptionPath subscriptionPath = pubsubClient.createRandomSubscription(projectPath, topicPath, DEAULT_ACK_TIMEOUT_SEC);
            LOG.warn("Created subscription {} to topic {}." + " Note this subscription WILL NOT be deleted when the pipeline terminates", subscriptionPath, topic);
            return subscriptionPath;
        }
    } catch (Exception e) {
        throw new RuntimeException(String.format("Failed to create subscription to topic %s on project %s: %s", topicPath, projectPath, e.getMessage()), e);
    }
}
Also used : TopicPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath) GcpOptions(org.apache.beam.sdk.extensions.gcp.options.GcpOptions) SubscriptionPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath) ProjectPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.ProjectPath) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

Aggregations

SubscriptionPath (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath)13 TopicPath (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath)10 IOException (java.io.IOException)5 Test (org.junit.Test)3 ApiException (com.google.api.gax.rpc.ApiException)2 ByteString (com.google.protobuf.ByteString)2 PipelineOptions (org.apache.beam.sdk.options.PipelineOptions)2 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 ApiFuture (com.google.api.core.ApiFuture)1 ApiFutures (com.google.api.core.ApiFutures)1 GrpcTransportChannel (com.google.api.gax.grpc.GrpcTransportChannel)1 FixedTransportChannelProvider (com.google.api.gax.rpc.FixedTransportChannelProvider)1 TransportChannelProvider (com.google.api.gax.rpc.TransportChannelProvider)1 Subscriptions (com.google.api.services.pubsub.Pubsub.Projects.Subscriptions)1 ListSubscriptionsResponse (com.google.api.services.pubsub.model.ListSubscriptionsResponse)1 AckReplyConsumer (com.google.cloud.pubsub.v1.AckReplyConsumer)1 MessageReceiver (com.google.cloud.pubsub.v1.MessageReceiver)1 Publisher (com.google.cloud.pubsub.v1.Publisher)1 Subscriber (com.google.cloud.pubsub.v1.Subscriber)1 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)1