use of org.apache.pulsar.client.api.Schema in project incubator-pulsar by apache.
the class PulsarClientImpl method patternTopicSubscribeAsync.
private <T> CompletableFuture<Consumer<T>> patternTopicSubscribeAsync(ConsumerConfigurationData<T> conf, Schema<T> schema) {
String regex = conf.getTopicsPattern().pattern();
TopicName destination = TopicName.get(regex);
NamespaceName namespaceName = destination.getNamespaceObject();
CompletableFuture<Consumer<T>> consumerSubscribedFuture = new CompletableFuture<>();
lookup.getTopicsUnderNamespace(namespaceName).thenAccept(topics -> {
if (log.isDebugEnabled()) {
log.debug("Get topics under namespace {}, topics.size: {}", namespaceName.toString(), topics.size());
topics.forEach(topicName -> log.debug("Get topics under namespace {}, topic: {}", namespaceName.toString(), topicName));
}
List<String> topicsList = topicsPatternFilter(topics, conf.getTopicsPattern());
conf.getTopicNames().addAll(topicsList);
ConsumerBase<T> consumer = new PatternTopicsConsumerImpl<>(conf.getTopicsPattern(), PulsarClientImpl.this, conf, externalExecutorProvider.getExecutor(), consumerSubscribedFuture, schema);
synchronized (consumers) {
consumers.put(consumer, Boolean.TRUE);
}
}).exceptionally(ex -> {
log.warn("[{}] Failed to get topics under namespace", namespaceName);
consumerSubscribedFuture.completeExceptionally(ex);
return null;
});
return consumerSubscribedFuture;
}
use of org.apache.pulsar.client.api.Schema in project incubator-pulsar by apache.
the class TopicsConsumerImpl method subscribeAsync.
// subscribe one more given topic
public CompletableFuture<Void> subscribeAsync(String topicName) {
if (!topicNameValid(topicName)) {
return FutureUtil.failedFuture(new PulsarClientException.AlreadyClosedException("Topic name not valid"));
}
if (getState() == State.Closing || getState() == State.Closed) {
return FutureUtil.failedFuture(new PulsarClientException.AlreadyClosedException("Topics Consumer was already closed"));
}
CompletableFuture<Void> subscribeResult = new CompletableFuture<>();
final AtomicInteger partitionNumber = new AtomicInteger(0);
client.getPartitionedTopicMetadata(topicName).thenAccept(metadata -> {
if (log.isDebugEnabled()) {
log.debug("Received topic {} metadata.partitions: {}", topicName, metadata.partitions);
}
List<CompletableFuture<Consumer<T>>> futureList;
if (metadata.partitions > 1) {
this.topics.putIfAbsent(topicName, metadata.partitions);
numberTopicPartitions.addAndGet(metadata.partitions);
partitionNumber.addAndGet(metadata.partitions);
futureList = IntStream.range(0, partitionNumber.get()).mapToObj(partitionIndex -> {
String partitionName = TopicName.get(topicName).getPartition(partitionIndex).toString();
CompletableFuture<Consumer<T>> subFuture = new CompletableFuture<>();
ConsumerImpl<T> newConsumer = new ConsumerImpl<>(client, partitionName, internalConfig, client.externalExecutorProvider().getExecutor(), partitionIndex, subFuture, schema);
consumers.putIfAbsent(newConsumer.getTopic(), newConsumer);
return subFuture;
}).collect(Collectors.toList());
} else {
this.topics.putIfAbsent(topicName, 1);
numberTopicPartitions.incrementAndGet();
partitionNumber.incrementAndGet();
CompletableFuture<Consumer<T>> subFuture = new CompletableFuture<>();
ConsumerImpl<T> newConsumer = new ConsumerImpl<>(client, topicName, internalConfig, client.externalExecutorProvider().getExecutor(), 0, subFuture, schema);
consumers.putIfAbsent(newConsumer.getTopic(), newConsumer);
futureList = Collections.singletonList(subFuture);
}
FutureUtil.waitForAll(futureList).thenAccept(finalFuture -> {
try {
if (numberTopicPartitions.get() > maxReceiverQueueSize) {
setMaxReceiverQueueSize(numberTopicPartitions.get());
}
int numTopics = this.topics.values().stream().mapToInt(Integer::intValue).sum();
checkState(numberTopicPartitions.get() == numTopics, "numberTopicPartitions " + numberTopicPartitions.get() + " not equals expected: " + numTopics);
// We have successfully created new consumers, so we can start receiving messages for them
startReceivingMessages(consumers.values().stream().filter(consumer1 -> {
String consumerTopicName = consumer1.getTopic();
if (TopicName.get(consumerTopicName).getPartitionedTopicName().equals(TopicName.get(topicName).getPartitionedTopicName().toString())) {
return true;
} else {
return false;
}
}).collect(Collectors.toList()));
subscribeResult.complete(null);
log.info("[{}] [{}] Success subscribe new topic {} in topics consumer, numberTopicPartitions {}", topic, subscription, topicName, numberTopicPartitions.get());
if (this.namespaceName == null) {
this.namespaceName = TopicName.get(topicName).getNamespaceObject();
}
return;
} catch (PulsarClientException e) {
handleSubscribeOneTopicError(topicName, e);
subscribeResult.completeExceptionally(e);
}
}).exceptionally(ex -> {
handleSubscribeOneTopicError(topicName, ex);
subscribeResult.completeExceptionally(ex);
return null;
});
}).exceptionally(ex1 -> {
log.warn("[{}] Failed to get partitioned topic metadata: {}", topicName, ex1.getMessage());
subscribeResult.completeExceptionally(ex1);
return null;
});
return subscribeResult;
}
Aggregations