Aim of the project is using Objectify as a persistance API in a Spring Boot project. First we use a Data Store Emulator for local development which is the topic of this assay. Deploy it to a Cloud Run compute service as a real cloud project is second part and topic of the second assay.
I assume that you already have basic Google Cloud and Spring Boot knowledge. I will not talk about Objectify, Spring Boot or GCP deeply. I just want to develop a basic project quickly and try to explain how you can do it. So, we have a spring boot project which has various endpoints. We will make some Datastore transactions by using these endpoints. Additionally, you can use this project as a MVC template as well. You can follow the source code from here.
What is Objectify?
Objectify is defined in its own Github page as ‘ a third party JAVA data access API which is designed for Google Cloud Platform projects’. It is easy to use and allows you to write code faster.
Add the dependency to gradle file for use Objectify in the project;
What is Data Store Emulator?
Data store emulator is basically emulation of Datastore. It is allows us develop and test locally our application before deploy to Google Cloud services.
For install it use command below;
gcloud components install cloud-datastore-emulator
gcloud beta emulators datastore start --host-port=localhost:8484
After execute one of this way, there will be message in the console output like ‘Dev App Server is now running.’
Writing persistance layer
Persistance layer is consist of some different conceptional classes. Let’s se one by one.
All transactions are begin with a Objectify instance which we obtain with ObjectifyService. So, we have to initialize this service. The default init() will connect to the default Datastore Service configured for your environment which we see in the second part. But if you will use emulator, you have to initialize with additional settings. Morover, we have to call this initialization (ObjectifyService.init()) even before object registery or using any of objectify command, even including ofy() . Otherwise we get exception:
Caused by: java.lang.IllegalStateException: You must call ObjectifyService.init() before using Objectify
For this reason, I made this initialization in the our very first execution.
I use ObjectifyRegistry.java class in the project as a persistance service. After initialize the ObjectifyService, we have to register our entities. Best practise to do it is defining in a static initializer method. So, I made definitions in ObjectifyRegistry.java class. With this way we guaranteed the registery only for once.
Web filter is some kind of filter that intercept the request. Objectify requires a filter for transactions and pending requests. We use webfilter with @WebFilter annotation, and execute it in runtime with @Component annotation which is coming from Spring Boot.
If a webfilter is not exist, than you will probabaly get this error;
Unless you will get java.lang.IllegalStateException: You have not started an Objectify context.You are probably missing the ObjectifyFilter. If you are not running in the context of an http request, see the ObjectifyService.run() method.
Let’s use them
There is a POJO named Airplane. I had already registered it in the ObjectifyRegistry.java. I use ObjectifyService.ofy() command at the repository layer for the persistance transactions.
I use Postman for call API and added command to the project that I use.
Datastore emulator has not got a official user interface or CLI commands to see the database.For this reason, I use POST and GET method to crosscheck the results. There are some open source user interfaces on github, you can use them if you want.
Add a new aiplane with /add and crosscheck with using /getAirplane method using registration id.
Datastore emulator do not remove your data. It creates a .bin file and uses it as a database. Default address of storage is ~/.config/gcloud/emulators/datastore/. in linux systems. If you want to remove data, remove it manually after stop the emulator or delete them with command which is pretty unpractical.
I hope you’ve found this assay helpful. For see the deploying this project to Cloud Run, check the second part.