All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.castled.core.CastledOffsetListQueue Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package io.castled.core;

import com.google.common.collect.Sets;
import io.castled.schema.models.MessageOffsetSupplier;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;

public class CastledOffsetListQueue extends CastledBlockingQueue> {
    private final Set pendingMessageIds = Sets.newConcurrentHashSet();
    private long lastBufferedMessageId = 0;

    public CastledOffsetListQueue(Consumer> consumer, int parallelism, int maxCapacity, boolean exitOnError) {
        super(consumer, parallelism, maxCapacity, exitOnError);
    }

    public void writePayload(List messageList, int timeout, TimeUnit timeUnit) throws TimeoutException {
        if (CollectionUtils.isEmpty(messageList)) {
            return;
        }
        this.lastBufferedMessageId = messageList.get(messageList.size() - 1).getOffset();
        messageList.forEach(message -> pendingMessageIds.add(message.getOffset()));
        super.writePayload(messageList, timeout, timeUnit);

    }

    public Consumer> decorateConsumer(Consumer> consumer) {
        return (messageList -> {
            consumer.accept(messageList);
            messageList.forEach(message -> pendingMessageIds.remove(message.getOffset()));
        });
    }

    public long getProcessedOffset() {
        try {
            long currentMinPendingId = Collections.min(pendingMessageIds);
            return currentMinPendingId - 1;
        } catch (NoSuchElementException e) {
            return lastBufferedMessageId;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy