@Override protected void acknowledgeIDs(long checkpointId, Set<MessageId> messageIds) { if (consumer == null) { LOG.error("null consumer unable to acknowledge messages"); throw new RuntimeException("null pulsar consumer unable to acknowledge messages"); } if (messageIds.isEmpty()) { LOG.info("no message ids to acknowledge"); return; } Map<String, CompletableFuture<Void>> futures = new HashMap<>(messageIds.size()); for (MessageId id : messageIds) { futures.put(id.toString(), consumer.acknowledgeAsync(id)); } futures.forEach((k, f) -> { try { f.get(); } catch (Exception e) { LOG.error("failed to acknowledge messageId " + k, e); throw new RuntimeException("Messages could not be acknowledged during checkpoint creation.", e); } }); }
@Override public void onWebSocketText(String message) { super.onWebSocketText(message); // We should have received an ack MessageId msgId; try { ConsumerAck ack = ObjectMapperFactory.getThreadLocal().readValue(message, ConsumerAck.class); msgId = MessageId.fromByteArrayWithTopic(Base64.getDecoder().decode(ack.messageId), topic); } catch (IOException e) { log.warn("Failed to deserialize message id: {}", message, e); close(WebSocketError.FailedToDeserializeFromJSON); return; } consumer.acknowledgeAsync(msgId).thenAccept(consumer -> numMsgsAcked.increment()); int pending = pendingMessages.getAndDecrement(); if (pending >= maxPendingMessages) { // Resume delivery receiveMessage(); } }
consumer.acknowledgeAsync(msg); };
@Override public CompletableFuture<BenchmarkConsumer> createConsumer(String topic, String subscriptionName, ConsumerCallback consumerCallback) { return client.newConsumer().subscriptionType(SubscriptionType.Failover).messageListener((consumer, msg) -> { consumerCallback.messageReceived(msg.getData(), msg.getPublishTime()); consumer.acknowledgeAsync(msg); }).topic(topic).subscriptionName(subscriptionName).subscribeAsync() .thenApply(consumer -> new PulsarBenchmarkConsumer(consumer)); }
@Override public void received(Consumer<T> consumer, Message<T> message) { String topicName; // If more than one topics are being read than the Message return by the consumer will be TopicMessageImpl // If there is only topic being read then the Message returned by the consumer wil be MessageImpl if (message instanceof TopicMessageImpl) { topicName = ((TopicMessageImpl<?>) message).getTopicName(); } else { topicName = consumer.getTopic(); } Record<T> record = PulsarRecord.<T>builder() .message(message) .topicName(topicName) .ackFunction(() -> { if (pulsarSourceConfig .getProcessingGuarantees() == FunctionConfig.ProcessingGuarantees.EFFECTIVELY_ONCE) { consumer.acknowledgeCumulativeAsync(message); } else { consumer.acknowledgeAsync(message); } }).failFunction(() -> { if (pulsarSourceConfig.getProcessingGuarantees() == FunctionConfig.ProcessingGuarantees.EFFECTIVELY_ONCE) { throw new RuntimeException("Failed to process message: " + message.getMessageId()); } }) .build(); consume(record); }