Using existing or exposing Ratpack's Guice injector

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

Using existing or exposing Ratpack's Guice injector

rehevkor5
I'm adding ratpack to a pre-existing app that uses a single instance of a class for access to a data store (multiple instances cannot be created due to locks on the data store).

The problem I'm having is that I can't make Ratpack use the same Guice injector as the rest of the app, so that there is only one instance of my @Singleton class. Ratpack creates its own injector, and I can't get access to it so that I can use the same instances in the rest of the app.

I was thinking that I could give Ratpack my injector to use, but the only means I have of putting things into Ratpack are: the handler factory classname, and the "others" property of the LaunchConfig (Strings only). As for retrieving Ratpack's injector so I can use it, the only means I have of getting things from Ratpack is: RatpackServer#getLaunchConfig()... which is fairly useless for finding arbitrary data.

I am thinking that perhaps I can create a custom implementation of the GuiceBackedHandlerFactory, so that it can use a pre-existing Guice injector. However, I'm not sure how to pass the injector into it aside from using a static singleton method (which I don't want to do). Also, in the default impelmentation of GuiceBackedHandlerFactory, there's some things going on with module registries and so forth and I'm not sure what to do about that...

Does anyone have an idea what the easiest/best way to accomplish my goal would be?
Reply | Threaded
Open this post in threaded view
|

Re: Using existing or exposing Ratpack's Guice injector

Marcin Erdmann
How are you using ratpack? With the Java DSL? Do you already have a Guice module that provides your singleton? If so, you could register it in a similar way as HandlebarsModule is registered in the javadoc for this class: http://www.ratpack-framework.org/manual/current/api/org/ratpackframework/handlebars/HandlebarsModule.html. The examples in that javadoc are available for both Groovy and Java DSL.
Reply | Threaded
Open this post in threaded view
|

Re: Using existing or exposing Ratpack's Guice injector

Luke Daley
Administrator
In reply to this post by rehevkor5
We could add methods to the Ratpack Guice class that use a parent injector (https://github.com/ratpack/ratpack/issues/142). Then you would have to do your own bootstrapping, as the RatpackMain entry points won't work for you.

You'd have to use the LaunchConfigBuilder to build a launch config (with your own HandlerFactory implemenation), then build a server using RatpackServerBuilder. All pretty easy.

What kind of app is this? Groovy or Java? Getting this working with the Groovy script app mode (i.e. reads ratpack.groovy) is going to be more work.
Reply | Threaded
Open this post in threaded view
|

Re: Using existing or exposing Ratpack's Guice injector

Luke Daley
Administrator
In reply to this post by rehevkor5
I've added the necessary methods to make it possible to use a parent injector for a ratpack-guice app.

https://github.com/ratpack/ratpack/issues/142
Reply | Threaded
Open this post in threaded view
|

Re: Using existing or exposing Ratpack's Guice injector

rehevkor5
@Marcin yeah that's pretty much what I was using. I had no problem getting ratpack to use my module, the problem was getting it to use the same injector.

@Luke sorry for the delayed response: that looks perfect, thanks for the help! I am using ratpack via Java because I didn't want to add Groovy to this particular application, so that's no problem. I am looking forward to your first releast of 0.9 so that I don't have to lock my app to snapshot versions any more!
Reply | Threaded
Open this post in threaded view
|

Re: Using existing or exposing Ratpack's Guice injector

Luke Daley
Administrator
I'm glad to hear that people are using Ratpack from Java :)

Is this app open source? Can I take a look?

We are zeroing in on a stable release. Our biggest blocker is good enough docs. Always looking for contributions :)