To run the examples, you need to clone ActiveJ project from GitHub:
$ git clone https://github.com/activej/activej And import it as a Maven project. Check out tag v4.3. Before running the example, build the project.
Simple RPC Example is located at activej -> examples -> cloud -> rpc.
RPC strategies examples are located at activej -> cloud-rpc -> src -> test -> RpcStrategiesTest.
Simple RPC Example
In the “Hello World” client and server RPC Example, the client sends a request which contains the word "World" to server. When
server receives it, it sends a response that contains the word "Hello ". If everything completes successfully, we get the
Let’s take a look at the implementation:
RpcExample class extends ActiveJ Launcher to help us manage application
Next, we use Dependency Injection library ActiveJ Inject to provide RpcServer
with relevant configurations and required dependencies. RpcClient sends requests with a String message to the
specified server according to the provided RPC strategy
(getting a single RPC-service).
For RpcServer we define the type of messages to proceed, a corresponding RpcRequestHandler and a listener port.
Since we extend Launcher, we will also override 2 methods: getModule to provide ServiceGraphModule
and run to describe the main logic of the example.
Finally, we define the main method, which will launch our example.
ActiveJ RPC contains pre-defined strategies for requests arrangement between RPC servers or shards of servers.
Round-Robin is one of the simplest of strategies: it just goes through the servers or shards in a cyclic way one by
In this example we create an RPC pool with 5 equal connections and set Round-Robin
strategy for them. Next, we create a sender for the pool with the previously defined strategy. That’s it, 100
requests will be equally distributed between the servers:
Round-Robin and First Available Strategies Combined
You can simply combine RPC strategies. In this example we will combine Round Robin
and First Available strategies.
First, we create 4 connections without putting connection3 into the pool. Then we start sending 20 requests.
As a result, all the requests will be equally distributed between connection1 (as it is always first available) and
connection4 (as connection3 isn’t available for the pool):
You can also create your own sharding functions and combine them with other strategies if needed. In this example we
create 5 equal connections but don’t put connection2 into the pool. Next, we provide a simple sharding function which
distributes requests between shards in accordance to the content of the request.
We split the connections into two shards,
and set First Valid Result
strategy for both of them. This strategy sends request to all available servers.
Now, we manually send 7 requests:
4 with 0 message, so they’ll be sent to the first shard’s connection1
3 with 1, so they’ll all be sent to all three connections of the second shard
This strategy simply distributes requests among shards in accordance to the type of the request. In the example
all String requests are sent on the first shard which has First Valid Result strategy for the servers. Requests
with all other types are sent to the second shard with First Available strategy. As a result, connection1 and
connection2 will process 35 requests, connection3 - 25 requests, while connection4 and connection5 - 0 requests
as connection3 was always First Available: