To learn more about QBit read one of these:
[Detailed Tutorial] QBit microservice example
public class ServiceWorkers {
public static RoundRobinServiceDispatcher workers() {...
public static ShardedMethodDispatcher shardedWorkers(final ShardRule shardRule) {...
//Your POJO
public class MultiWorker {
void doSomeWork(...) {
...
}
}
public interface MultiWorkerClient {
void doSomeWork(...);
}
/* Create a service builder. */
final ServiceBuilder serviceBuilder = serviceBuilder();
/* Create some qbit services. */
final Service service1 = serviceBuilder
.setServiceObject(new MultiWorker()).build();
final Service service2 = serviceBuilder
.setServiceObject(new MultiWorker()).build();
final Service service3 = serviceBuilder
.setServiceObject(new MultiWorker()).build();
ServiceWorkers dispatcher;
dispatcher = workers(); //Create a round robin service dispatcher
dispatcher.addServices(service1, service2, service3);
dispatcher.start(); // start up the workers
/* Add the dispatcher to a service bundle. */
bundle = serviceBundleBuilder().setAddress("/root").build();
bundle.addServiceConsumer("/workers", dispatcher);
bundle.start();
/* Start using the workers. */
final MultiWorkerClient worker =
bundle.createLocalProxy(MultiWorkerClient.class, "/workers");
public interface ShardRule {
int shard(String methodName, Object[] args, int numWorkers);
}
public static ShardedMethodDispatcher shardedWorkers(final ShardRule shardRule) {
...
}
dispatcher = shardedWorkers((methodName, methodArgs, numWorkers) -> {
String userName = methodArgs[0].toString();
int shardKey = userName.hashCode() % numWorkers;
return shardKey;
});
int workerCount = Runtime.getRuntime().availableProcessors();
for (int index = 0; index < workerCount; index++) {
final Service service = serviceBuilder
.setServiceObject(new ContentRulesEngine()).build();
dispatcher.addServices(service);
}
dispatcher.start();
bundle = serviceBundleBuilder().setAddress("/root").build();
bundle.addServiceConsumer("/workers", dispatcher);
bundle.start();
final MultiWorkerClient worker = bundle.createLocalProxy(MultiWorkerClient.class, "/workers");
for (int index = 0; index < 100; index++) {
String userName = "rickhigh" + index;
worker.pickSuggestions(userName);
}
No comments:
Post a Comment