A simple RPC framework based on Lettuce for Java applications.
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.xcodiq</groupId>
<artifactId>simple-lettuce-rpc</artifactId>
<version>VERSION</version>
</dependency>
Put latest version thing here
In this example I'm implementing the RPC principle by letting a Client application request a Server application for specific data based on the provided record data.
public final class TestRecord extends Record<BytePacket, IntegerPacket> {
public TestRecord(int input) {
super(new BytePacket((byte) input));
}
}
public class TestRecordHandler extends RecordHandler<BytePacket, IntegerPacket> {
@Override
public IntegerPacket handlePacket(BytePacket packet) {
return new IntegerPacket(packet.getPayload() == 1
? Server.DATA_INTEGER_TRUE : Server.DATA_INTEGER_FALSE);
}
}
import com.xcodiq.rpc.Options;
public final class Server {
// data to share with clients
public static final int DATA_INTEGER_TRUE = 1337;
public static final int DATA_INTEGER_FALSE = 42069;
public Server() {
// Create a new RPC instance
final RPC<Server> rpc = new RPC<>(Server.this, Options.of("REDIS_URI", "TOPIC", "RECORD_PREFIX"));
// Get the record manager, and bind the record handler to the created record
final RecordManager recordManager = rpc.getRecordManager();
recordManager.bindRecordHandler(TestRecord.class, new TestRecordHandler());
}
}
public final class Client {
public Client() {
// Create a new RPC instance
final RPC<Client> rpc = new RPC<>(Client.this, Options.of("REDIS_URI", "TOPIC", "RECORD_PREFIX"));
// I used a completable future to make sure the data is received before the program continues
CompletableFuture<Integer> dataFuture = new CompletableFuture<>();
// Create the new record, and configure the timeout and reply consumers
final Record<BytePacket, IntegerPacket> testRecord = new TestRecord(0) //<- Input is 0 so Output Should be 42069
.onTimeout(bytePacket -> dataFuture.complete(-1))
.onReply(integerPacket -> dataFuture.complete(integerPacket.getPayload()));
// Set the timeout interval and publish it
testRecord.setTimeout(500, TimeUnit.MILLISECONDS).send(rpc.getRecordManager());
// When the data is received, print it
dataFuture.whenComplete((integer, throwable) -> System.out.println("Received data: " + integer));
}
}
This project is licensed under the MIT License