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.
The dispatcher uses a multiply-high scaling algorithm to map hash codes to routing nodes efficiently.
✨ Features:
AffinityDispatcher<T>Main dispatcher for routing messages to workers.
Supports: int, long, String, byte[] keys
Important Methods:
dispatch(String key, T value)dispatch(int key, T value)dispatch(long key, T value)dispatch(byte[] key, T value)start() ✅shutdown() 🛑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:
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);
}