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;
}
}
Aggregations