Guice and VertX injectable request context available throught the application

Viewed 593

I have been trying to learn ins and outs of VertX Web framework for the past couple of months. I have however came across a problem which I thought would be easily solvable - but to this day I haven't been able to find the solution.

So my problem is: I would like to have a web request scoped context to be injectable through out the application.

What I tried doing is something like this:

router.route("...").handler(RoutingContext ctx -> {
    val requestContext = new AppContext(UUID.randomUUID());
    val scope = injector.getInstance(CustomScope.class);
    scope.enter();
    scope.seed(IAppContext.class, requestContext);

    ctx.next();
});

router.route("...").handler(RoutingContext ctx -> {
    // actual code

    ctx.next();
});

router.route("...").handler(RoutingContext ctx -> {
    val scope = injector.getInstance(CustomScope.class);
    scope.exit();

    ctx.response().end();
});

I do have a feeling that current solution won't work with multiple requests comming in at the same time. Because VertX runs on a single thread I have to finish the scope before I could enter a new one so this solution is not ok for now.

Guice custom scope is the same as this one so if I can fix this in this custom scope code it would be just perfect. I don't know much about these custom scopes so I'm kinda lost on this front...

I would just like to have some request storage somewhere without having to pass it as a parameter to methods all over the application code. I've search around for some way to statically access RoutingContext but no luck for now.

0 Answers
Related