Skip to content

Instantly share code, notes, and snippets.

@makotan
Forked from anonymous/HttpRestVerticle1.java
Created June 14, 2012 09:01
Show Gist options
  • Save makotan/2929185 to your computer and use it in GitHub Desktop.
Save makotan/2929185 to your computer and use it in GitHub Desktop.
HttpRestVerticle1 long polling
import java.text.SimpleDateFormat;
import java.util.Date;
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServer;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.http.RouteMatcher;
import org.vertx.java.deploy.Verticle;
public class HttpRestVerticle1 extends Verticle {
public final static String DBGKEY = "[" + HttpRestVerticle1.class.getSimpleName() + "_"
+ Long.toHexString(Double.doubleToLongBits(Math.random())) + "]";
HttpServer server;
public static String getNow() {
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//dd/MM/yyyy
Date now = new Date();
String strDate = sdfDate.format(now);
return strDate;
}
@Override
public void start() throws Exception {
System.out.println(DBGKEY + ": start()");
server = vertx.createHttpServer();
RouteMatcher routeMatcher = new RouteMatcher();
routeMatcher.get("/tests/longpollingtimeoutmeasure", new Handler<HttpServerRequest>() {
@Override
public void handle(final HttpServerRequest req) {
System.out.println(DBGKEY + ": longpolling1 - got client - #req=" + req + " - " + getNow());
req.exceptionHandler(new Handler<Exception>() {
@Override
public void handle(Exception event) {
System.out.println("Got Exception: " + event.getMessage() + " - " + getNow());
}
});
req.response.exceptionHandler(new Handler<Exception>() {
@Override
public void handle(Exception event) {
System.out.println("Got Exception2: " + event.getMessage() + " - " + getNow());
}
});
req.endHandler(new Handler<Void>() {
public void handle(Void event) {
System.out.println("endHandler: " + " - " + getNow());
}
});
}
});
routeMatcher.noMatch(new Handler<HttpServerRequest>() {
@Override
public void handle(HttpServerRequest req) {
req.response.end("Not found");
}
});
server.requestHandler(routeMatcher).listen(80, "localhost");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment