Java Code Examples for org.apache.pulsar.client.api.SubscriptionInitialPosition#Earliest

The following examples show how to use org.apache.pulsar.client.api.SubscriptionInitialPosition#Earliest . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConsumerConnector.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private void resetOffsets(Consumer<byte[]> consumer, SubscriptionInitialPosition strategy) {
    if (strategy == null) {
        return;
    }
    log.info("Resetting partition {} for group-id {} and seeking to {} position", consumer.getTopic(),
            consumer.getSubscription(), strategy);
    try {
        if (strategy == SubscriptionInitialPosition.Earliest) {
            consumer.seek(MessageId.earliest);
        } else {
            consumer.seek(MessageId.latest);
        }
    } catch (PulsarClientException e) {
        log.warn("Failed to reset offset for consumer {} to {}, {}", consumer.getTopic(), strategy,
                e.getMessage(), e);
    }
}
 
Example 2
Source File: ConsumerConnector.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public ConsumerConnector(ConsumerConfig config) {
    checkNotNull(config, "ConsumerConfig can't be null");
    clientId = config.clientId();
    groupId = config.groupId();
    isAutoCommit = config.autoCommitEnable();
    if ("largest".equalsIgnoreCase(config.autoOffsetReset())) {
        strategy = SubscriptionInitialPosition.Latest;
    } else if ("smallest".equalsIgnoreCase(config.autoOffsetReset())) {
        strategy = SubscriptionInitialPosition.Earliest;
    }
    String consumerId = !config.consumerId().isEmpty() ? config.consumerId().get() : null;
    int maxMessage = config.queuedMaxMessages();
    String serviceUrl = config.zkConnect();

    Properties properties = config.props() != null && config.props().props() != null ? config.props().props()
            : new Properties();
    try {
        client = PulsarClientKafkaConfig.getClientBuilder(properties).serviceUrl(serviceUrl).build();
    } catch (PulsarClientException e) {
        throw new IllegalArgumentException(
                "Failed to create pulsar-client using url = " + serviceUrl + ", properties = " + properties, e);
    }

    topicStreams = Sets.newConcurrentHashSet();
    consumerBuilder = client.newConsumer();
    consumerBuilder.subscriptionName(groupId);
    if (properties.containsKey("queued.max.message.chunks") && config.queuedMaxMessages() > 0) {
        consumerBuilder.receiverQueueSize(maxMessage);
    }
    if (consumerId != null) {
        consumerBuilder.consumerName(consumerId);
    }
    if (properties.containsKey("auto.commit.interval.ms") && config.autoCommitIntervalMs() > 0) {
        consumerBuilder.acknowledgmentGroupTime(config.autoCommitIntervalMs(), TimeUnit.MILLISECONDS);
    }
    this.executor = Executors.newScheduledThreadPool(1, new DefaultThreadFactory("pulsar-kafka"));
}
 
Example 3
Source File: PulsarKafkaConsumer.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private SubscriptionInitialPosition getStrategy(final String strategy) {
    switch(strategy) {
	    case "earliest":
	        return SubscriptionInitialPosition.Earliest;
	    default:
            return SubscriptionInitialPosition.Latest;
	}
}
 
Example 4
Source File: PulsarKafkaConsumer.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private SubscriptionInitialPosition resetOffsets(final TopicPartition partition) {
	log.info("Resetting partition {} and seeking to {} position", partition, strategy);
    if (strategy == SubscriptionInitialPosition.Earliest) {
        seekToBeginning(Collections.singleton(partition));
    } else {
        seekToEnd(Collections.singleton(partition));
    } 
    return strategy;
}
 
Example 5
Source File: PulsarKafkaConsumer.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private SubscriptionInitialPosition getStrategy(final String strategy) {
    if ("earliest".equals(strategy)) {
        return SubscriptionInitialPosition.Earliest;
    } else {
        return SubscriptionInitialPosition.Latest;
    }
}
 
Example 6
Source File: PulsarKafkaConsumer.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private SubscriptionInitialPosition resetOffsets(final TopicPartition partition) {
    log.info("Resetting partition {} and seeking to {} position", partition, strategy);
    if (strategy == SubscriptionInitialPosition.Earliest) {
        seekToBeginning(partition);
    } else {
        seekToEnd(partition);
    }
    return strategy;
}