当我不熟悉Vertx时,我正在尝试使用Eventbus进行请求响应,但是在尝试将我卡在某个地方的同时,我做了什么:
EventBus bus = vertx.eventBus();
//Here I call the request
bus.<JsonObject>request("previewdata", m ,this::handle);
public void handle(AsyncResult<Message<JsonObject>> result) {
//request get fails before consumer don't send reply within 30 seconds
if(result.succeeded()){
System.out.println("Answer: "+Thread.currentThread().getName());
System.out.println(result.result().body());
} else{
result.cause().printStackTrace();
}
}
IDEng ie = new IDEng();
//Consumer of request
bus.<JsonObject>consumer("previewdata", this::getPreviewData);
public void getPreviewData(Message<JsonObject> message) {
JsonObject json = message.body();
for (int i=0; i<10000; i++) {
json.put("flag"+i, "got"+i);
}
try {
//Only for example I had put wait of 40 seconds (so it fail as greate than 30 seconds) it may take more time
Thread.sleep(40000);
}catch (Exception e) {
// TODO: handle exception
}
message.reply(json);
}
我已经使用Worker Verticle创建了一个vertx示例,并且我有一个请求和使用者,但是请求不是等待使用者回复响应,并且它在30秒内失败了,因为它的默认超时为30秒,但是按照我们的实际时间场景有时无法确定需要多少时间。因此,请帮助我如何等待回复。
我知道我们可以使用DeliveryOptions设置超时,但这不是正确的方法,因为如果它依赖于第三台服务器,我们就永远不会估计某些任务时间。 例如
new DeliveryOptions().setSendTimeout(50000)
请让我知道是否有人可以帮助我,或者我这样做的方式有误吗?
答案 0 :(得分:1)
如何将响应解耦到另一个总线地址,例如“ previewdataresponse
”:
Verticle A。)
bus.<JsonObject>request("previewdata", m ,this::handle);
// show error if reply is not success
bus.<JsonObject>consumer("previewdataresponse", this::handleResponse);
public void handleResponse(Message<JsonObject> message) {
// do something with the reponse
}
Verticle B。)
bus.<JsonObject>consumer("previewdata", this::getPreviewData);
public void getPreviewData(Message<JsonObject> message) {
JsonObject json = message.body();
//...
message.reply(json); // acknowledge that you received the message
try {
Thread.sleep(40000);
bus.<JsonObject>send("previewdataresponse", json);
}catch (Exception e) {
// TODO: handle exception
}
}