Search in sources :

Example 1 with CursorClient

use of com.google.cloud.pubsublite.internal.CursorClient in project java-pubsublite-kafka by googleapis.

the class ConsumerSettings method instantiate.

public Consumer<byte[], byte[]> instantiate() throws ApiException {
    try {
        CloudRegion region = subscriptionPath().location().extractRegion();
        TopicPath topic;
        if (topicPathOverride().isPresent()) {
            topic = topicPathOverride().get();
        } else {
            try (AdminClient adminClient = AdminClient.create(AdminClientSettings.newBuilder().setRegion(region).build())) {
                Subscription subscription = adminClient.getSubscription(subscriptionPath()).get();
                topic = TopicPath.parse(subscription.getTopic());
            }
        }
        AssignerFactory assignerFactory = receiver -> {
            try {
                return AssignerSettings.newBuilder().setReceiver(receiver).setSubscriptionPath(subscriptionPath()).setServiceClient(PartitionAssignmentServiceClient.create(ServiceClients.addDefaultSettings(region, PartitionAssignmentServiceSettings.newBuilder()))).build().instantiate();
            } catch (Throwable t) {
                throw toCanonical(t).underlying;
            }
        };
        SubscriberServiceClient subscriberServiceClient = SubscriberServiceClient.create(ServiceClients.addDefaultSettings(region, SubscriberServiceSettings.newBuilder()));
        PullSubscriberFactory pullSubscriberFactory = (partition, initialSeek, resetHandler) -> {
            SubscriberFactory subscriberFactory = consumer -> {
                try {
                    return SubscriberBuilder.newBuilder().setPartition(partition).setSubscriptionPath(subscriptionPath()).setMessageConsumer(consumer).setStreamFactory(responseStream -> {
                        ApiCallContext context = getCallContext(PubsubContext.of(FRAMEWORK), RoutingMetadata.of(subscriptionPath(), partition));
                        return subscriberServiceClient.subscribeCallable().splitCall(responseStream, context);
                    }).setInitialLocation(initialSeek).setResetHandler(resetHandler).build();
                } catch (Throwable t) {
                    throw toCanonical(t).underlying;
                }
            };
            return new BlockingPullSubscriberImpl(subscriberFactory, perPartitionFlowControlSettings());
        };
        CursorServiceClient cursorServiceClient = CursorServiceClient.create(addDefaultSettings(subscriptionPath().location().extractRegion(), CursorServiceSettings.newBuilder()));
        CommitterFactory committerFactory = partition -> {
            try {
                return CommitterSettings.newBuilder().setSubscriptionPath(subscriptionPath()).setPartition(partition).setStreamFactory(responseStream -> cursorServiceClient.streamingCommitCursorCallable().splitCall(responseStream)).build().instantiate();
            } catch (Throwable t) {
                throw toCanonical(t);
            }
        };
        ConsumerFactory consumerFactory = () -> new SingleSubscriptionConsumerImpl(topic, autocommit(), pullSubscriberFactory, committerFactory);
        CursorClient cursorClient = CursorClient.create(CursorClientSettings.newBuilder().setRegion(region).build());
        TopicStatsClient topicStatsClient = TopicStatsClient.create(TopicStatsClientSettings.newBuilder().setRegion(region).build());
        SharedBehavior shared = new SharedBehavior(AdminClient.create(AdminClientSettings.newBuilder().setRegion(region).build()));
        return new PubsubLiteConsumer(subscriptionPath(), topic, shared, consumerFactory, assignerFactory, cursorClient, topicStatsClient, cursorServiceClient, subscriberServiceClient);
    } catch (Exception e) {
        throw toCanonical(e).underlying;
    }
}
Also used : SubscriberServiceClient(com.google.cloud.pubsublite.v1.SubscriberServiceClient) CursorClient(com.google.cloud.pubsublite.internal.CursorClient) RoutingMetadata(com.google.cloud.pubsublite.internal.wire.RoutingMetadata) FlowControlSettings(com.google.cloud.pubsublite.cloudpubsub.FlowControlSettings) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) Framework(com.google.cloud.pubsublite.internal.wire.PubsubContext.Framework) CloudRegion(com.google.cloud.pubsublite.CloudRegion) SubscriptionPath(com.google.cloud.pubsublite.SubscriptionPath) CommitterSettings(com.google.cloud.pubsublite.internal.wire.CommitterSettings) TopicPath(com.google.cloud.pubsublite.TopicPath) PartitionAssignmentServiceSettings(com.google.cloud.pubsublite.v1.PartitionAssignmentServiceSettings) CursorClientSettings(com.google.cloud.pubsublite.internal.CursorClientSettings) PubsubContext(com.google.cloud.pubsublite.internal.wire.PubsubContext) AdminClient(com.google.cloud.pubsublite.AdminClient) SubscriberFactory(com.google.cloud.pubsublite.internal.wire.SubscriberFactory) CursorServiceSettings(com.google.cloud.pubsublite.v1.CursorServiceSettings) Consumer(org.apache.kafka.clients.consumer.Consumer) ServiceClients(com.google.cloud.pubsublite.internal.wire.ServiceClients) AdminClientSettings(com.google.cloud.pubsublite.AdminClientSettings) SubscriberServiceSettings(com.google.cloud.pubsublite.v1.SubscriberServiceSettings) PartitionAssignmentServiceClient(com.google.cloud.pubsublite.v1.PartitionAssignmentServiceClient) TopicStatsClient(com.google.cloud.pubsublite.internal.TopicStatsClient) CursorServiceClient(com.google.cloud.pubsublite.v1.CursorServiceClient) AssignerSettings(com.google.cloud.pubsublite.internal.wire.AssignerSettings) ApiException(com.google.api.gax.rpc.ApiException) Subscription(com.google.cloud.pubsublite.proto.Subscription) ServiceClients.addDefaultSettings(com.google.cloud.pubsublite.internal.wire.ServiceClients.addDefaultSettings) BlockingPullSubscriberImpl(com.google.cloud.pubsublite.internal.BlockingPullSubscriberImpl) SubscriberBuilder(com.google.cloud.pubsublite.internal.wire.SubscriberBuilder) AssignerFactory(com.google.cloud.pubsublite.internal.wire.AssignerFactory) TopicStatsClientSettings(com.google.cloud.pubsublite.internal.TopicStatsClientSettings) AutoValue(com.google.auto.value.AutoValue) ExtractStatus.toCanonical(com.google.cloud.pubsublite.internal.ExtractStatus.toCanonical) Optional(java.util.Optional) ServiceClients.getCallContext(com.google.cloud.pubsublite.internal.wire.ServiceClients.getCallContext) CloudRegion(com.google.cloud.pubsublite.CloudRegion) TopicPath(com.google.cloud.pubsublite.TopicPath) AssignerFactory(com.google.cloud.pubsublite.internal.wire.AssignerFactory) SubscriberServiceClient(com.google.cloud.pubsublite.v1.SubscriberServiceClient) CursorServiceClient(com.google.cloud.pubsublite.v1.CursorServiceClient) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) ApiException(com.google.api.gax.rpc.ApiException) CursorClient(com.google.cloud.pubsublite.internal.CursorClient) SubscriberFactory(com.google.cloud.pubsublite.internal.wire.SubscriberFactory) BlockingPullSubscriberImpl(com.google.cloud.pubsublite.internal.BlockingPullSubscriberImpl) TopicStatsClient(com.google.cloud.pubsublite.internal.TopicStatsClient) Subscription(com.google.cloud.pubsublite.proto.Subscription) AdminClient(com.google.cloud.pubsublite.AdminClient)

Aggregations

ApiCallContext (com.google.api.gax.rpc.ApiCallContext)1 ApiException (com.google.api.gax.rpc.ApiException)1 AutoValue (com.google.auto.value.AutoValue)1 AdminClient (com.google.cloud.pubsublite.AdminClient)1 AdminClientSettings (com.google.cloud.pubsublite.AdminClientSettings)1 CloudRegion (com.google.cloud.pubsublite.CloudRegion)1 SubscriptionPath (com.google.cloud.pubsublite.SubscriptionPath)1 TopicPath (com.google.cloud.pubsublite.TopicPath)1 FlowControlSettings (com.google.cloud.pubsublite.cloudpubsub.FlowControlSettings)1 BlockingPullSubscriberImpl (com.google.cloud.pubsublite.internal.BlockingPullSubscriberImpl)1 CursorClient (com.google.cloud.pubsublite.internal.CursorClient)1 CursorClientSettings (com.google.cloud.pubsublite.internal.CursorClientSettings)1 ExtractStatus.toCanonical (com.google.cloud.pubsublite.internal.ExtractStatus.toCanonical)1 TopicStatsClient (com.google.cloud.pubsublite.internal.TopicStatsClient)1 TopicStatsClientSettings (com.google.cloud.pubsublite.internal.TopicStatsClientSettings)1 AssignerFactory (com.google.cloud.pubsublite.internal.wire.AssignerFactory)1 AssignerSettings (com.google.cloud.pubsublite.internal.wire.AssignerSettings)1 CommitterSettings (com.google.cloud.pubsublite.internal.wire.CommitterSettings)1 PubsubContext (com.google.cloud.pubsublite.internal.wire.PubsubContext)1 Framework (com.google.cloud.pubsublite.internal.wire.PubsubContext.Framework)1