public class RpcServerExample {
public static void main(String[] args) throws Exception {
Eventloop eventloop = Eventloop.create();
RpcServer server = RpcServer.create(eventloop)
.withMessageTypes(Request.class, Response.class)
.withHandler(Request.class, (request, responseConsumer) -> {
System.out.println("Received request: " + request);
responseConsumer.accept(new Response("Hello, " + request.getName() + "!"));
})
.withListenPort(8080);
server.listen();
eventloop.run();
}
}
public class RpcClientExample {
public static void main(String[] args) throws IOException {
RpcClient client = RpcClient.create(Eventloop.getCurrentEventloop())
.withMessageTypes(Request.class, Response.class)
.withAddress(InetAddress.getLocalHost(), 8080);
Request request = new Request("Alice");
Promise<Response> promise = client.sendRequest(request);
promise.whenComplete((response, exception) -> {
if (exception == null) {
System.out.println("Response received: " + response);
} else {
System.err.println("Error occurred: " + exception.getMessage());
}
});
}
}
RpcServer server = RpcServer.create(eventloop)
.withMessageTypes(Request.class, Response.class)
.withHandler(Request.class, (request, responseConsumer) -> {
})
.withListenPort(8080)
.withSerializer(MessageSerializerBuilder.create(JsonMessageSerializer.class)
.withCompression(true)
.build())
.withProtocol(RpcProtocolFactory.ofBinary(protocolConfigurator))
.withExecutorsStrategy(ExecutorsStrategy.CACHED_EXECUTOR);