use of com.github.howinfun.starter.listener.PulsarListener 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