22
33import java .util .HashMap ;
44import java .util .Map ;
5+ import java .util .UUID ;
56import java .util .concurrent .atomic .AtomicInteger ;
6- import java .util .function .Consumer ;
77
88/**
99 * AffinityDispatcher is a high-performance dispatcher that routes messages between workers based on a key's hash code.
@@ -35,7 +35,7 @@ public final class AffinityDispatcher<T> {
3535 * @param hashCodeProvider provider to compute hash codes from keys
3636 * @param config dispatcher configuration (worker count, buffer size, etc.)
3737 */
38- public AffinityDispatcher (String name , Consumer <T > handler , HashCodeProvider hashCodeProvider , Config config ) {
38+ public AffinityDispatcher (String name , Handler <T > handler , HashCodeProvider hashCodeProvider , Config config ) {
3939 this .name = name ;
4040 this .workerCount = config .getWorkerCount ();
4141 this .nodesPerWorker = config .getRoutingNodePerWorker ();
@@ -47,7 +47,7 @@ public AffinityDispatcher(String name, Consumer<T> handler, HashCodeProvider has
4747 this .init (name , handler , config );
4848 }
4949
50- private void init (String name , Consumer <T > handler , Config config ) {
50+ private void init (String name , Handler <T > handler , Config config ) {
5151 WorkerFactory <T > workerFactory = new WorkerFactory <>(name , config .getWorkerThreadPriority (), config .getBatchSize (), handler );
5252
5353 for (int i = 0 ; i < workerCount ; i ++) {
@@ -212,4 +212,17 @@ public Map<String, Long> getChannelSizes() {
212212 return result ;
213213 }
214214
215+ public static void main (String [] args ) {
216+ Handler <Object > handler = (workerName , value ) -> {
217+ System .out .println ("Handled by " + workerName + ": " + value );
218+ };
219+ AffinityDispatcher <Object > dispatcher = new AffinityDispatcher <>("test" , handler , DefaultHashCodeProvider .INSTANCE , Config .builder ().build ());
220+
221+ dispatcher .start ();
222+
223+ for (int i = 0 ; i < 10_000_000 ; i ++) {
224+ dispatcher .dispatch (UUID .randomUUID ().toString (), i );
225+ }
226+ }
227+
215228}
0 commit comments