Java: how do I access per-instance shared memory from an Endpoint?

59 views
Skip to first unread message

Zak Taccardi

unread,
Jul 1, 2015, 4:58:25 PM7/1/15
to google-a...@googlegroups.com
I am using dagger for dependency injection, and I don't want to create a new object graph for every endpoint.

Android lets us extend `Application`, which is a great place to store our singletons so they can be easily reused throughout different pages (actiivities) in the app.

What is Google App-Engine's equivalent of this?


note: I am okay with this being a per-instance thing.

Patrice (Cloud Platform Support)

unread,
Jul 2, 2015, 10:40:27 AM7/2/15
to google-a...@googlegroups.com, Zak Taccardi
Hi Zack,

I could see saving data in the datastore (as a blob, possibly), and retrieving it when needed from there to be a solution.

If I get it correctly, you want to be able to have a single point of access for your object graphs? to me, I'd say the best would be datastore as a blob. Maybe your precise use-case doesn't warrant that though, so if you could expand, it would be great so we can discuss an answer more tailored to your question :)

Cheers!

Jeff Schnitzer

unread,
Jul 6, 2015, 12:28:46 AM7/6/15
to Google App Engine, Zak Taccardi
Patrice: Dagger is a dependency injection framework like Guice. He's asking how you create a singleton object in Java webland.

Zak: There are lots of ways of doing this. First of all, you do not want to try to create some sort of cross-instance object graph; each instance should instantiate the ObjectGraph at instance start. Any other way lies madness.

The most "natural" place to do this is in a ServletContextListener you configure in web.xml. Then put the ObjectGraph in the ServletContext.get/setAttribute collection. However, you're going to quickly figure out that all of this feels a little inside-out because really, things like servlets or JAXRS endpoints or whatnot should be instantiated by Dagger, not the other way around.

Another option is to simply save the ObjectGraph as a static field somewhere, instantiated as a singleton statically. This suffers from testability issues but if you want easy, that's the simplest solution.

FWIW, Dagger has some catching up to do before it's ready to replace Guice on the server-side. You might want to switch to Guice; it will feel less like swimming upstream but for the most part the API is pretty similar.

Jeff

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengi...@googlegroups.com.
To post to this group, send email to google-a...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-appengine.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/165b1007-c4f4-4bc1-9069-74c5c2561f2e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Patrice (Cloud Platform Support)

unread,
Jul 6, 2015, 11:24:13 AM7/6/15
to google-a...@googlegroups.com, je...@infohazard.org, Zak Taccardi, je...@infohazard.org
Hi Jeff,

Thank you for calling me out on this. Yeah looking into Dagger a bit more (I must admit to being a newbie with that framework), my answer should be completely disregarded. I gave a way to share DATA, not objects :P.

Jeff seems to be in the right here, While you possibly CAN create some cross-instance, using a LOT of work, a backend permanent instance for it to reside, and creating endpoints to get it and update it... it's a LOT of work for something you can simply declare locally as static and your app won't really change it's behavior (be careful about testing, as Jeff points out, as this will definitely be special, testing a "fake" singleton as a real one will be misleading). 

Cheers!

On Monday, July 6, 2015 at 12:28:46 AM UTC-4, Jeff Schnitzer wrote:
Patrice: Dagger is a dependency injection framework like Guice. He's asking how you create a singleton object in Java webland.

Zak: There are lots of ways of doing this. First of all, you do not want to try to create some sort of cross-instance object graph; each instance should instantiate the ObjectGraph at instance start. Any other way lies madness.

The most "natural" place to do this is in a ServletContextListener you configure in web.xml. Then put the ObjectGraph in the ServletContext.get/setAttribute collection. However, you're going to quickly figure out that all of this feels a little inside-out because really, things like servlets or JAXRS endpoints or whatnot should be instantiated by Dagger, not the other way around.

Another option is to simply save the ObjectGraph as a static field somewhere, instantiated as a singleton statically. This suffers from testability issues but if you want easy, that's the simplest solution.

FWIW, Dagger has some catching up to do before it's ready to replace Guice on the server-side. You might want to switch to Guice; it will feel less like swimming upstream but for the most part the API is pretty similar.

Jeff
On Thu, Jul 2, 2015 at 7:40 AM, Patrice (Cloud Platform Support) <pvout...@google.com> wrote:
Hi Zack,

I could see saving data in the datastore (as a blob, possibly), and retrieving it when needed from there to be a solution.

If I get it correctly, you want to be able to have a single point of access for your object graphs? to me, I'd say the best would be datastore as a blob. Maybe your precise use-case doesn't warrant that though, so if you could expand, it would be great so we can discuss an answer more tailored to your question :)

Cheers!

On Wednesday, July 1, 2015 at 4:58:25 PM UTC-4, Zak Taccardi wrote:
I am using dagger for dependency injection, and I don't want to create a new object graph for every endpoint.

Android lets us extend `Application`, which is a great place to store our singletons so they can be easily reused throughout different pages (actiivities) in the app.

What is Google App-Engine's equivalent of this?


note: I am okay with this being a per-instance thing.

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengine+unsubscribe@googlegroups.com.
To post to this group, send email to google-appengine@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages