'modules' block in ratpack.groovy executing once per request

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

'modules' block in ratpack.groovy executing once per request

Neeraj
I notice that Modules that are registered via the 'modules' block in ratpack.groovy, get invoked each time a request is processed. Is this expected ? In the example below, the 'configure' method would be invoked at startup of the container and then for each subsequent request.

sample :

ratpack.groovy :
modules {
        register new BootStrapModule()
}

BootstrapModule.groovy:

class BootstrapModule extends AbstractModule {

    @Override
    protected void configure() {
        println 'inside boostrap configure'
    }
}

I am attempting to load and bind a few system/env properties into the guice injector at startup. My attempt to use modules closure does not work as expected. What would the right way to initialize the guice injector ?


Reply | Threaded
Open this post in threaded view
|

Re: 'modules' block in ratpack.groovy executing once per request

Marcin Erdmann

It's the configure method that gets invoked multiple times and not the modules closure - you're not calling configure() in modules closure but it's the framework doing it. I'm not sure that this is the real reason (Luke would need to confirm) but I think that this is because a registry/injector gets created per request? If you want to initialize your services registered with modules you can do so using an init { } inside modules { }. Have a look at the books example in ratpack org on github.

On Feb 2, 2014 12:48 AM, "Neeraj [via Ratpack Forum]" <[hidden email]> wrote:
I notice that Modules that are registered via the 'modules' block in ratpack.groovy, get invoked each time a request is processed. Is this expected ? In the example below, the 'configure' method would be invoked at startup of the container and then for each subsequent request.

sample :

ratpack.groovy :
modules {
        register new BootStrapModule()
}

BootstrapModule.groovy:

class BootstrapModule extends AbstractModule {

    @Override
    protected void configure() {
        println 'inside boostrap configure'
    }
}

I am attempting to load and bind a few system/env properties into the guice injector at startup. My attempt to use modules closure does not work as expected. What would the right way to initialize the guice injector ?





If you reply to this email, your message will be added to the discussion below:
http://forum.ratpack.io/modules-block-in-ratpack-groovy-executing-once-per-request-tp341.html
To start a new topic under Ratpack Forum, email [hidden email]
To unsubscribe from Ratpack Forum, click here.
NAML
Reply | Threaded
Open this post in threaded view
|

Re: 'modules' block in ratpack.groovy executing once per request

Neeraj
On further debugging, it turns out that if you have a block of 'register modules' and one of them encounters an error, the created injector is discarded. All subsequent requests will hence cause attempts to register all modules. In my case this was complicated by the fact that no stack trace showed up in the log.

Marcin, i tried the 'init' closure and yes that does seem to be a good alternative to execute one time initialization code. Thank you for the help.
Reply | Threaded
Open this post in threaded view
|

Re: 'modules' block in ratpack.groovy executing once per request

Luke Daley
Administrator
What was the nature of the error? It might be a bug that you weren't seeing a log message.