gpt4 book ai didi

java - Vert.x 如何将消息从 REST 传递/获取到消息总线?

转载 作者:行者123 更新时间:2023-11-29 04:44:18 25 4
gpt4 key购买 nike

我想通过 REST 将消息传递给总线,然后取回它。但我无法正确设置消息总线接收器,它抛出 java.lang.IllegalStateException: Response has already been written。在现实生活中,消息总线应该从不同的来源接收消息并将消息传递给另一个目标。因此我们只需要将消息发布到总线上。但是如何正确读取消息并处理所有消息呢?例如,从 REST 界面:阅读该消息!我的简单应用启动:

 public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new RESTVerticle());
vertx.deployVerticle(new Receiver());
EventBus eventBus = vertx.eventBus();
eventBus.registerDefaultCodec(MessageDTO.class, new CustomMessageCodec());

}

休息部分

public class RESTVerticle extends AbstractVerticle {

private EventBus eventBus = null;

@Override
public void start() throws Exception {
Router router = Router.router(vertx);
eventBus = vertx.eventBus();
router.route().handler(BodyHandler.create());
router.route().handler(CorsHandler.create("*")
.allowedMethod(HttpMethod.GET)
.allowedHeader("Content-Type"));

router.post("/api/message").handler(this::publishToEventBus);
// router.get("/api/messagelist").handler(this::getMessagesFromBus);

router.route("/*").handler(StaticHandler.create());
vertx.createHttpServer().requestHandler(router::accept).listen(9999);
System.out.println("Service running at 0.0.0.0:9999");

}

private void publishToEventBus(RoutingContext routingContext) {
System.out.println("routingContext.getBodyAsString() " + routingContext.getBodyAsString());
final MessageDTO message = Json.decodeValue(routingContext.getBodyAsString(),
MessageDTO.class);

HttpServerResponse response = routingContext.response();
response.setStatusCode(201)
.putHeader("content-type", "application/json; charset=utf-8")
.end(Json.encodePrettily(message));

eventBus.publish("messagesBus", message);

}

接收者:我把它移到另一个类,但这没有帮助

public class Receiver extends AbstractVerticle {

@Override
public void start() throws Exception {
EventBus eventBus = vertx.eventBus();
Router router = Router.router(vertx);

router.route().handler(BodyHandler.create());
router.route().handler(CorsHandler.create("*")
.allowedMethod(HttpMethod.GET)
.allowedHeader("Content-Type"));

router.get("/api/messagelist").handler(this::getMessagesFromBus);
router.route("/*").handler(StaticHandler.create());

vertx.createHttpServer().requestHandler(router::accept).listen(9998);
System.out.println("Service Receiver running at 0.0.0.0:9998");

private void getMessagesFromBus(RoutingContext routingContext) {
EventBus eventBus = vertx.eventBus();
eventBus.consumer("messagesBus", message -> {
MessageDTO customMessage = (MessageDTO) message.body();
HttpServerResponse response = routingContext.response();
System.out.println("Receiver ->>>>>>>> " + customMessage);
if (customMessage != null) {
response.putHeader("content-type", "application/json; charset=utf-8")
.end(Json.encodePrettily(customMessage));
}
response.closed();

});
}

因此,如果我将消息发布到 REST 并且处理程序将其发布到总线,当我在运行时得到 http://localhost:9998/api/messagelist是返回json,但是第二次抛出异常

java.lang.IllegalStateException: Response has already been written
at io.vertx.core.http.impl.HttpServerResponseImpl.checkWritten(HttpServerResponseImpl.java:561)
at io.vertx.core.http.impl.HttpServerResponseImpl.putHeader(HttpServerResponseImpl.java:154)
at io.vertx.core.http.impl.HttpServerResponseImpl.putHeader(HttpServerResponseImpl.java:52)
at com.project.backend.Receiver.lambda$getMessagesFromBus$0(Receiver.java:55)
at io.vertx.core.eventbus.impl.HandlerRegistration.handleMessage(HandlerRegistration.java:207)
at io.vertx.core.eventbus.impl.HandlerRegistration.handle(HandlerRegistration.java:201)
at io.vertx.core.eventbus.impl.EventBusImpl.lambda$deliverToHandler$127(EventBusImpl.java:498)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$18(ContextImpl.java:335)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:358)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
at java.lang.Thread.run(Thread.java:745)

Receiver ->>>>>>>> Message{username=Aaaewfewf2d, message=41414wefwef2d2}

如何正确获取接收方的所有消息?或者如果总线收到消息,我应该立即将它们存储到数据库中吗?消息总线能否保留消息而不丢失它们?

谢谢

最佳答案

每次点击入口点“/api/messagelist”都会创建一个具有请求路由上下文的新消费者。

第一个请求将创建消费者并回复请求。当第二条消息发布时,该消费者将收到该消息并将回复先前的请求(实例)并且该请求已关闭。

我认为您误解了事件总线的用途,我真的建议您阅读文档。 http://vertx.io/docs/vertx-core/java/#event_bus

关于java - Vert.x 如何将消息从 REST 传递/获取到消息总线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37897876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com