Global variable/object to access static data source

Hello Team,
I have recently started using CUBA.platform (so far the exploration was interesting). However, due to lack of complete understanding i am unable to figure out a solution for the below mentioned problem.

We have a .txt file of 50+Mb in size that we are trying to use as a static data source(yes, after marshalling) inside the views. We are planning to load this file at the time of application startup (utilizing AppContext.Listener’s applicationStarted method). Our expected objective is to access this marshalled data inside views through ServiceBean classes. Although, i am unable to understand the mechanism to create a global variable (wanted to avoid individual I/O operations) that can be accessed inside views without re-initializing.

I hope the problem is not hard to understand, any help would be grateful.

Thanks,
S

Hi,

CUBA platform is based on Spring framework.
Any Spring bean is a singleton - this means that only one instance of particular class is created by the framework, bound with other beans and lives in the running server.

Any member field of a singleton bean is essentially a global variable.
You can think that this is the modern way of creating global variables, if you want.

You can take a look into the CUBA class com.haulmont.cuba.core.app.LockManager
and use it as an example how to keep a cached result of a complex calculation.

Locking configuration is stored in LockManager#config variable. It is initialized on first access and then used multiple times. There is also a LockManager#reloadConfiguration method which can reset cached value in runtime.

1 Like

Thanks for your response @AlexBudarov.
After some digging i came to the concept of CustomDataStore. Do you think using CustomDataStore with non-persistent entities is more efficient in this use case?

Well, custom DataStore is another concept. It’s more for custom databases which aren’t supported by EclipseLink ORM. For example MongoDB or other no-sql databases.

So implementing own DataStore would be an overkill.

1 Like

Thank you so much for sharing these insights @AlexBudarov.

A few more hints that may help you to design a solution:

  • CUBA visual components can easily show information represented by entities of your data model. The entities can be persistent (mapped to a database table), or non-persistent (existing only in memory). In your case, you could create some non-persistent entities representing data from your file, load data to the entities when initializing the application and keep them in the server memory (in a service which is a singleton).

  • Data Loaders used in screens can redirect loading to your service as follows:

      @Inject
      private MyService myService;
    
      @Install(to = "myEntityDl", target = Target.DATA_LOADER)
      protected List<MyEntity> myEntityDlLoadDelegate(LoadContext<MyEntity> loadContext) {
          return myService.loadMyEntitiesList();
      }
    
  • A custom data store can be another option (instead of redirecting from data loaders to a custom service). It’s quite easy to implement if you just need to read data from some source and return it as list of entities.

1 Like

Thank you @knstvk, thats another neat solution to solve this problem i might give it a try for other non-persistent entities.

Thank you so much. I am really amazed to see the extensibility of this platform.