use of org.apache.pulsar.client.api.KeySharedPolicy in project pulsar by yahoo.
the class ConsumerBuilderImpl method subscribeAsync.
@Override
public CompletableFuture<Consumer<T>> subscribeAsync() {
if (conf.getTopicNames().isEmpty() && conf.getTopicsPattern() == null) {
return FutureUtil.failedFuture(new InvalidConfigurationException("Topic name must be set on the consumer builder"));
}
if (StringUtils.isBlank(conf.getSubscriptionName())) {
return FutureUtil.failedFuture(new InvalidConfigurationException("Subscription name must be set on the consumer builder"));
}
if (conf.getKeySharedPolicy() != null && conf.getSubscriptionType() != SubscriptionType.Key_Shared) {
return FutureUtil.failedFuture(new InvalidConfigurationException("KeySharedPolicy must set with KeyShared subscription"));
}
CompletableFuture<Void> applyDLQConfig;
if (conf.isRetryEnable() && conf.getTopicNames().size() > 0) {
TopicName topicFirst = TopicName.get(conf.getTopicNames().iterator().next());
// Issue 9327: do compatibility check in case of the default retry and dead letter topic name changed
String oldRetryLetterTopic = topicFirst.getNamespace() + "/" + conf.getSubscriptionName() + RetryMessageUtil.RETRY_GROUP_TOPIC_SUFFIX;
String oldDeadLetterTopic = topicFirst.getNamespace() + "/" + conf.getSubscriptionName() + RetryMessageUtil.DLQ_GROUP_TOPIC_SUFFIX;
DeadLetterPolicy deadLetterPolicy = conf.getDeadLetterPolicy();
if (deadLetterPolicy == null || StringUtils.isBlank(deadLetterPolicy.getRetryLetterTopic()) || StringUtils.isBlank(deadLetterPolicy.getDeadLetterTopic())) {
CompletableFuture<PartitionedTopicMetadata> retryLetterTopicMetadata = client.getPartitionedTopicMetadata(oldRetryLetterTopic);
CompletableFuture<PartitionedTopicMetadata> deadLetterTopicMetadata = client.getPartitionedTopicMetadata(oldDeadLetterTopic);
applyDLQConfig = CompletableFuture.allOf(retryLetterTopicMetadata, deadLetterTopicMetadata).thenAccept(__ -> {
String retryLetterTopic = topicFirst + "-" + conf.getSubscriptionName() + RetryMessageUtil.RETRY_GROUP_TOPIC_SUFFIX;
String deadLetterTopic = topicFirst + "-" + conf.getSubscriptionName() + RetryMessageUtil.DLQ_GROUP_TOPIC_SUFFIX;
if (retryLetterTopicMetadata.join().partitions > 0) {
retryLetterTopic = oldRetryLetterTopic;
}
if (deadLetterTopicMetadata.join().partitions > 0) {
deadLetterTopic = oldDeadLetterTopic;
}
if (deadLetterPolicy == null) {
conf.setDeadLetterPolicy(DeadLetterPolicy.builder().maxRedeliverCount(RetryMessageUtil.MAX_RECONSUMETIMES).retryLetterTopic(retryLetterTopic).deadLetterTopic(deadLetterTopic).build());
} else {
if (StringUtils.isBlank(deadLetterPolicy.getRetryLetterTopic())) {
conf.getDeadLetterPolicy().setRetryLetterTopic(retryLetterTopic);
}
if (StringUtils.isBlank(deadLetterPolicy.getDeadLetterTopic())) {
conf.getDeadLetterPolicy().setDeadLetterTopic(deadLetterTopic);
}
}
conf.getTopicNames().add(conf.getDeadLetterPolicy().getRetryLetterTopic());
});
} else {
conf.getTopicNames().add(conf.getDeadLetterPolicy().getRetryLetterTopic());
applyDLQConfig = CompletableFuture.completedFuture(null);
}
} else {
applyDLQConfig = CompletableFuture.completedFuture(null);
}
return applyDLQConfig.thenCompose(__ -> {
if (interceptorList == null || interceptorList.size() == 0) {
return client.subscribeAsync(conf, schema, null);
} else {
return client.subscribeAsync(conf, schema, new ConsumerInterceptors<>(interceptorList));
}
});
}
use of org.apache.pulsar.client.api.KeySharedPolicy in project flink by apache.
the class PulsarPartitionSplitReaderBase method createPulsarConsumer.
/**
* Create a specified {@link Consumer} by the given topic partition.
*/
protected Consumer<byte[]> createPulsarConsumer(TopicPartition partition) {
ConsumerBuilder<byte[]> consumerBuilder = createConsumerBuilder(pulsarClient, Schema.BYTES, sourceConfiguration);
consumerBuilder.topic(partition.getFullTopicName());
// Add KeySharedPolicy for Key_Shared subscription.
if (sourceConfiguration.getSubscriptionType() == SubscriptionType.Key_Shared) {
KeySharedPolicy policy = KeySharedPolicy.stickyHashRange().ranges(partition.getPulsarRange());
consumerBuilder.keySharedPolicy(policy);
}
// Create the consumer configuration by using common utils.
return sneakyClient(consumerBuilder::subscribe);
}
use of org.apache.pulsar.client.api.KeySharedPolicy in project incubator-pulsar by apache.
the class ConsumerBuilderImpl method subscribeAsync.
@Override
public CompletableFuture<Consumer<T>> subscribeAsync() {
if (conf.getTopicNames().isEmpty() && conf.getTopicsPattern() == null) {
return FutureUtil.failedFuture(new InvalidConfigurationException("Topic name must be set on the consumer builder"));
}
if (StringUtils.isBlank(conf.getSubscriptionName())) {
return FutureUtil.failedFuture(new InvalidConfigurationException("Subscription name must be set on the consumer builder"));
}
if (conf.getKeySharedPolicy() != null && conf.getSubscriptionType() != SubscriptionType.Key_Shared) {
return FutureUtil.failedFuture(new InvalidConfigurationException("KeySharedPolicy must set with KeyShared subscription"));
}
CompletableFuture<Void> applyDLQConfig;
if (conf.isRetryEnable() && conf.getTopicNames().size() > 0) {
TopicName topicFirst = TopicName.get(conf.getTopicNames().iterator().next());
// Issue 9327: do compatibility check in case of the default retry and dead letter topic name changed
String oldRetryLetterTopic = topicFirst.getNamespace() + "/" + conf.getSubscriptionName() + RetryMessageUtil.RETRY_GROUP_TOPIC_SUFFIX;
String oldDeadLetterTopic = topicFirst.getNamespace() + "/" + conf.getSubscriptionName() + RetryMessageUtil.DLQ_GROUP_TOPIC_SUFFIX;
DeadLetterPolicy deadLetterPolicy = conf.getDeadLetterPolicy();
if (deadLetterPolicy == null || StringUtils.isBlank(deadLetterPolicy.getRetryLetterTopic()) || StringUtils.isBlank(deadLetterPolicy.getDeadLetterTopic())) {
CompletableFuture<PartitionedTopicMetadata> retryLetterTopicMetadata = client.getPartitionedTopicMetadata(oldRetryLetterTopic);
CompletableFuture<PartitionedTopicMetadata> deadLetterTopicMetadata = client.getPartitionedTopicMetadata(oldDeadLetterTopic);
applyDLQConfig = CompletableFuture.allOf(retryLetterTopicMetadata, deadLetterTopicMetadata).thenAccept(__ -> {
String retryLetterTopic = topicFirst + "-" + conf.getSubscriptionName() + RetryMessageUtil.RETRY_GROUP_TOPIC_SUFFIX;
String deadLetterTopic = topicFirst + "-" + conf.getSubscriptionName() + RetryMessageUtil.DLQ_GROUP_TOPIC_SUFFIX;
if (retryLetterTopicMetadata.join().partitions > 0) {
retryLetterTopic = oldRetryLetterTopic;
}
if (deadLetterTopicMetadata.join().partitions > 0) {
deadLetterTopic = oldDeadLetterTopic;
}
if (deadLetterPolicy == null) {
conf.setDeadLetterPolicy(DeadLetterPolicy.builder().maxRedeliverCount(RetryMessageUtil.MAX_RECONSUMETIMES).retryLetterTopic(retryLetterTopic).deadLetterTopic(deadLetterTopic).build());
} else {
if (StringUtils.isBlank(deadLetterPolicy.getRetryLetterTopic())) {
conf.getDeadLetterPolicy().setRetryLetterTopic(retryLetterTopic);
}
if (StringUtils.isBlank(deadLetterPolicy.getDeadLetterTopic())) {
conf.getDeadLetterPolicy().setDeadLetterTopic(deadLetterTopic);
}
}
conf.getTopicNames().add(conf.getDeadLetterPolicy().getRetryLetterTopic());
});
} else {
conf.getTopicNames().add(conf.getDeadLetterPolicy().getRetryLetterTopic());
applyDLQConfig = CompletableFuture.completedFuture(null);
}
} else {
applyDLQConfig = CompletableFuture.completedFuture(null);
}
return applyDLQConfig.thenCompose(__ -> {
if (interceptorList == null || interceptorList.size() == 0) {
return client.subscribeAsync(conf, schema, null);
} else {
return client.subscribeAsync(conf, schema, new ConsumerInterceptors<>(interceptorList));
}
});
}
use of org.apache.pulsar.client.api.KeySharedPolicy in project flink by splunk.
the class PulsarPartitionSplitReaderBase method createPulsarConsumer.
/**
* Create a specified {@link Consumer} by the given topic partition.
*/
protected Consumer<byte[]> createPulsarConsumer(TopicPartition partition) {
ConsumerBuilder<byte[]> consumerBuilder = createConsumerBuilder(pulsarClient, Schema.BYTES, sourceConfiguration);
consumerBuilder.topic(partition.getFullTopicName());
// Add KeySharedPolicy for Key_Shared subscription.
if (sourceConfiguration.getSubscriptionType() == SubscriptionType.Key_Shared) {
KeySharedPolicy policy = KeySharedPolicy.stickyHashRange().ranges(partition.getPulsarRange());
consumerBuilder.keySharedPolicy(policy);
}
// Create the consumer configuration by using common utils.
return sneakyClient(consumerBuilder::subscribe);
}
use of org.apache.pulsar.client.api.KeySharedPolicy in project pulsar by apache.
the class ConsumerBuilderImpl method subscribeAsync.
@Override
public CompletableFuture<Consumer<T>> subscribeAsync() {
if (conf.getTopicNames().isEmpty() && conf.getTopicsPattern() == null) {
return FutureUtil.failedFuture(new InvalidConfigurationException("Topic name must be set on the consumer builder"));
}
if (StringUtils.isBlank(conf.getSubscriptionName())) {
return FutureUtil.failedFuture(new InvalidConfigurationException("Subscription name must be set on the consumer builder"));
}
if (conf.getKeySharedPolicy() != null && conf.getSubscriptionType() != SubscriptionType.Key_Shared) {
return FutureUtil.failedFuture(new InvalidConfigurationException("KeySharedPolicy must set with KeyShared subscription"));
}
CompletableFuture<Void> applyDLQConfig;
if (conf.isRetryEnable() && conf.getTopicNames().size() > 0) {
TopicName topicFirst = TopicName.get(conf.getTopicNames().iterator().next());
// Issue 9327: do compatibility check in case of the default retry and dead letter topic name changed
String oldRetryLetterTopic = topicFirst.getNamespace() + "/" + conf.getSubscriptionName() + RetryMessageUtil.RETRY_GROUP_TOPIC_SUFFIX;
String oldDeadLetterTopic = topicFirst.getNamespace() + "/" + conf.getSubscriptionName() + RetryMessageUtil.DLQ_GROUP_TOPIC_SUFFIX;
DeadLetterPolicy deadLetterPolicy = conf.getDeadLetterPolicy();
if (deadLetterPolicy == null || StringUtils.isBlank(deadLetterPolicy.getRetryLetterTopic()) || StringUtils.isBlank(deadLetterPolicy.getDeadLetterTopic())) {
CompletableFuture<PartitionedTopicMetadata> retryLetterTopicMetadata = client.getPartitionedTopicMetadata(oldRetryLetterTopic);
CompletableFuture<PartitionedTopicMetadata> deadLetterTopicMetadata = client.getPartitionedTopicMetadata(oldDeadLetterTopic);
applyDLQConfig = CompletableFuture.allOf(retryLetterTopicMetadata, deadLetterTopicMetadata).thenAccept(__ -> {
String retryLetterTopic = topicFirst + "-" + conf.getSubscriptionName() + RetryMessageUtil.RETRY_GROUP_TOPIC_SUFFIX;
String deadLetterTopic = topicFirst + "-" + conf.getSubscriptionName() + RetryMessageUtil.DLQ_GROUP_TOPIC_SUFFIX;
if (retryLetterTopicMetadata.join().partitions > 0) {
retryLetterTopic = oldRetryLetterTopic;
}
if (deadLetterTopicMetadata.join().partitions > 0) {
deadLetterTopic = oldDeadLetterTopic;
}
if (deadLetterPolicy == null) {
conf.setDeadLetterPolicy(DeadLetterPolicy.builder().maxRedeliverCount(RetryMessageUtil.MAX_RECONSUMETIMES).retryLetterTopic(retryLetterTopic).deadLetterTopic(deadLetterTopic).build());
} else {
if (StringUtils.isBlank(deadLetterPolicy.getRetryLetterTopic())) {
conf.getDeadLetterPolicy().setRetryLetterTopic(retryLetterTopic);
}
if (StringUtils.isBlank(deadLetterPolicy.getDeadLetterTopic())) {
conf.getDeadLetterPolicy().setDeadLetterTopic(deadLetterTopic);
}
}
conf.getTopicNames().add(conf.getDeadLetterPolicy().getRetryLetterTopic());
});
} else {
conf.getTopicNames().add(conf.getDeadLetterPolicy().getRetryLetterTopic());
applyDLQConfig = CompletableFuture.completedFuture(null);
}
} else {
applyDLQConfig = CompletableFuture.completedFuture(null);
}
return applyDLQConfig.thenCompose(__ -> {
if (interceptorList == null || interceptorList.size() == 0) {
return client.subscribeAsync(conf, schema, null);
} else {
return client.subscribeAsync(conf, schema, new ConsumerInterceptors<>(interceptorList));
}
});
}
Aggregations