{"id":13753151,"url":"https://github.com/advantageous/qbit","last_synced_at":"2025-05-16T14:04:33.878Z","repository":{"id":20069332,"uuid":"23338118","full_name":"advantageous/qbit","owner":"advantageous","description":"The Java microservice lib. QBit is a reactive programming lib for building microservices - JSON, HTTP, WebSocket, and REST. QBit uses reactive programming to build elastic REST, and WebSockets based cloud friendly, web services. SOA evolved for mobile and cloud.  ServiceDiscovery, Health, reactive StatService, events, Java idiomatic reactive programming for Microservices.","archived":false,"fork":false,"pushed_at":"2018-01-18T19:02:18.000Z","size":16576,"stargazers_count":706,"open_issues_count":61,"forks_count":139,"subscribers_count":93,"default_branch":"master","last_synced_at":"2025-04-12T10:58:45.830Z","etag":null,"topics":["actor","aws","health","java","microservice","monitoring","qbit","websocket","websocket-support"],"latest_commit_sha":null,"homepage":"http://advantageous.github.io/qbit/","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/advantageous.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"License","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-26T03:56:15.000Z","updated_at":"2025-03-03T12:19:27.000Z","dependencies_parsed_at":"2022-08-30T06:01:22.789Z","dependency_job_id":null,"html_url":"https://github.com/advantageous/qbit","commit_stats":null,"previous_names":[],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/advantageous%2Fqbit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/advantageous%2Fqbit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/advantageous%2Fqbit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/advantageous%2Fqbit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/advantageous","download_url":"https://codeload.github.com/advantageous/qbit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254544146,"owners_count":22088807,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["actor","aws","health","java","microservice","monitoring","qbit","websocket","websocket-support"],"created_at":"2024-08-03T09:01:17.183Z","updated_at":"2025-05-16T14:04:33.840Z","avatar_url":"https://github.com/advantageous.png","language":"Java","readme":"[QBit Java Micorservices lib tutorials](https://github.com/MammatusTech/qbit-microservices-examples/wiki)|\n[QBit Website](http://advantageous.github.io/qbit/) | [QBit uses Reakt](http://advantageous.github.io/reakt/) | [QBit works with Vert.x](http://vertx.io/) | [Reakt Vertx](http://advantageous.github.io/reakt-vertx/)\n\n# QBit - The Microservice Lib for Java - JSON, REST, WebSocket, Speed! \n[![Join the chat at https://gitter.im/advantageous/qbit](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/advantageous/qbit?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\n## Quick overview of QBit\n\nThe Java microservice lib. QBit is a reactive programming lib for building microservices - JSON, HTTP, WebSocket, and REST. QBit uses reactive programming to build elastic REST, and WebSockets based cloud friendly, web services. SOA evolved for mobile and cloud. ServiceDiscovery, Health, reactive StatService, events, Java idiomatic reactive programming for Microservices.\n\nGot a question? Ask here: [QBit Google Group](https://groups.google.com/forum/#!forum/qbit-microservice).\n\nEverything is a queue. You have a choice. You can embrace it and control it. You can optimize for it.\nOr you can hide behind abstractions. QBit opens you up to peeking into what is going on, and allows you\nto pull some levers without selling your soul.\n\nQBit is a library not a framework. You can mix and match QBit with Spring, Guice, etc.\n\n\n\n## New and Improved! \nQBit now supports [Reakt](http://advantageous.github.io/reakt/) invokable promises for local and remote client proxies. \nThis gives a nice fluent API for async programming. \n\n#### Invokeable promise\n```java\n        employeeService.lookupEmployee(\"123\")\n               .then((employee)-\u003e {...}).catchError(...).invoke();\n```\n\nQBit callbacks are now also Reakt Callbacks without breaking the QBit contract for Callbacks. \n\nSee [Reakt Invokable Promises for more details](https://github.com/advantageous/reakt/wiki/Invokable-Promise).\n\n## QBit is FAST!\n\n![QBit the microservice framework for java](https://docs.google.com/spreadsheets/d/1kd3gjyyz1MyTJvNLJ-BC0YIkzIU-8YYLLrxpjUl0TBQ/pubchart?oid=781959089\u0026format=image)\n\n## Getting started\n#### Using from maven\n\nQBit is published to the [maven public repo](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22io.advantageous.qbit%22%20). \n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.advantageous.qbit\u003c/groupId\u003e\n    \u003cartifactId\u003eqbit-admin\u003c/artifactId\u003e\n    \u003cversion\u003e1.10.0.RELEASE\u003c/version\u003e\n\u003c/dependency\u003e\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.advantageous.qbit\u003c/groupId\u003e\n    \u003cartifactId\u003eqbit-vertx\u003c/artifactId\u003e\n    \u003cversion\u003e1.10.0.RELEASE\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n#### Using from gradle\n```java\ncompile 'io.advantageous.qbit:qbit-admin:1.10.0.RELEASE'\ncompile 'io.advantageous.qbit:qbit-vertx:1.10.0.RELEASE'\n```\n\nCore Features\n============\n\n* Write REST based async microservices\n* Write ***WebSocket*** based async microservices (fast async RPC over WebSocket)\n* Actor Service Queues using micro-batching for high-speed message passing\n* Strongly async typed event bus which can be distributed\n* Async low overhead metrics gathering which can be queried and distributed (for doing fast lane analytics)\n* Complex async call coordination (with the Reactor) for reactive programming\n* Built-in support for ***health checks*** (and integration with tools like Consul)\n* Built-in support for ***monitoring*** (and integration with wire protocols like StatsD)\n* Built-in support for ***Service Discovery*** (with integration with health system, DNS SRV records and Consul)\n* Integration with persistent queues\n* [12 factor app port bindings](http://12factor.net/), health checks, KPI gathering, Logging MDC\n* API Gateway support for client generation and consumption via [Swagger](http://swagger.io/).\n* Our services can generate Swagger files (like idl for JSON/REST).\n* Remote client proxies for WebSocket (your interface is your IDL)\n\n\nStatus\n=====\nDeployed at several large fortune 100 companies. \nQBit now works with Vertx (standalone or embedded).\nYou can also use QBit on non-QBit projects, it is just a lib.\n\nLicense\n=====\nApache 2\n\n\n## Java Microservice Lib\n\nQBit has inproc services, REST microservices and WebSocket microservices as well as an\nin-proc service event bus (which can be per module or per app). It supports workers and in-memory services.\n\nBefore we describe more, here are two sample services:\n\n#### Todo Service\n```java\n\n@RequestMapping(\"/todo-service\")\npublic class TodoService {\n\n    @RequestMapping(\"/todo/count\")\n    public int size() {...\n\n    @RequestMapping(\"/todo/\")\n    public List\u003cTodoItem\u003e list() {...\n```\n\n\n#### Adder Service using URI params\n```java\n\n    @RequestMapping(\"/adder-service\")\n    public class AdderService {\n\n        @RequestMapping(\"/add/{0}/{1}\")\n        public int add(@PathVariable int a, @PathVariable int b) {...\n    }\n```\n\n\n\nQBit philosophy:\n====\nAt the end of the day QBit is a simple library not a framework.\nYour app is not a QBit app but a Java app that uses the QBit lib.\nQBit allows you to work with Java UTIL concurrent, and does not endeavor to hide it from you.\nJust trying to take the sting out of it.\n\nDoes it work\n=====\nWe have used techniques in Boon and QBit with great success in high-end, high-performance, high-scalable apps.\nWe helped clients handle 10x the load with 1/10th the servers of their competitors using techniques in QBit.\nQBit is us being sick of hand tuning queue access and threads.\n\n\nBoon and QBit humility policy\n=====\nIdeas for Boon and QBit often come from all over the web. We make mistakes. Point them out.\nAs a developer of Boon and QBit, we are fellow travelers.\nIf you have an idea or technique you want to share, we listen.\n\n\nInspiration\n====\n\nA big inspiration for Boon/QBit was Vertx, Akka, Go Channels, Active Objects, Apartment Model Threading, Actor,\nand the Mechanical Sympathy papers.\n\nQBit has ideas that are similar to many frameworks. We are all reading the same papers.\nQBit got inspiration from the LMAX disruptor papers and this blog post about\n[link transfer queue versus disruptor](http://php.sabscape.com/blog/?p=557). We had some theories about\nqueues that blog post inspired us to try them out. Some of these theories are deployed at some of the\nbiggest middleware backends and whose name brands are known around the world. And thus QBit was born.\n\nQBit also took an lot of inspiration by the great work done\nby Tim Fox on Vertx. The first project using something that could actually be called QBit (albeit early QBit)\n was using Vertx on an web/mobile microservice for an app that could potentially have 80 million users.\n It was this\nexperience with Vertx and early QBit that led to QBit development and evolution. QBit is built on the\nshoulders of giants (Netty/Vertx).\n\nDoes QBit compete with...\n====\nSpring Disruptor: No. You could use QBit to write plugins for Spring Disruptor I suppose, but QBit does\nnot compete with Spring Disruptor.\nSpring Boot/Spring MVC: No. We use the same annotations but QBit is geared for high-speed in-memory\nmicroservices. It is more like Akka than Spring Boot. QBit has a subset of the features of Spring MVC\ngeared only for microservices, i.e., WebSocket RPC, REST, JSON marshaling, etc.\nAkka: No. Well Maybe. Akka has similar concepts but they take a different approach. QBit is more focused\n on Java, and microservices (REST, JSON, WebSocket) than Akka.\nLMAX Disruptor: No. In fact, we can use disruptor as on of the queues that QBit uses underneath the covers.\n\n\n(Early benchmarks have been removed. They were here. QBit got a lot faster.\n Benchmarking QBit is a moving target at the moment.\n Links and reports will be created.)\n\n\nCode Examples\n\n\n## Basic Queue example (REST style services is further down)\n\n====\n\n```java\n\n     BasicQueue\u003cInteger\u003e queue =  BasicQueue.create(Integer.class, 1000);\n\n    //Sending threads\n\n     SendQueue\u003cInteger\u003e sendQueue = queue.sendQueue();\n     for (int index = 0; index \u003c amount; index++) {\n           sendQueue.send(index);\n     }\n     sendQueue.flushSends();\n     ...\n     sendQueue.sendAndFlush(code);\n     //other methods for sendQueue, writeBatch, writeMany\n\n\n     //Receiving Threads\n     ReceiveQueue\u003cInteger\u003e receiveQueue = queue.receiveQueue();\n     Integer item = receiveQueue.take();\n     //other methods poll(), pollWait(), readBatch(), readBatch(count)\n```\n\n\n### What is QBit again?\n\nQBit is a queuing library for microservices. It is similar to many other projects like Akka, Spring Reactor,\netc. QBit is just a library not a platform. QBit has libraries to put a service behind a queue.\nYou can use QBit queues directly or you can create a service. QBit services can be exposed by WebSocket,\nHTTP, HTTP pipeline, and other types of remoting. A service in QBit is a Java class whose methods are\n executed behind service queues. QBit implements apartment model threading and is similar to the\n Actor model or a better description would be Active Objects. QBit does not use a disruptor (but could).\n It uses regular Java Queues. QBit can do north of 100 million ping pong calls per second which is\n  an amazing speed (seen as high as 200M). QBit also supports calling services via REST, and WebSocket.\n   QBit is microservices in the pure Web sense: JSON, HTTP, WebSocket, etc. QBit uses micro batching to\n    push messages through the pipe (queue, IO, etc.) faster to reduce thread hand-off.\n\n### QBit lingo\n\nQBit is a Java microservice lib supporting REST, JSON and WebSocket. It is written in Java but we could\none day write a version in Rust or Go or C# (but that would require a large payday).\n\n**Service**\nPOJO (plain old Java object) behind a queue that can receive method calls via proxy calls or events\n(May have one thread managing events, method calls, and responses or two one for method calls and events\n and the other for responses so response handlers do not block service. One is faster unless responses block). Services can use Spring MVC style REST annotations to expose themselves to the outside world via REST and WebSocket.\n\n**ServiceBundle**\nMany POJOs behind one response queue and many receive queues. There may be one thread for all responses\nor not. They also can be one receive queue.\n\n**Queue**\nA thread managing a queue. It supports batching. It has events for empty, reachedLimit, startedBatch,\nidle. You can listen to these events from services that sit behind a queue. You don't have to use Services.\nYou can use Queue's direct. In QBit, you have sender queues and receivers queues. They are separated to\nsupport micro-batching.\n\n**ServiceEndpointServer**\nServiceBundle that is exposed to REST and WebSocket communication.\n\n**EventBus**\nEventBus is a way to send a lot of messages to services that may be loosely coupled.\n\n**ClientProxy**\nClientProxy is a way to invoke service through async interface, service can be inproc (same process) or\nremoted over WebSocket.\n\n**Non-blocking**\nQBit is a non-blocking lib. You use CallBacks via Java 8 Lambdas. You can also send event messages and get\nreplies. Messaging is built into the system so you can easily coordinate complex tasks.\nQBit takes an object-oriented approach to service development so services look like normal Java services\nthat you\nalready write, but the services live behind a queue/thread. This is not a new concept. Microsoft did this\nwith DCOM/COM and called it active objects. Akka does it with actors and called them strongly typed Actors.\nThe important concepts is that you get the speed of reactive and actor style messaging but you develop\nin a natural OOP approach. QBit is not the first. QBit is not the only.\n\n\n**Speed**\nQBit is VERY fast. There is a of course a lot of room for improvement. But already 200M+ TPS inproc ping pong, 10M-20M+ TPS event bus, 500K TPS RPC calls over WebSocket/JSON, etc.\nMore work needs to be done to improve speed, but now it is fast enough where we are focusing more on\nusability.\nThe JSON support uses Boon by default which is up to 4x faster than other JSON parsers for the\nREST/JSON, WebSocket/JSON use case.\n\n**Reactive Programming**\nQBit provides a **Reactor** to manage async calls. This allows callbacks to be handled on the same thread that called them and it provides for timeout and error handling. Read [Reactor tutorial for creating reactive micro service programming](https://github.com/MammatusTech/qbit-microservices-examples/wiki/Reactor-tutorial--%7C-reactively-handling-async-calls-with-QBit-Reactive-Microservices)\n\n**Service Discovery**\nBuilt in support for service discovery. This includes integration with Consul.\n\n\n**StatService**\nBuilt in support for stats. The **StatService** can be integrated with **StatsD** (Graphite, Grafana, DataDog, etc.) to publish passive stats. Or you can query the stats engine and react to the stats (counts, timings and levels). The **StatsService** is a reactive stats system that can be clustered. The StatService is reactive in that your services can publish to it and query it and react based on the results. You can implement things like rate limiting and react to an increased rate of something. The ServiceDiscovery system integrates with the HealthSystem and Consul to roll up each of your internal services that make up you micro service and publish the composite availably of your micro service to a single HTTP endpoint or a dead mans switch in Consul (TTL). \n\n\n\n### CURLable REST services example\n\nTalk is cheap. Let's look at some code. You can get a detailed walk through in the Wiki.\nWe have a lot of documentation already.\n\nWe will create a service that is exposed through REST/JSON.\n\nTo query the size of the todo list:\n\n```bash\ncurl localhost:8080/services/todo-service/todo/count\n```\n\nTo add a new TODO item.\n\n```bash\ncurl -X POST -H \"Content-Type: application/json\" -d \\\n'{\"name\":\"xyz\",\"description\":\"xyz\"}' \\\nhttp://localhost:8080/services/todo-service/todo\n```\n\nTo get a list of TODO items\n```bash\ncurl http://localhost:8080/services/todo-service/todo/\n```\n\nThe TODO example will use and track Todo items.\n\n#### Todo item POJO sans getter\n\n```java\npackage io.advantageous.qbit.examples;\n\nimport java.util.Date;\n\n\npublic class TodoItem {\n\n\n    private final String description;\n    private final String name;\n    private final Date due;\n\n```\n\nThe TodoService uses Spring MVC style annotations.\n\n#### Todo Service\n```java\n\n@RequestMapping(\"/todo-service\")\npublic class TodoService {\n\n\n    private List\u003cTodoItem\u003e todoItemList = new ArrayList\u003c\u003e();\n\n\n    @RequestMapping(\"/todo/count\")\n    public int size() {\n\n        return todoItemList.size();\n    }\n\n    @RequestMapping(\"/todo/\")\n    public List\u003cTodoItem\u003e list() {\n\n        return todoItemList;\n    }\n\n    @RequestMapping(value = \"/todo\", method = RequestMethod.POST)\n    public void add(TodoItem item) {\n\n        todoItemList.add(item);\n    }\n\n}\n\n```\n\n\n#### Sending non-JSON\n\nYou can POST/PUT non-JSON and you can capture the body as a `String` or as a `byte[]`. \nIf the content-type is set to anything but `application/json` and your body is defined a String or byte[].\nThis works automatically. (The content-type has to be set.)\n\n```java\n    @RequestMapping(value = \"/body/bytes\", method = RequestMethod.POST)\n    public boolean bodyPostBytes( byte[] body) {\n        String string = new String(body, StandardCharsets.UTF_8);\n        return string.equals(\"foo\");\n    }\n\n    @RequestMapping(value = \"/body/string\", method = RequestMethod.POST)\n    public boolean bodyPostString(String body) {\n        return body.equals(\"foo\");\n    }\n```\n\n\n#### Sending different response codes for success\n\nBy default QBit sends a `200` (OK) for a non-void call (a call that has a return or a Callback). If the REST operation has no return or no callback then QBit sends a `202` (Accepted). There may be times when you want to send a 201 (Created) or some other code that is not an Exception. You can do that by setting `code` on `@RequestMapping`. By default the code is -1 which means use the default behavior (200 for success, 202 for one-way message, and 500 for errors).\n\n\n#### Sending different response codes for success\n```java\n\n  @RequestMapping(value = \"/helloj7\", code = 221)\n    public void helloJSend7(Callback\u003cJSendResponse\u003cList\u003cString\u003e\u003e\u003e callback) {\n        callback.returnThis(JSendResponseBuilder.jSendResponseBuilder(Lists.list(\n                \"hello \" + System.currentTimeMillis())).build());\n    }\n\n```\n\n`Callbacks` can be used for internal services as well. It is often the case that you use a [CallbackBuilder](https://github.com/advantageous/qbit/wiki/%5BDocument%5D-CallbackBuilder-and-generics-for-Reactive-Java-Microservices) or a QBit [Reactor](https://github.com/MammatusTech/qbit-microservices-examples/wiki/Reactor-tutorial--%7C-reactively-handling-async-calls-with-QBit-Reactive-Microservices) to manage service calls. \n\n#### Working with non JSON responses\n\nYou do not have to return JSON form rest calls.\nYou can return any binary or any text by using `HttpBinaryResponse` and `HttpTextResponse`.\n\n#### Returning non JSON from REST call\n```java\n      @RequestMapping(method = RequestMethod.GET)\n        public void ping2(Callback\u003cHttpTextResponse\u003e callback) {\n\n            callback.resolve(HttpResponseBuilder.httpResponseBuilder()\n                    .setBody(\"hello mom\").setContentType(\"mom\")\n                    .setCode(777)\n                    .buildTextResponse());\n        }\n```\n\n\n#### Returning binary from REST call\n```java\n      @RequestMapping(method = RequestMethod.GET)\n        public void ping2(Callback\u003cHttpBinaryResponse\u003e callback) {\n\n            callback.resolve(HttpResponseBuilder.httpResponseBuilder()\n                    .setBody(\"hello mom\").setContentType(\"mom\")\n                    .setCode(777)\n                    .buildBinaryResponse());\n        }\n```\n\n#### Side note Why Spring style annotations?\nWhy did we pick Spring style annotations?\n1) Spring is not a standard and neither is QBit. 2) We found the Spring annotations to be less verbose.\n3) More people use Spring than Java EE. We wrote QBit for people to use.\nWe could easily support JAX-RS style annotations, and we probably will.\nSince QBit focuses on JSON, we do not need all of the complexity of JAX-RS or even all the features of\nthe Spring MVC annotations. Also we can literally use the actual Spring annotations. QBit and Boon\nuse a non-type safe mechanism for annotations which means they are not tied to a particular lib.\nYou can define your own. We hate vendor tie-in even if it is an open source vendor.\n(We also support @POST, @GET which is similar to JAX-RS).\n\nNow just start it up.\n\n```java\n\n    public static void main(String... args) {\n\n        ServiceEndpointServer server = new EndpointServerBuilder().build();\n        server.initServices(new TodoService());\n        server.start();\n    }\n```\n\n\n\nThat is it. There is also out of the box WebSocket support with client side proxy generation so\nyou can call into services at the rate of millions of calls per second.\n\n## Using URI Params for QBit microservice\n\n```java\n\n    @RequestMapping(\"/adder-service\")\n    public class AdderService {\n\n\n        @RequestMapping(\"/add/{0}/{1}\")\n        public int add(@PathVariable int a, @PathVariable int b) {\n\n            return a + b;\n        }\n    }\n\n```\n\n## WebSocket\nYou can always invoke QBit services via a WebSocket proxy.\nThe advantage of a WebSocket proxy is it allows you execute 1M RPC+ a second (1 million remote calls\nevery second).\n\n\n#### Using a microservice remotely with WebSocket\n```java\n       /* Start QBit client for WebSocket calls. */\n        final Client client = clientBuilder()\n                   .setPort(7000).setRequestBatchSize(1).build();\n\n\n       /* Create a proxy to the service. */\n        final AdderServiceClientInterface adderService =\n                client.createProxy(AdderServiceClientInterface.class,\n                \"adder-service\");\n\n        client.start();\n\n\n\n       /* Call the service */\n        adderService.add(System.out::println, 1, 2);\n\n```\n\nThe output is 3.\n\n```output\n3\n```\n\n\nThe above uses a WebSocket proxy interface to call the service async.\n\n```java\n\n    interface AdderServiceClientInterface {\n\n        void add(Callback\u003cInteger\u003e callback, int a, int b);\n    }\n```\n\n#### ServiceDiscovery aware websocket builders\n\nCreate websocket service client that is ServiceDiscovery aware.\n\n\n```java\n        final Client client = clientBuilder.setServiceDiscovery(serviceDiscovery, \"echo\")\n                .setUri(\"/echo\").setProtocolBatchSize(20).build().startClient();\n\n\n        final EchoAsync echoClient = client.createProxy(EchoAsync.class, \"echo\");\n```\n\nCurrently the `clientBuilder` will load all service endpoints that are registered under the service name,\nand randomly pick one. \n\nServiceDiscovery includes Consul based, watching JSON files on disk, and DNS. It is easy to write your own service discovery as well and plug it into QBit.\n\n\nIn the future we can RoundRobin calls or shard calls to websocket service and/or provide auto fail over if the connection is closed. We do this for the event bus that uses service discovery but it is not baked into WebSocket based client stubs yet.\n\n\n## REST call with URI params\n\nThe last client example uses WebSocket. You could also just use REST, and actually use the URI params\nthat we setup.\nREST is nice but it is going to be slower than WebSocket support.\n\nQBit ships with a nice little HTTP client. We can use it.\n\nYou can use it to send async calls and WebSocket messages with the HTTP client.\n\n\nHere we will use the http client to invoke our remote method:\n\n#### Using a microservice remotely with REST QBit microservice client\n```java\n\n\n        HttpClient httpClient = httpClientBuilder()\n                .setHost(\"localhost\")\n                .setPort(7000).build();\n\n        httpClient.start();\n        String results = httpClient\n                   .get(\"/services/adder-service/add/2/2\").body();\n        System.out.println(results);\n\n```\n\n\nThe output is 4.\n\n```output\n4\n```\n\n## Accessing The URI Param example with CURL\n\nYou can also access the service from curl.\n\n```bash\n$ curl http://localhost:7000/services/adder-service/add/2/2\n```\n\nSee this full example here: [QBit microservice getting started tutorial](https://github.com/advantageous/qbit/wiki/%5BDetailed-Tutorial%5D-------------QBit-microservice-example).\n\n[QBit URI params and WebSocket proxy client](https://github.com/advantageous/qbit/wiki/%5BRough-Cut%5D-Using-QBit-microservice-lib's-REST-support-with-URI-Params)\n\n## Working with WebSocket, HttpClient etc.\n\nQBit has a library for working with and writing async microservices that is lightweight and fun to use.\n\n#### WebSocket server and client.\n\n\n#### Create an HTTP server\n```java\n\n        /* Create an HTTP server. */\n        HttpServer httpServer = httpServerBuilder()\n                .setPort(8080).build();\n\n```\n\n#### Setup server WebSocket support\n```java\n        /* Setup WebSocket Server support. */\n        httpServer.setWebSocketOnOpenConsumer(webSocket -\u003e {\n            webSocket.setTextMessageConsumer(message -\u003e {\n                webSocket.sendText(\"ECHO \" + message);\n            });\n        });\n\n```\n\n#### Start the server\n```java\n\n        /* Start the server. */\n        httpServer.start();\n```\n\n#### Setup the WebSocket client\n```java\n\n        /** CLIENT. */\n\n        /* Setup an httpClient. */\n        HttpClient httpClient = httpClientBuilder()\n                .setHost(\"localhost\").setPort(8080).build();\n        httpClient.start();\n```\n\n#### Client WebSocket\n\n```java\n\n        /* Setup the client websocket. */\n        WebSocket webSocket = httpClient\n                .createWebSocket(\"/websocket/rocket\");\n\n        /* Setup the text consumer. */\n        webSocket.setTextMessageConsumer(message -\u003e {\n            System.out.println(message);\n        });\n        webSocket.openAndWait();\n\n        /* Send some messages. */\n        webSocket.sendText(\"Hi mom\");\n        webSocket.sendText(\"Hello World!\");\n\n```\n\n#### Output\n```output\n\nECHO Hi mom\nECHO Hello World!\n\n```\n\nNow stop the server and client. Pretty easy eh?\n\n## High-speed HTTP client and server done microservice style\n\n\n##### Starting up an HTTP server\n\n```java\n\n        /* Create an HTTP server. */\n        HttpServer httpServer = httpServerBuilder()\n                .setPort(8080).build();\n\n        /* Setting up a request Consumer with Java 8 Lambda expression. */\n        httpServer.setHttpRequestConsumer(httpRequest -\u003e {\n\n            Map\u003cString, Object\u003e results = new HashMap\u003c\u003e();\n            results.put(\"method\", httpRequest.getMethod());\n            results.put(\"uri\", httpRequest.getUri());\n            results.put(\"body\", httpRequest.getBodyAsString());\n            results.put(\"headers\", httpRequest.getHeaders());\n            results.put(\"params\", httpRequest.getParams());\n            httpRequest.getReceiver()\n                .response(200, \"application/json\", Boon.toJson(results));\n        });\n\n\n        /* Start the server. */\n        httpServer.start();\n\n\n```\n\nThe focus is on ease of use and using Java 8 Lambdas for callbacks so the code is tight and small.\n\n[Find out more about QBit's microservice style WebSocket support here](https://github.com/advantageous/qbit/wiki/%5BDoc%5D-Using-QBit-microservice-lib's-WebSocket-support)\n\n\n## Using HTTP Client lib\n\nNow, let's try out our HTTP client.\n\n##### Starting up an HTTP client\n\n```java\n\n        /* Setup an httpClient. */\n        HttpClient httpClient = httpClientBuilder()\n                  .setHost(\"localhost\").setPort(8080).build();\n        httpClient.start();\n```\n\nYou just pass the URL, the port and then call start.\n\n## Synchronous HTTP calls\n\nNow you can start sending HTTP requests.\n\n##### No Param HTTP GET\n\n```java\n        /* Send no param get. */\n        HttpResponse httpResponse = httpClient.get( \"/hello/mom\" );\n        puts( httpResponse );\n```\n\nAn HTTP response just contains the results from the server.\n\n\n##### No Param HTTP Response\n\n```java\npublic interface HttpResponse {\n\n    MultiMap\u003cString, String\u003e headers();\n\n    int code();\n\n    String contentType();\n\n    String body();\n\n}\n\n```\n\nThere are helper methods for sync HTTP GET calls.\n\n\n\n##### Helper methods for GET\n```java\n\n\n        /* Send one param get. */\n        httpResponse = httpClient.getWith1Param(\"/hello/singleParam\",\n                                        \"hi\", \"mom\");\n        puts(\"single param\", httpResponse );\n\n\n        /* Send two param get. */\n        httpResponse = httpClient.getWith2Params(\"/hello/twoParams\",\n                \"hi\", \"mom\", \"hello\", \"dad\");\n        puts(\"two params\", httpResponse );\n\n...\n\n        /* Send five param get. */\n        httpResponse = httpClient.getWith5Params(\"/hello/5params\",\n                \"hi\", \"mom\",\n                \"hello\", \"dad\",\n                \"greetings\", \"kids\",\n                \"yo\", \"pets\",\n                \"hola\", \"neighbors\");\n        puts(\"5 params\", httpResponse );\n\n\n```\n\nThe puts method is a helper method it does System.out.println more or less by the way.\n\nThe first five params are covered. Beyond five, you have to use the HttpBuilder.\n\n```java\n\n\n        /* Send six params with get. */\n\n        final HttpRequest httpRequest = httpRequestBuilder()\n                .addParam(\"hi\", \"mom\")\n                .addParam(\"hello\", \"dad\")\n                .addParam(\"greetings\", \"kids\")\n                .addParam(\"yo\", \"pets\")\n                .addParam(\"hola\", \"pets\")\n                .addParam(\"salutations\", \"all\").build();\n\n        httpResponse = httpClient.sendRequestAndWait(httpRequest);\n        puts(\"6 params\", httpResponse );\n```\n\n## Http Async HTTP Client\n\nThere are async calls for GET as well.\n\n#### Async calls for HTTP GET using Java 8 lambda\n\n```java\n\n        /* Using Async support with lambda. */\n        httpClient.getAsync(\"/hi/async\", (code, contentType, body) -\u003e {\n            puts(\"Async text with lambda\", body);\n        });\n\n        Sys.sleep(100);\n\n\n        /* Using Async support with lambda. */\n        httpClient.getAsyncWith1Param(\"/hi/async\", \"hi\", \"mom\", (code, contentType, body) -\u003e {\n            puts(\"Async text with lambda 1 param\\n\", body);\n        });\n\n        Sys.sleep(100);\n\n\n\n        /* Using Async support with lambda. */\n        httpClient.getAsyncWith2Params(\"/hi/async\",\n                \"p1\", \"v1\",\n                \"p2\", \"v2\",\n                (code, contentType, body) -\u003e {\n                    puts(\"Async text with lambda 2 params\\n\", body);\n                });\n\n        Sys.sleep(100);\n\n\n...\n        /* Using Async support with lambda. */\n        httpClient.getAsyncWith5Params(\"/hi/async\",\n                \"p1\", \"v1\",\n                \"p2\", \"v2\",\n                \"p3\", \"v3\",\n                \"p4\", \"v4\",\n                \"p5\", \"v5\",\n                (code, contentType, body) -\u003e {\n                    puts(\"Async text with lambda 5 params\\n\", body);\n                });\n\n        Sys.sleep(100);\n\n```\n\n[Find more about the easy to use, fast microservice HTTP client here]\n(https://github.com/advantageous/qbit/wiki/%5BDoc%5D-Using-QBit-microservice-lib's-HttpClient-GET,-POST,-et-al,-JSON,-Java-8-Lambda).\n\n## InProc QBit services\n\nQBit allows for services behind queues to be run in-proc as well.\n\n```java\n\n        /* POJO service. */\n        final TodoManager todoManagerImpl = new TodoManager();\n\n        /*\n        Create the service which manages async calls to todoManagerImpl.\n         */\n        final Service service = serviceBuilder()\n                .setServiceObject(todoManagerImpl)\n                .build().startServiceQueue();\n\n\n        /* Create Asynchronous proxy over Synchronous service. */\n        final TodoManagerClientInterface todoManager =\n              service.createProxy(TodoManagerClientInterface.class);\n\n        service.startCallBackHandler();\n\n\n        System.out.println(\"This is an async call\");\n        /* Asynchronous method call. */\n        todoManager.add(new Todo(\"Call Mom\", \"Give Mom a call\"));\n\n\n        AtomicInteger countTracker = new AtomicInteger();\n        //Hold count from async call to service... for testing and showing it is an async callback\n\n        System.out.println(\"This is an async call to count\");\n\n        todoManager.count(count -\u003e {\n            System.out.println(\"This lambda expression is the callback \" + count);\n\n            countTracker.set(count);\n        });\n\n\n        todoManager.clientProxyFlush(); //Flush all methods. It batches calls.\n\n        Sys.sleep(100);\n\n        System.out.printf(\"This is the count back from the server %d\\n\", countTracker.get());\n\n```\n\n[Detailed tutorial on in-proc services is being written.](https://github.com/advantageous/qbit/wiki/%5BDetailed-Tutorial%5D-Working-with-inproc-MicroServices-within-QBit.)\n\n\n## QBit Event Bus\n\n[QBit Event Bus more detailed example](https://github.com/advantageous/qbit/wiki/%5BRough-Cut%5D-Working-with-event-bus-for-QBit-the-microservice-engine)\n\nQBit also has a service event bus. This example is a an employee benefits services example.\n\nWe have two channels.\n\n```\npublic static final String NEW_HIRE_CHANNEL = \"com.mycompnay.employee.new\";\n\npublic static final String PAYROLL_ADJUSTMENT_CHANNEL = \"com.mycompnay.employee.payroll\";\n```\n\nAn employee object looks like this:\n\n```java\n\npublic static class Employee {\n       final String firstName;\n       final int employeeId;\n```\n\n\nThis example has three services: EmployeeHiringService, BenefitsService, and PayrollService.\n\nThese services are inproc services. QBit supports WebSocket, HTTP and REST remote services as well, but for now, let's focus on inproc services. If you understand inproc then you will understand remote.\n\nThe EmployeeHiringService actually fires off the events to other two services.\n\n```java\npublic class EmployeeHiringService {\n\n\n    public void hireEmployee(final Employee employee) {\n\n           int salary = 100;\n           System.out.printf(\"Hired employee %s\\n\", employee);\n\n           //Does stuff to hire employee\n\n           //Sends events\n           final EventManager eventManager =\n                               serviceContext().eventManager();\n           eventManager.send(NEW_HIRE_CHANNEL, employee);\n\n           eventManager.sendArray(PAYROLL_ADJUSTMENT_CHANNEL,\n                                     employee, salary);\n\n\n    }\n\n   }\n```\n\n\nNotice that we call sendArray so we can send the employee and their salary.\nThe listener for PAYROLL_ADJUSTMENT_CHANNEL will have to handle both an employee and an int\nthat represents the new employees salary.\nYou can also use event bus proxies so you do not have to call into the event bus at all.\n\nThe BenefitsService listens for new employees being hired so it can enroll them into the benefits system.\n\n```java\npublic static class BenefitsService {\n\n       @OnEvent(NEW_HIRE_CHANNEL)\n       public void enroll(final Employee employee) {\n\n           System.out.printf(\"Employee enrolled into benefits system employee %s %d\\n\",\n                   employee.getFirstName(), employee.getEmployeeId());\n\n       }\n\n```\n\nDaddy needs to get paid.\n\n```java\n    public static class PayrollService {\n\n        @OnEvent(PAYROLL_ADJUSTMENT_CHANNEL)\n        public void addEmployeeToPayroll(final Employee employee, int salary) {\n\n            System.out.printf(\"Employee added to payroll  %s %d %d\\n\",\n                    employee.getFirstName(), employee.getEmployeeId(), salary);\n\n        }\n\n    }\n\n```\n\nThe employee is the employee object from the EmployeeHiringService.\n\nso you can get your benefits, and paid!\n\nFind more details here:\n\n[QBit Event Bus more detailed example](https://github.com/advantageous/qbit/wiki/%5BRough-Cut%5D-Working-with-event-bus-for-QBit-the-microservice-engine)\n\n\n## Private event bus and event bus proxies\n\n You can define your own interface to the event bus and you can use your own event buses with QBit.\n Each module in your service can have its own internal event bus.\n\n To learn more read:\n [QBit Microservice working with a private event bus](https://github.com/advantageous/qbit/wiki/%5BRough-Cut%5D-Working-with-private-event-bus-for-inproc-microservices)\n and [QBit Java Microservice lib using your own interface to the event bus](https://github.com/advantageous/qbit/wiki/%5BRough-Cut%5D-Working-with-strongly-typed-event-bus-proxies-for-QBit-Java-Microservice-lib).\n\n\n\n## Queue Callbacks\n\n To really grasp QBit, one must grasp the concepts of a CallBack.\n\n A CallBack is a way to get an async response in QBit.\n\n You call a service method and it calls you back.\n\n Client proxies can have callbacks:\n\n\n#### Queue Callbacks - RecommendationService client interface\n```java\n\npublic interface RecommendationServiceClient {\n\n\n    void recommend(final Callback\u003cList\u003cRecommendation\u003e\u003e recommendationsCallback,\n                          final String userName);\n}\n\n```\n\nCallbacks are Java 8 Consumers with some optional extra error handling.\n\n\n#### Queue Callbacks - Callback\n\n```java\n\n\npublic interface Callback \u003cT\u003e extends java.util.function.Consumer\u003cT\u003e {\n    default void onError(java.lang.Throwable error) { /* compiled code */ }\n}\n\n```\n\nServices that can block should use callbacks.\nThus if loadUser blocked in the following example, it should really use a callback instead of returning a value.\n\npublic class RecommendationService {\n\n#### Queue Callbacks - Simple minded implementation of RecommendationService\n```java\n\n    private final SimpleLRUCache\u003cString, User\u003e users =\n            new SimpleLRUCache\u003c\u003e(10_000);\n\n    public List\u003cRecommendation\u003e recommend(final String userName) {\n        User user = users.get(userName);\n        if (user == null) {\n            user = loadUser(userName);\n        }\n        return runRulesEngineAgainstUser(user);\n    }\n\n```\n\n\nLet's pretend `loadUser` has to look in a local cache, and if the user is not found, look in an off-heap cache\nand if not found it must ask for the user from the UserService which must check its caches and perhaps fallback\nto loading the user data from a database or from other services.\nIn other words, `loadUser` can potentially block on IO.\n\n\n\n### Queue Callbacks - The first rule of Queue Club - don't block\n\n\nOur client does not block, but our service does. Going back to our `RecommendationService`.\nIf we get a lot of cache hits for user loads, perhaps the\nblock will not be that long, but it will be there and every time we have to fault in a user, the whole system\nis gummed up. What we want to be able to do is if we can't handle the recommendation request,\nwe go ahead and make an async call to the `UserDataService`. When that async callback comes back, then we\nhandle that request. In the mean time, we handle recommendation lists requests as quickly as we can.\nWe never block.\n\n\nSo let's revisit the service. The first thing we are going to do is make the service method take\na callback. Before we do that, let's set down some rules.\n\n\n#### The first rule of queue club don't block.\n#### The second rule of queue club if you are not ready, use a callback and continue handling stuff you are ready for\n\n\n#### Queue Callbacks - Adding a CallBack to the RecommendationService inproc microservice\n\n```java\npublic class RecommendationService {\n\n\n    public void recommend(final Callback\u003cList\u003cRecommendation\u003e\u003e recommendationsCallback,\n                          final String userName) {\n\n```\n\n\n\nNow we are taking a callback and we can decide when we want to handle this recommendation generation request.\nWe can do it right away if there user data we need is in-memory or we can delay it.\n\n\n#### If the user is found, call the callback right away for RecommendationService inproc microservice\n\n```java\n\n    public void recommend(final Callback\u003cList\u003cRecommendation\u003e\u003e recommendationsCallback,\n                          final String userName) {\n\n        /** Look for user in user cache. */\n        User user = users.get(userName);\n\n        /** If the user not found, load the user from the user service. */\n        if (user == null) {\n             ...\n        } else {\n             /* Call the callback now because we can handle the callback now. */\n            recommendationsCallback.accept(runRulesEngineAgainstUser(user));\n        }\n\n    }\n\n```\n\n\nNotice, if the user is found in the cache, we run our recommendation rules in-memory and call the callback right away\n`recommendationsCallback.accept(runRulesEngineAgainstUser(user))`.\n\nThe interesting part is what do we do if don't have the user loaded.\n\n\n#### If the user was not found, load him from the user microservice, but still don't block\n\n```java\n\n\n    public void recommend(final Callback\u003cList\u003cRecommendation\u003e\u003e recommendationsCallback,\n                          final String userName) {\n\n\n        /** Look for user in users cache. */\n        User user = users.get(userName);\n\n        /** If the user not found, load the user from the user service. */\n        if (user == null) {\n\n            /* Load user using Callback. */\n            userDataService.loadUser(new Callback\u003cUser\u003e() {\n                @Override\n                public void accept(final User loadedUser) {\n                        handleLoadFromUserDataService(loadedUser,\n                                recommendationsCallback);\n                }\n            }, userName);\n\n        }\n        ...\n\n```\n\nHere we use a CallBack to load the user, and when the user is loaded, we call `handleLoadFromUserDataService`\nwhich adds some management about handling the callback so we can still handle this call, just not now.\n\n\n\n#### Lambda version of last example\n\n```java\n\n\n    public void recommend(final Callback\u003cList\u003cRecommendation\u003e\u003e recommendationsCallback,\n                          final String userName) {\n\n\n        /** Look for user in users cache. */\n        User user = users.get(userName);\n\n        /** If the user not found, load the user from the user service. */\n        if (user == null) {\n\n            /* Load user using lambda expression. */\n         userDataService.loadUser(\n                    loadedUser -\u003e {\n                        handleLoadFromUserDataService(loadedUser,\n                        recommendationsCallback);\n                    }, userName);\n\n        }\n        ...\n\n```\n\nUsing lambdas like this makes the code more readable and terse, but remember don't deeply nest\nlambda expressions or you will create a code maintenance nightmare. Use them judiciously.\n\n\n### Queue Callbacks - Doing something later\n\nWhat we want is to handle the request for recommendations after the user service system loads\nthe user from its store.\n\n\n\n#### Handling UserServiceData callback methods once we get them.\n\n```java\n\npublic class RecommendationService {\n\n\n    private final SimpleLRUCache\u003cString, User\u003e users =\n            new SimpleLRUCache\u003c\u003e(10_000);\n\n    private UserDataServiceClient userDataService;\n\n\n    private BlockingQueue\u003cRunnable\u003e callbacks =\n               new ArrayBlockingQueue\u003cRunnable\u003e(10_000);\n\n\n    ...\n\n    public void recommend(final Callback\u003cList\u003cRecommendation\u003e\u003e recommendationsCallback,\n                          final String userName) {\n\n        ...\n\n    }\n\n    /** Handle defered recommendations based on user loads. */\n    private void handleLoadFromUserDataService(final User loadedUser,\n                                               final Callback\u003cList\u003cRecommendation\u003e\u003e recommendationsCallback) {\n\n        /** Add a runnable to the callbacks queue. */\n        callbacks.add(new Runnable() {\n            @Override\n            public void run() {\n              List\u003cRecommendation\u003e recommendations = runRulesEngineAgainstUser(loadedUser);\n              recommendationsCallback.accept(recommendations);\n            }\n        });\n    }\n\n\n\n```\n\n\n\n\n####  handleLoadFromUserDataService rewritten using Lambda\n\n```java\n\n\npublic class RecommendationService {\n\n...\n\n    /** Handle defered recommendations based on user loads. */\n    private void handleLoadFromUserDataService(final User loadedUser,\n                                               final Callback\u003cList\u003cRecommendation\u003e\u003e recommendationsCallback) {\n\n        /** Add a runnable to the callbacks list. */\n        callbacks.add(() -\u003e {\n            List\u003cRecommendation\u003e recommendations = runRulesEngineAgainstUser(loadedUser);\n            recommendationsCallback.accept(recommendations);\n        });\n\n    }\n\n```\n\n\nThe important part there is that every time we get a callback call from `UserDataService`, we then\nperform our CPU intensive recommendation rules and callback our caller. Well not exactly, what we\ndo is enqueue an runnable onto our callbacks queue, and later we will iterate through those but when?\n\n\n### Queue Callbacks Handling callbacks when our receive queue is empty, a new batch started or we hit a batch limit\n\nThe `RecommendationService` can be notified when its queue is empty, it has started a new batch and when\nit has reached a batch limit. These are all good times to handle callbacks from the `UserDataService`.\n\n#### Draining our callback queue\n```java\n\n    @QueueCallback({\n            QueueCallbackType.EMPTY,\n            QueueCallbackType.START_BATCH,\n            QueueCallbackType.LIMIT})\n    private void handleCallbacks() {\n\n        flushServiceProxy(userDataService);\n        Runnable runnable = callbacks.poll();\n\n        while (runnable != null) {\n            runnable.run();\n            runnable = callbacks.poll();\n        }\n    }\n\n```\n\nIt is important to remember when handling callbacks from another microservice that you want to handle\ncallbacks from the other service before you handle more incomming requests from you clients.\nEssentially you have clients that have been waiting (async waiting but still), and these clients\nmight represent an open TCP/IP connection like an HTTP call so it is best to close them out\nbefore handling more requests and like we said they were already waiting around with an open connection\nfor users to load form the user service.\n\nTo learn more about CallBacks, plesae read [QBit Java MicroService Lib CallBack fundamentals]([Rough Cut] QBit Microservice Lib Working With CallBacks).\n\n\n\n\n## Workers - pools and shards\n\n```java\npublic class ServiceWorkers {\n\n    public static RoundRobinServiceDispatcher workers() {...\n\n    public static ShardedMethodDispatcher shardedWorkers(final ShardRule shardRule) {...\n```\n\nYou can compose sharded workers (for in-memory, thread safe, CPU intensive services), or workers for IO\nor talking to foreign services or foreign buses.\n\nHere is an example that uses a worker pool with three service workers in it:\n\nLet's say you have a service that does something:\n\n```java\n\n    //Your POJO\n    public  class MultiWorker {\n\n        void doSomeWork(...) {\n           ...\n        }\n\n    }\n\n```\n\nNow this does some sort of IO and you want to have a bank of these running not just one so you can do\nIO in parallel. After some performance testing, you found out that three is the magic number.\n\nYou want to use your API for accessing this service:\n\n```java\n    public  interface MultiWorkerClient {\n        void doSomeWork(...);\n    }\n\n```\n\nNow let's create a bank of these and use it.\n\nFirst create the QBit services which add the thread/queue/microbatch.\n\n```java\n\n        /* Create a service builder. */\n        final ServiceBuilder serviceBuilder = serviceBuilder();\n\n        /* Create some qbit services. */\n        final Service service1 = serviceBuilder.setServiceObject(new MultiWorker()).build();\n        final Service service2 = serviceBuilder.setServiceObject(new MultiWorker()).build();\n        final Service service3 = serviceBuilder.setServiceObject(new MultiWorker()).build();\n```\n\nNow add them to a ServiceWorkers object.\n\n```java\n\n        ServiceWorkers dispatcher;\n        dispatcher = workers(); //Create a round robin service dispatcher\n        dispatcher.addServices(service1, service2, service3);\n        dispatcher.start(); // start up the workers\n\n```\n\nYou can add services, POJOs and method consumers, method dispatchers to a service bundle.\nThe service bundle is an integration point into QBit.\n\nLet's add our new Service workers. ServiceWorkers is a ServiceMethodDispatcher.\n\n```Java\n        /* Add the dispatcher to a service bundle. */\n        bundle = serviceBundleBuilder().setAddress(\"/root\").build();\n        bundle.addServiceConsumer(\"/workers\", dispatcher);\n        bundle.start();\n```\n\nWe are probably going to add a helper method to the service bundle so most of this can happen in\na single call.\n\nNow you can start using your workers.\n\n```java\n\n        /* Start using the workers. */\n        final MultiWorkerClient worker = bundle.createLocalProxy(MultiWorkerClient.class, \"/workers\");\n\n```\n\nNow you could use Spring or Guice to configure the builders and the service bundle.\nBut you can just do it like the above which is good for testing and understanding QBit internals.\n\nQBit also supports the concept of sharded services which is good for sharding resources like CPU\n(run a rules engine on each CPU core for a user recommendation engine).\n\nQBit does not know how to shard your services, you have to give it a hint.\nYou do this through a shard rule.\n\n```java\npublic interface ShardRule {\n    int shard(String methodName, Object[] args, int numWorkers);\n}\n```\nWe worked on an app where the first argument to the services was the username, and then we used that to\nshard calls to a CPU intensive in-memory rules engine. This technique works. :)\n\nThe ServiceWorkers class has a method for creating a sharded worker pool.\n\n```java\n\n    public static ShardedMethodDispatcher shardedWorkers(final ShardRule shardRule) {\n        ...\n    }\n\n```\n\nTo use you just pass a shard key when you create the service workers.\n\n```java\n\n\n        dispatcher = shardedWorkers((methodName, methodArgs, numWorkers) -\u003e {\n            String userName = methodArgs[0].toString();\n            int shardKey =  userName.hashCode() % numWorkers;\n            return shardKey;\n        });\n\n```\n\nThen add your services to the ServiceWorkers composition.\n```java\n        int workerCount = Runtime.getRuntime().availableProcessors();\n\n        for (int index = 0; index \u003c workerCount; index++) {\n            final Service service = serviceBuilder\n                    .setServiceObject(new ContentRulesEngine()).build();\n            dispatcher.addServices(service);\n\n        }\n```\n\nThen add it to the service bundle as before.\n```java\n\n        dispatcher.start();\n\n        bundle = serviceBundleBuilder().setAddress(\"/root\").build();\n\n        bundle.addServiceConsumer(\"/workers\", dispatcher);\n        bundle.start();\n```\n\nThen just use it:\n\n```java\n        final MultiWorkerClient worker = bundle.createLocalProxy(MultiWorkerClient.class, \"/workers\");\n\n        for (int index = 0; index \u003c 100; index++) {\n            String userName = \"rickhigh\" + index;\n            worker.pickSuggestions(userName);\n        }\n\n```\n\n### Built in shard rules\n\n```java\n\n\npublic class ServiceWorkers {\n...\n    public static ShardedMethodDispatcher shardOnFirstArgumentWorkers() {\n       ...\n    }\n\n...\n\n    public static ShardedMethodDispatcher shardOnFifthArgumentWorkers() {\n         ...\n    }\n\n\n    public static ShardedMethodDispatcher shardOnBeanPath(final String beanPath) {\n        ...\n    }\n\n```\n\nThe shardOnBeanPath allows you to create a complex bean path navigation call and use its property to shard on.\n\n```java\n\n     /* shard on 2nd arg which is an employee\n       Use the employees department's id property. */\n     dispatcher = shardOnBeanPath(\"[1].department.id\");\n\n     /* Same as above. */\n     dispatcher = shardOnBeanPath(\"1/department/id\");\n\n```\n\n[Read more about Service sharding and service workers here](https://github.com/advantageous/qbit/wiki/%5BRough-Cut%5D-QBit-Microservices-using-Service-Workers-and-sharded-service-workers)\n\n\nYou can find a lot more in the wiki. Also follow the commits.\nWe have been busy beavers.\n[QBit the microservice lib for Java - JSON, REST, WebSocket](https://github.com/advantageous/qbit/wiki).\n","funding_links":[],"categories":["Dependency Injection","微服务库","websocket","Service Toolkits"],"sub_categories":["Java VM"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadvantageous%2Fqbit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadvantageous%2Fqbit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadvantageous%2Fqbit/lists"}