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

io.descoped.rawdata.memory.MemoryRawdataProducer Maven / Gradle / Ivy

The newest version!
package io.descoped.rawdata.memory;

import de.huxhorn.sulky.ulid.ULID;
import io.descoped.rawdata.api.RawdataClosedException;
import io.descoped.rawdata.api.RawdataMessage;
import io.descoped.rawdata.api.RawdataNotBufferedException;
import io.descoped.rawdata.api.RawdataProducer;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

class MemoryRawdataProducer implements RawdataProducer {

    final ULID ulid = new ULID();

    final AtomicReference prevUlid = new AtomicReference<>(ulid.nextValue());

    final MemoryRawdataTopic topic;

    final AtomicBoolean closed = new AtomicBoolean(false);

    Consumer closeAction;

    MemoryRawdataProducer(MemoryRawdataTopic topic, Consumer closeAction) {
        this.topic = topic;
        this.closeAction = closeAction;
    }

    @Override
    public String topic() {
        return topic.topic;
    }

    @Override
    public void publish(RawdataMessage... messages) throws RawdataClosedException, RawdataNotBufferedException {
        topic.tryLock(5, TimeUnit.SECONDS);
        try {
            for (RawdataMessage message : messages) {
                if (message == null) {
                    throw new NullPointerException("on of the messages was null");
                }
                ULID.Value ulid = message.ulid();
                if (ulid == null) {
                    ulid = RawdataProducer.nextMonotonicUlid(this.ulid, prevUlid.get());
                }
                prevUlid.set(ulid);
                topic.write(ulid, message);
            }
        } finally {
            topic.unlock();
        }
    }

    @Override
    public CompletableFuture publishAsync(RawdataMessage... messages) {
        if (isClosed()) {
            throw new RawdataClosedException();
        }
        return CompletableFuture.runAsync(() -> publish(messages));
    }

    @Override
    public boolean isClosed() {
        return closed.get();
    }

    @Override
    public void close() {
        closeAction.accept(this);
        closed.set(true);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy