use of org.apache.pulsar.client.api.DeadLetterPolicy 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.DeadLetterPolicy 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.DeadLetterPolicy 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));
}
});
}
use of org.apache.pulsar.client.api.DeadLetterPolicy in project study-by-myself by Howinfun.
the class PulsarConsumerAutoConfigure method run.
/**
* 注入消费者到IOC容器
* @param args
* @throws Exception
*/
@Override
public void run(String... args) throws Exception {
if (!CollectionUtils.isEmpty(this.listeners)) {
this.listeners.forEach(baseMessageListener -> {
// 获取当前监听器的@PulsarListener信息
PulsarListener pulsarListener = AnnotationUtils.findAnnotation(baseMessageListener.getClass(), PulsarListener.class);
if (Objects.nonNull(pulsarListener)) {
try {
ConsumerBuilder<String> consumerBuilder = this.pulsarClient.newConsumer(Schema.STRING).receiverQueueSize(pulsarListener.receiverQueueSize());
if (pulsarListener.topics().length > 0) {
List<String> topics = new ArrayList<>(pulsarListener.topics().length);
String tenant = StringUtils.isBlank(pulsarListener.tenant()) ? this.pulsarProperties.getTenant() : pulsarListener.tenant();
String namespace = StringUtils.isBlank(pulsarListener.namespace()) ? this.pulsarProperties.getNamespace() : pulsarListener.namespace();
Boolean persistent = Objects.nonNull(pulsarListener.persistent()) ? pulsarListener.persistent() : Boolean.TRUE;
/**
* 处理topics
*/
for (String topic : pulsarListener.topics()) {
topics.add(TopicUtil.generateTopic(persistent, tenant, namespace, topic));
}
consumerBuilder.topics(topics);
/**
* 处理订阅名称
*/
String subscriptionName = StringUtils.isBlank(pulsarListener.subscriptionName()) ? "subscription_" + RandomUtil.randomString(3) : pulsarListener.subscriptionName();
consumerBuilder.subscriptionName(subscriptionName);
consumerBuilder.ackTimeout(Long.parseLong(pulsarListener.ackTimeout()), TimeUnit.MILLISECONDS);
consumerBuilder.subscriptionType(pulsarListener.subscriptionType());
/**
* 处理死信策略
*/
if (Boolean.TRUE.equals(pulsarListener.enableRetry())) {
DeadLetterPolicy deadLetterPolicy = DeadLetterPolicy.builder().maxRedeliverCount(pulsarListener.maxRedeliverCount()).build();
if (StringUtils.isNotBlank(pulsarListener.retryLetterTopic())) {
deadLetterPolicy.setRetryLetterTopic(pulsarListener.retryLetterTopic());
}
if (StringUtils.isNotBlank(pulsarListener.deadLetterTopic())) {
deadLetterPolicy.setDeadLetterTopic(pulsarListener.deadLetterTopic());
}
consumerBuilder.enableRetry(pulsarListener.enableRetry()).deadLetterPolicy(deadLetterPolicy);
} else {
if (StringUtils.isNotBlank(pulsarListener.deadLetterTopic())) {
if (SubscriptionType.Exclusive.equals(pulsarListener.subscriptionType())) {
throw new PulsarAutoConfigException("消费端仅支持在Shared/Key_Shared模式下单独使用死信队列");
}
DeadLetterPolicy deadLetterPolicy = DeadLetterPolicy.builder().maxRedeliverCount(pulsarListener.maxRedeliverCount()).deadLetterTopic(pulsarListener.deadLetterTopic()).build();
consumerBuilder.deadLetterPolicy(deadLetterPolicy);
}
}
consumerBuilder.messageListener(baseMessageListener);
Consumer<String> consumer = consumerBuilder.subscribe();
log.info("[Pulsar] 消费者初始化完毕,topic is {},", consumer.getTopic());
}
} catch (PulsarClientException e) {
throw new PulsarAutoConfigException("[Pulsar] consumer初始化异常", e);
}
}
});
} else {
log.warn("[Pulsar] 未发现有消费者");
}
}
Aggregations