Java: Passing Arguments to a Timer-Handler

356 views
Skip to first unread message

Jonas Bandi

unread,
Dec 1, 2012, 7:27:10 PM12/1/12
to ve...@googlegroups.com
To show the non-blocking characteristics of Vertx I would like to simulate a long-running request.

I am wondering if the following code is correct:

public class Server extends Verticle {

    HttpServerRequest _req;

    public void start() {
        vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
            public void handle(HttpServerRequest req) {
                System.out.println("Got request: " + req.uri);
                System.out.println("Headers are: ");
                for (String key : req.headers().keySet()) {
                    System.out.println(key + ":" + req.headers().get(key));
                }
                _req = req;

                vertx.setTimer(5000, new Handler<Long>() {

                    @Override
                    public void handle(Long timerId) {
                        _req.response.headers().put("Content-Type", "text/html; charset=UTF-8");
                        _req.response.end("<html><body><h1>Hello from vert.x!</h1></body></html>");
                    }
                });

            }
        }).listen(8080);
    }

Is this the correct way to pass the request into the handler? I guess it is not... but how can I pass the request into the handler?

Tim Fox

unread,
Dec 2, 2012, 5:00:47 AM12/2/12
to ve...@googlegroups.com
This is more or less right. But you don't need a separate member
variable _req. Just make req final and you will be able to access it in
your handler.



>
> --
> You received this message because you are subscribed to the Google
> Groups "vert.x" group.
> To view this discussion on the web, visit
> https://groups.google.com/d/msg/vertx/-/DkwYCyY2CZ4J.
> To post to this group, send an email to ve...@googlegroups.com.
> To unsubscribe from this group, send email to
> vertx+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/vertx?hl=en-GB.


--
Tim Fox

Vert.x - effortless polyglot asynchronous application development
http://vertx.io
twitter:@timfox

Reply all
Reply to author
Forward
0 new messages