Making Class Mapped by Guice Module Available

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

Making Class Mapped by Guice Module Available

Tim Stearn
Hi All,

I just got started with Ratpack about a week ago and just got started with Guice last night after doing Spring for years, so sorry if this question is very basic.  I looked through all the threads on Guice and couldn't find an  answer.

I've written a Guice module for a DAO and confirmed with unit tests that this is functional.  In the unit tests, of course, I'm manually creating the injector and then retrieving the impl I need using it, like so:

    def injector = Guice.createInjector(new UserModule())
    UserDAO dao = injector.getInstance(UserDAO.class)

Now, I'm trying to make UserDAO available to my handlers using the Groovy DSL.  After reading the doc and several websites, I've setup my very simple Ratpack.groovy like so:

ratpack {
    bindings {
        add new JacksonModule()
        add new UserModule()
    }

    handlers { UserDAO dao ->
        get {
            render(file('public/index.html'))
        }
        prefix('api/user') {
            get(':email'){
                String email = pathTokens.email
                render(json(dao.findByEmail(email)))
            }
        }

        assets "public"
  }
}

When I run with this configuration, I get the following exception:
    ratpack.registry.NotInRegistryException: No object for type 'com.stepchat.db.UserDAO' in registry

In reading the docs, it doesn't seem like I supposed to create the injector and call getInstance() on my own - I thought this is what the Ratpack Guice integration did.

Please let me know what I'm missing and if you could point me to an example.  I've paste the code for UserModule and the constructor for UserDAOImpl below.

Thanks,

Tim Stearn

Code:

class UserModule extends AbstractModule {
    Map settings;

    UserModule(){
        settings = SettingsUtil.loadSettings()
    }

    @Override
    protected void configure() {
        bind(UserDAO.class).to(UserDAOImpl.class).in(Scopes.SINGLETON)
    }


    @Provides
    @Singleton
    DB getAppDatabase() {
        GMongo mongo = createMongoConnection()
        return mongo.getDB(settings.mongoConnection.databaseName)
    }

    GMongo createMongoConnection(){
        return new GMongo(settings.mongoConnection.host as String,
                          settings.mongoConnection.port as Integer)
    }

}

class UserDAOImpl implements UserDAO {
    static COLLECTION_NAME = 'users'
    private DB db
    private DBCollection collection


    @Inject
    UserDAOImpl(DB db) {
        this.db = db
        collection = db.getCollection(COLLECTION_NAME);
    }
    ...
}
Reply | Threaded
Open this post in threaded view
|

Re: Making Class Mapped by Guice Module Available

Dan Woods
Thank you for the post, Tim. Fear not -- no question is too basic :-)

From a quick glance at your example code, I think the problem is just from where you're trying to inject the UserDAO. Try from the handler:

handlers {
  prefix("api/user") {
    get { UserDAO dao ->
      String email = pathTokens.email
      render(json(dao.findByEmail(email)))
    }
  }
}
rus
Reply | Threaded
Open this post in threaded view
|

Re: Making Class Mapped by Guice Module Available

rus
I'm pretty sure you can inject at the top level `handlers` as you're doing.  What version of Ratpack are you using?  If it's a recent version the `add new UserModule() ` should now be `module new UserModule() `.  Likewise for JacksonModule.

BTW, don't forget that GMongo is not non-blocking.  It's using the blocking 2.x version of the Java driver.  Therefore you should be doing the I/O work on a blocking thread.  I've been recently looking at the version 3 async driver and the RxJava driver api that sits on top of it.  So far working out pretty sweet http://mongodb.github.io/mongo-java-driver-rx/
Reply | Threaded
Open this post in threaded view
|

Re: Making Class Mapped by Guice Module Available

Tim Stearn

I'm using  0.9.17​.  The way I was declaring the modules was the problem.  Thanks!


Tim Stearn
Principal Solutions Architect
SAS Institute Inc
(919)531-9332 (o)
(412)418-2688 (m)
[hidden email]
www.sas.com


From: rus [via Ratpack Forum] <ml-node+[hidden email]>
Sent: Sunday, June 7, 2015 2:57 PM
To: Tim Stearn
Subject: Re: Making Class Mapped by Guice Module Available
 
I'm pretty sure you can inject at the top level `handlers` as you're doing.  What version of Ratpack are you using?  If it's a recent version the `add new UserModule() ` should now be `module new UserModule() `.  Likewise for JacksonModule.

BTW, don't forget that GMongo is not non-blocking.  It's using the blocking 2.x version of the Java driver.  Therefore you should be doing the I/O work on a blocking thread.  I've been recently looking at the version 3 async driver and the RxJava driver api that sits on top of it.  So far working out pretty sweet http://mongodb.github.io/mongo-java-driver-rx/



If you reply to this email, your message will be added to the discussion below:
http://forum.ratpack.io/Making-Class-Mapped-by-Guice-Module-Available-tp1098p1100.html
To unsubscribe from Making Class Mapped by Guice Module Available, click here.
NAML
rus
Reply | Threaded
Open this post in threaded view
|

Re: Making Class Mapped by Guice Module Available

rus
Great. I notice you have a SettingsUtil too, you might want to look at ConfigurableModule instead.  If you do and get stuck, don't hesitate to post back
Reply | Threaded
Open this post in threaded view
|

Re: Making Class Mapped by Guice Module Available

Luke Daley
Administrator
In reply to this post by Dan Woods

For the Groovy DSL, we do support closure params at both the chain level and the handler level.

Chain level:

handlers { SomeThing thing ->
  get {
    //
  }
}

Handler level:

handlers {
  get { SomeThing thing ->
    //
  }
}

The difference is that for the chain level, the source of “things” is the server registry. For the handler level, it’s the context registry (which is a super set of the server registry).