workers

Maven Central LICENSE Maven CI pages-build-deployment

Workers is a high-performance Java library for distributing events across multiple worker threads. It is designed around ring buffers, lock-free sequences, and consistent hashing to achieve predictable throughput and low latency.

โœจ Features

๐Ÿ“ฆ Core Concepts

๐Ÿš€ Example Usage

private static final EventHandler<Event> EVENT_HANDLER = new EventHandler<>() {
    @Override
    public void onEvent(String name, Event event, long sequence) {
        System.out.println("Worker name: " + name + ", sequence: " + sequence);
    }

    @Override
    public void onError(String name, Event event, long sequence, Throwable ex) {

    }

    @Override
    public void onStart(String name) {

    }

    @Override
    public void onShutdown(String name) {

    }
};
private static final EventFactory<Event> EVENT_FACTORY = Event::new;
private static final EventTranslatorOneArg<Event, Integer>  EVENT_TRANSLATOR = Event::setId;
private static final WorkerService<Event> WORKER_SERVICE = new WorkerService<>("test", EVENT_HANDLER, EVENT_FACTORY, DefaultHashCodeProvider.INSTANCE, WorkerServiceConfig.INSTANCE);

void main(String[] args) {
    WORKER_SERVICE.start();
    for (int i = 0; i < 10_000_000; i++) {
        WORKER_SERVICE.publishEvent(i, EVENT_TRANSLATOR, i);
    }
    WORKER_SERVICE.shutdown();
}

public static class Event {
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

โš ๏ธ Exceptions

WorkerServiceTerminatedException โ€“ thrown when a worker service is shut down or no longer available.

๐Ÿ“Š Performance

Optimized for millions of events per second on modern CPUs.

Scales with number of workers (power of 2 recommended for distribution).