Dependency Injection and Nashorn

112 views
Skip to first unread message

Luis Rodrigues

unread,
May 29, 2017, 1:56:30 PM5/29/17
to Play Framework
Hi all,

With the previous global method using :
import play.db.DB;
DB.getConnection();

we could write a class that should be easily injected inside NashHorn and used by the server side javascript and accessible 
simply with something like this:

var DbFoo = Java.type('com.foo.bar.CreatedDBClass');
var db = new DbFoo();
var result = db.query("sql query");


But changing to play 2.6 with Dependency Injection, that will be a lot harder because we don't have a way to call the class with the required 
Database class from the javascript side.

Do you have any plan to workaround this ? or any suggestion ?


Thanks
-Luis

Luis Rodrigues

unread,
May 30, 2017, 3:50:02 AM5/30/17
to Play Framework
Currently I have a small workaround for this that is not super elegant and required creating objects to wrap the java classes in the javascript side, that inject the parameter [Database]  automatically in the java class and with this we are avoiding the user to set the parameter all the time (annoying) and to send the Database /Connection class to the javascript engine every time that we receive a request in the controller.

So,

I created a JSEngine service because we need to use hot executions and use the same engine all the time to use already "compiled" code ... :
@Singleton
public class JavascriptEngine implements JSEngine {
private Database db;
ScriptEngineManager factory;
ScriptEngine engine;

@Inject
public JavascriptEngine(Database db) {
this.db = db;
     ....


and 
Basically I'm setting a special variable inside the javascript engine every time that I receive a request in the controller and sending the Database object
to the javascript side/engine: (not perfect)

public class  ControllerA extends Controller {
     @Inject
JavascriptEngine jsengine;
....

     jsengine.addToEngine(db); // this basically calls the method (engine.put("DS_DATABASE",db);)


works fine, but I want to know if you have a better approach for this or any suggestion ?

Thanks
Regards
-Luis

Will Sargent

unread,
Jun 2, 2017, 7:47:01 AM6/2/17
to play-fr...@googlegroups.com
You can dependency inject the database instance directly, or (if you are really hard up and want the user to have unbounded visibility) you can inject the Injector itself -- at the point that you're calling "com.foo.bar.CreatedDBClass" you're delegating the lookups there anyway.  

The big problem is that doing database queries from Javascript inside of Play will pretty much require you do everything inside one big custom thread pool there, because you can't control the blocking operations inside that Javascript engine (or at least multi-threading was not a thing there the last time I looked).

--
Will Sargent
Engineer, Lightbend, Inc.


--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/307e7a33-8888-449e-8d53-945dd59ee2af%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Heidrich

unread,
Jun 10, 2017, 7:47:29 PM6/10/17
to Play Framework
Hey,

maybe the plugin I wrote to execute Javascript for play could be of help:

It also supports promises and async execution (it uses akka underneath)
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.

Will Sargent

unread,
Jun 12, 2017, 8:43:24 PM6/12/17
to play-fr...@googlegroups.com
Hi David,

This looks really interesting -- do you have a blog post or documentation on this?

--
Will Sargent
Engineer, Lightbend, Inc.


To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/0bb73124-5064-4019-bc23-ba98bda1c589%40googlegroups.com.

David Heidrich

unread,
Jun 13, 2017, 4:40:57 AM6/13/17
to Play Framework
Hi Sargent,

not yet, I have a sample application but it's a private repo. I will create an example this week.

--
David

David Heidrich

unread,
Jun 14, 2017, 6:58:41 PM6/14/17
to Play Framework
Hi,

I created an example/boilerplate Project:


--
David

Am Dienstag, 13. Juni 2017 02:43:24 UTC+2 schrieb Will Sargent:
Reply all
Reply to author
Forward
0 new messages