Support Java pojo arguments and return value.

This commit is contained in:
Qing Wang
2020-10-23 16:39:10 +08:00
parent 4530dc7cae
commit 0e6689d53b
4 changed files with 112 additions and 10 deletions
+5
View File
@@ -34,6 +34,11 @@
<artifactId>msgpack-core</artifactId>
<version>0.8.21</version>
</dependency>
<dependency>
<groupId>org.msgpack</groupId>
<artifactId>jackson-dataformat-msgpack</artifactId>
<version>0.8.21</version>
</dependency>
</dependencies>
<build>
<resources>
@@ -1,11 +1,14 @@
package org.restrpc.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.msgpack.core.MessageBufferPacker;
import org.msgpack.core.MessagePack;
import org.msgpack.core.MessageUnpacker;
import java.awt.print.PrinterGraphics;
import org.msgpack.jackson.dataformat.JsonArrayFormat;
import org.msgpack.jackson.dataformat.MessagePackFactory;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
public class Codec {
@@ -39,13 +42,15 @@ public class Codec {
messagePacker.packString((String) arg);
break;
default:
throw new RuntimeException("Unknown type: " + argTypeName);
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
objectMapper.setAnnotationIntrospector(new JsonArrayFormat());
messagePacker.writePayload(objectMapper.writeValueAsBytes(arg));
}
}
return messagePacker.toByteArray();
}
public Object decodeReturnValue(Class returnClz, byte[] encodedBytes) throws IOException {
public Object decodeReturnValue(Class<?> returnClz, byte[] encodedBytes) throws IOException {
if (returnClz == null) {
throw new RuntimeException("Internal bug.");
}
@@ -66,7 +71,12 @@ public class Codec {
return messageUnpacker.unpackLong();
} else if (String.class.equals(returnClz)) {
return messageUnpacker.unpackString();
} else {
final int remainder = (int) (encodedBytes.length - messageUnpacker.getTotalReadBytes());
byte[] payload = messageUnpacker.readPayload(remainder);
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
objectMapper.setAnnotationIntrospector(new JsonArrayFormat());
return objectMapper.readValue(payload, returnClz);
}
throw new RuntimeException("Unknown type: " + returnClz);
}
}
@@ -0,0 +1,81 @@
package org.restrpc.examples;
import org.restrpc.client.NativeRpcClient;
import org.restrpc.client.RpcClient;
import java.io.Serializable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
class Person implements Serializable {
private static final long serialVersionUID = 5989656920203532884L;
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person {" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class PassPojoAsArgumentExample {
public static void main(String[] args) throws InterruptedException, ExecutionException {
/**
* An example shows how we use this Java client to connect to
* the C++ RPC server and invoke the C++ RPC methods.
*
* First of all, we should run a C++ rpc server. In this example,
* we first run the `basic_server` which is written here:
* https://github.com/qicosmos/rest_rpc/blob/master/examples/server/main.cpp
*/
RpcClient rpcClient = new NativeRpcClient();
rpcClient.connect("127.0.0.1:9000");
{
Person p = new Person();
p.setId(10001);
p.setName("Jack");
p.setAge(22);
CompletableFuture<?> future = rpcClient.asyncFunc("get_person_name").invoke(String.class, p);
System.out.println("The result of get_person_name() is " + future.get());
}
{
CompletableFuture<?> future = rpcClient.asyncFunc("get_person").invoke(Person.class);
System.out.println("The result of get_person() is " + future.get());
}
rpcClient.close();
}
}