How to response http request from eventbus consumer.

564 views
Skip to first unread message

蓝鲸鱼

unread,
Sep 19, 2016, 1:10:20 AM9/19/16
to vert.x

i have a verticle that handler the socket write and response.

public void start() {   // 1
    NetClient netClient = vertx.createNetClient();
    netClient.connect(port, host, ar -> {
        socket = ar.result(); // NetSocket
        socket.handler(this::doSocketHandleMethod);
        socket.write(BYTEBUFFER);// buffer here
    })
}

private void doSocketHandleMethod(Buffer buffer){ // socket handler 
    // process data here and send
    vertx.eventBus().send(ADDRESS, data here);
}

Then i use a verticle to response for the http request.

public void start() {
    Router router = Router.router(vertx);
    router.route(API_GET).handler(this::getData);

    vertx.eventBus().consumer(ADDRESSHERE, msg -> {
        // get data from the socket send.    2
    });
         vertx.createHttpServer().requestHandler(router::accept).listen(8080, result -> {
            // 3 
        });
}

private void getData(RoutingContext routingContext) {
    vertx.eventBus().send(ADDRESS, message); // send message to the top // 1 verticle

}

The question is the 2 can get data, but i don't know how to response the data to the http reqest 3.

ad...@cs.miami.edu

unread,
Sep 19, 2016, 1:54:54 AM9/19/16
to vert.x
Hi,

I don't exactly understand your question, but it seems that part of your question is sending data back from an http request.  Here is a full example in JS of creating an http server, a route, and sending data back to the client.  Maybe this will help out a bit.



var Router = require("vertx-web-js/router");
var server = vertx.createHttpServer({});
var router = Router.router(vertx);


router
.route("/test/*").handler(function (routingContext) {
   
var response = routingContext.response();
    response
.putHeader("content-type", "text/html");
    response
.end("hello world");
});

server
.requestHandler(router.accept).listen(8080);

蓝鲸鱼

unread,
Sep 19, 2016, 2:00:39 AM9/19/16
to vert.x
Sorry for that. 
I want response a http request form a eventbus.consumer.
Because the netsocke handler can not return value(Future).
So, the  eventbus consumer  get the value from the socke handler.
And i want the value publish restful throught the http server.

在 2016年9月19日星期一 UTC+8下午1:54:54,ad...@cs.miami.edu写道:

ad...@cs.miami.edu

unread,
Sep 19, 2016, 4:46:04 PM9/19/16
to vert.x
Hi,

I am still a bit confused on your question.

It is not clear to me what type of relationship you want between the Eentbus, httpserver and netclient?   Are you using the netclient to connect to the httpserver that is in a separate veitcle?

If you want try to talk out the entire process.. something like (note this is just made up, not the real thing):
1) Request comes into http server
2) pass request parameters as a message along the eveentbus
3) handle that message in a separate verticle, and create a reesponse answer
4) pass response answer back along the eventbus
5) push the response answer back as a response to the originating htttp request.

-Adam

蓝鲸鱼

unread,
Sep 19, 2016, 9:36:35 PM9/19/16
to vert.x
That is what i need.
1) Request comes into http server
2) pass request parameters as a message along the eveentbus
3) handle that message in a separate verticle, and create a reesponse answer 
4) pass response answer back along the eventbus     (because it's a socket to another server and netsocket use handler, so response along the eventbus.)
5) push the response answer back as a response to the originating htttp request.  (this is my confusing thing.  the eventbus.consumer handler the response, 
  the eventbus and the httpserver are seprate).

vertx.eventBus().consumer("", msg -> {

   // handle socket response here

});


// http routingContext here.


They are seprate.






在 2016年9月20日星期二 UTC+8上午4:46:04,ad...@cs.miami.edu写道:

lukjel

unread,
Sep 20, 2016, 2:33:41 AM9/20/16
to vert.x
Quite simple:

        @Override

      public void start() throws Exception {

         HttpServer server = this.vertx.createHttpServer();

             Router router = Router.router(vertx);

          BodyHandler bodyHandler = BodyHandler.create();

        router.route().handler(bodyHandler);

           router.route("/path").handler(rc ->{

                   JsonObject parameters = rc.getBodyAsJson();

                    this.vertx.eventBus().<JsonObject>send("some.address", parameters, ar ->{

                              if (ar.succeeded()) {

                                  JsonObject result = ar.result().body();

                                rc.response().end(result.encode());

                            } else {

                                       // Do some fail - this is not good to return exception from server :D better  some  error  code

                                  // rc.fail(ar.cause());

                                rc.fail(-1);

                           }

                      });

            });

       }



Hope it helps

Reg.
Lukas.

蓝鲸鱼

unread,
Sep 20, 2016, 9:18:19 PM9/20/16
to vert.x
The socket handler has no result to return.
So 

this.vertx.eventBus().<JsonObject>send("some.address", parameters, ar ->{

                              if (ar.succeeded()) {

                                  JsonObject result = ar.result().body();

                                rc.response().end(result.encode());

                            } else {

                                       // Do some fail - this is not good to return exception from server :D better  some  error  code

                                  // rc.fail(ar.cause());

                                rc.fail(-1);

                           }

                      });


This can not get result from eventbus.consumer.

That is what i need.
1) Request comes into http server
2) pass request parameters as a message along the eveentbus
3) handle that message in a separate verticle, and create a reesponse answer 
4) pass response answer back along the eventbus     (because it's a socket to another server and netsocket use handler, so response along the eventbus.)
5) push the response answer back as a response to the originating htttp request.  (this is my confusing thing.  the eventbus.consumer handler the response, 
  the eventbus and the httpserver are seprate).

vertx.eventBus().consumer("", msg -> {

   // handle socket response here

});


// http routingContext here.


They are seprate.




在 2016年9月20日星期二 UTC+8下午2:33:41,lukjel写道:
Reply all
Reply to author
Forward
0 new messages