[英]What is the best way to send a collection of objects over the Event Bus in Vertx?
I have a handler that serves HTTP requests at a given endpoint.我有一个处理程序在给定端点处为 HTTP 请求提供服务。 The handler messages a verticle via the event bus that makes some external paginated REST calls, aggregates the results, and returns the results back to the handler.
处理程序通过事件总线向一个 verticle 发送消息,该事件总线进行一些外部分页 REST 调用,聚合结果,并将结果返回给处理程序。 The result of the paginated REST calls is represented as a List of custom objects.
分页 REST 调用的结果表示为自定义对象的列表。 If I just try to send the
List
itself, Vertx throws an exception complaining that it can't find a codec for java.util.ArrayList
.如果我只是尝试发送
List
本身,Vertx 会抛出一个异常,抱怨它找不到java.util.ArrayList
的编解码器。
I'm trying to find the "best" -- meaning the easiest, most efficient, and most readable/maintainable -- way in Vertx to send a list of these objects back across the event bus to my handler.我试图在 Vertx 中找到“最好的”——即最简单、最有效、最易读/可维护的方式——通过事件总线将这些对象的列表发送回我的处理程序。 These are the options I know of and have tried so far, are there better ways to achieve this?
这些是我所知道并尝试过的选项,有没有更好的方法来实现这一点?
JsonObject
.JsonObject
中。 This requires an explicit serialization/deserialization on either end which seems unnecessary:// Verticle
List<CustomObject> result = method();
JsonObject data = new JsonObject();
data.put("result", Json.encode(result));
msg.reply(data);
// Handler
String serializedList = body.getString("result");
List<CustomObject> list = objectMapper.readValue(serializedList, new TypeReference<List<CustomObject>>(){});
ArrayList<CustomObject>
.ArrayList<CustomObject>
定义消息编解码器。 In theory I believe this would work, but all the examples I've seen online for message codecs are always about creating a codec for a single object, and I'm not entirely sure if this would work for collections. Is there a simpler method that fits my use case that I'm unaware of?有没有更简单的方法适合我不知道的用例? Thanks!
谢谢!
Sorry for a lengthy example, but here you go:很抱歉举了一个冗长的例子,但这里是 go:
public class EventBusHolder {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.eventBus().registerDefaultCodec(Holder.class, new HolderCodec());
vertx.deployVerticle(new SomeVerticle(), (r) -> {
vertx.eventBus().send("custom", new Holder(new CustomObject("a")));
});
}
}
class HolderCodec implements MessageCodec<Holder, Holder> {
@Override
public void encodeToWire(Buffer buffer, Holder holder) {
}
@Override
public Holder decodeFromWire(int pos, Buffer buffer) {
return null;
}
@Override
public Holder transform(Holder holder) {
return holder;
}
@Override
public String name() {
return "HolderCodec";
}
@Override
public byte systemCodecID() {
return -1;
}
}
class SomeVerticle extends AbstractVerticle {
@Override
public void start() {
vertx.eventBus().consumer("custom", (msg) -> {
System.out.println(msg.body());
});
}
}
class CustomObject {
public String name;
public CustomObject(String name) {
this.name = name;
}
@Override
public String toString() {
return "CustomObject{" +
"name='" + name + '\'' +
'}';
}
}
final class Holder {
@Override
public String toString() {
return "Holder{" +
"data=" + data +
'}';
}
private final List<CustomObject> data;
public Holder(final CustomObject... data) {
this.data = Arrays.asList(data);
}
public List<CustomObject> getData() {
return data;
}
}
Take note that encodeToWire
and decodeFromWire
are not implemented.请注意,未实现
encodeToWire
和decodeFromWire
。 They aren't invoked for local messages.本地消息不会调用它们。
Having this Holder
object is an easy way to get around type erasure on the JVM.拥有此
Holder
object 是在 JVM 上绕过类型擦除的简单方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.