Last active
September 14, 2015 07:30
-
-
Save cdjones32/fc3b7ea8ebc009f5a92f to your computer and use it in GitHub Desktop.
Example of how to fix 'java.lang.IllegalStateException: Request has already been read' exceptions when using ClusteredSessionStore. For discussion: https://groups.google.com/forum/#!topic/vertx/928XJiz1wbI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.vertx.core.AbstractVerticle; | |
import io.vertx.core.Vertx; | |
import io.vertx.core.VertxOptions; | |
import io.vertx.core.http.HttpServerRequest; | |
import io.vertx.ext.web.Router; | |
import io.vertx.ext.web.handler.CookieHandler; | |
import io.vertx.ext.web.handler.SessionHandler; | |
import io.vertx.ext.web.sstore.ClusteredSessionStore; | |
/* | |
* Related to the discussion here: https://groups.google.com/forum/#!topic/vertx/928XJiz1wbI | |
*/ | |
public class ClusteredSessionReadException extends AbstractVerticle { | |
// Convenience method so you can run it in your IDE | |
public static void main(String[] args) { | |
Vertx.clusteredVertx(new VertxOptions().setClustered(true), res -> { | |
res.result().deployVerticle(new ClusteredSessionReadException()); | |
}); | |
} | |
@Override | |
public void start() throws Exception { | |
Router router = Router.router(vertx); | |
router.route().handler(CookieHandler.create()); | |
// CHANGE START - Mark the request as paused so the Vertx ServerConnection doesn't mark the request as ended | |
router.route().handler( routingContext -> { | |
routingContext.request().pause(); | |
routingContext.next(); | |
}); | |
// CHANGE END | |
router.route().handler(SessionHandler.create(ClusteredSessionStore.create(vertx))); | |
router.route().handler(routingContext -> { | |
HttpServerRequest req = routingContext.request(); | |
req.endHandler(aVoid -> { | |
req.response().end("finished"); | |
}); | |
// CHANGE - Resume the request once your handlers are set up | |
routingContext.request().resume(); | |
}); | |
vertx.createHttpServer().requestHandler(router::accept).listen(8080); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment