Objectify 6 with Cloud Functions

112 views
Skip to first unread message

healthy-interac

unread,
Oct 7, 2020, 3:45:13 PM10/7/20
to objectify-appengine
Hi Jeff,

Another POC we'd like to accomplish is about using server-less computing aka Cloud Functions on GCP.
We need to access Cloud Datastore from the function which is deployed separately and independently from our main application, but within the same App Engine and sharing the same datastore. The standard project structure required by CF is described here: https://cloud.google.com/functions/docs/concepts/java-deploy#deploy_from_source

So I'm wondering how to initialize Objectify correctly given this very simple project structure, which is obviously does not include any web-related directories?

Meanwhile all attempts to trigger ObjectifyService.init() ended up with the same error:
"Failed to execute com.example.Example java.lang.IllegalStateException: You must call ObjectifyService.init() before using Objectify"

Any advise appreciated :)

Jeff Schnitzer

unread,
Oct 7, 2020, 4:22:29 PM10/7/20
to objectify-appengine
Since you aren't using the ObjectifyFilter, you'll need to do the same work to set up the root context. Look at ObjectifyService.run() (or begin() if a closure doesn't work for you).

Jeff

--
You received this message because you are subscribed to the Google Groups "objectify-appengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objectify-appen...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/objectify-appengine/8656bd30-8a87-48af-9310-284e006643edn%40googlegroups.com.

Dmitry Tsukinovsky

unread,
Oct 7, 2020, 5:51:48 PM10/7/20
to objectify...@googlegroups.com
I tried to use both ObjectifyService.run() and begin() - always getting the same error.
For example, when using run() (I'm aware VoidWork is deprecated, but it's still supposed to work, isn't it?):

ObjectifyService.run(new VoidWork() {
      @Override
      public void vrun() {
        ObjectifyService.register(MyObject.class);
        MyObject myObj = ObjectifyService.ofy().load().type(MyObject.class).list();
        //then edit myObj and write it to the Datastore
      }
    });

Failed to execute com.example.Example java.lang.IllegalStateException: You must call ObjectifyService.init() before using Objectify at com.google.common.base.Preconditions.checkState(Preconditions.java:511) at 
com.googlecode.objectify.ObjectifyService.factory(ObjectifyService.java:34) at 
com.googlecode.objectify.ObjectifyService.begin(ObjectifyService.java:80) at 
com.googlecode.objectify.ObjectifyService.run(ObjectifyService.java:67) at 
com.example.Example.accept(Example.java:27) at 
com.example.Example.accept(Example.java:20) at com.google.cloud.functions.invoker.NewBackgroundFunctionExecutor$TypedFunctionExecutor.serviceLegacyEvent(NewBackgroundFunctionExecutor.java:258) at
...

Jeff Schnitzer

unread,
Oct 7, 2020, 5:55:36 PM10/7/20
to objectify-appengine
Sorry, my mistake for not reading closer. You need to initialize Objectify somewhere before the run() call is made. I'm not familiar enough with cloud functions to know the best approach, but there are lots of ways to initialize code in Java.

Jeff

Reply all
Reply to author
Forward
0 new messages