affinity-dispatcher

Maven Central LICENSE Maven CI pages-build-deployment

🚀 Affinity Dispatcher

Affinity Dispatcher is a high-performance, low-latency message routing framework in Java. It routes messages to workers based on key affinity and supports multiple key types.


📚 Table of Contents


Overview

The dispatcher uses a multiply-high scaling algorithm to map hash codes to routing nodes efficiently.

Features:


Key Components

🏗️ AffinityDispatcher<T>

Main dispatcher for routing messages to workers.

Supports: int, long, String, byte[] keys

Important Methods:

👷 Worker<T>

Worker thread consuming messages from its channel.

🗂️ RoutingNode<T>

Routing table node holding a reference to a worker and publishing messages.

📡 Channel<T>

Internal message queue:


🛠️ Usage Example

Handler<Object> handler = (workerName, value) -> {
    System.out.println("Handled by " + workerName + ": " + value);
};
AffinityDispatcher<Object> dispatcher = new AffinityDispatcher<>("test", handler, DefaultHashCodeProvider.INSTANCE, Config.builder().build());
dispatcher.start();

for (int i = 0; i < 10_000_000; i++) {
    dispatcher.dispatch(UUID.randomUUID().toString(), i);
}