Updates to the Spring Boot integration in version 7.1

With the MicroStream framework, the JVM memory and Java objects are considered to be the database so that you avoid conversion and latency delays when you retrieve the data from an external system.

When working with MicroStream, three aspects are important. The configuration of the StorageManager, access to the root object of the Object graph that keeps your data, and access to the StorageManager itself to initiate storing the binary representation of the changed objects so that they survive the process restart.

Integrations into frameworks like Spring boot, but also the other integrations that are maintained by MicroStream itself like the Jakarta EE one, facilitates these 3 aspects using features of the framework itself.

This blog goes into the details of using the Spring Boot integration and its new functionality of version 7.1 the was released recently.  You can find more examples in the GitHub repository.

Adding the integration

To use the MicroStream Spring Boot integration, add the microstream-integrations-spring-boot dependency to your project

<properties>
   <microstream.version>07.01.00-MS-GA</microstream.version>
</properties>

<dependencies>
   <dependency>
      <groupId>one.microstream</groupId>
      <artifactId>microstream-integrations-spring-boot</artifactId>
      <version>${microstream.version}</version>
   </dependency>
</dependencies>

 

This dependency also brings in the required artifacts from MicroStream itself, so it is the only entry that is needed for your project.

Now that we have the dependency, we can start by defining the configuration.

Configuration of the Storage Manager

The configuration of the framework must cover the 2 main aspects of MicroStream, identifying the Root object of the Object Graph and indicating where the binary representation of the Object Graph is stored.

Let us start with the first requirement, identifying the Root Object. Since it is the idea that the integration code handles the entire setup of MicroStream, we also need a way of indicating what class we will use as Root Object. This can be done by annotating it with one.microstream.integrations.spring.boot.types.Storage.

@Storage
public class Root {

 

The other part is done by defining configuration values that not only sets the path for the storage when using the disk as a storage medium but all configuration properties described on the documentation page are supported. You only need to make sure the keys are prepended with one.microstream. to make sure we do not have a name clash with any other key value that is also set using Spring Boot configuration.

[dt_code]one.microstream.storage-directory=<path-to-storage>>
one.microstream.channel-count=2[/dt_code]

The above entries in the application.properties file, but any supported Spring Boot configuration source is also supported, makes that there are 2 channels used for the indicated directory.

But some applications might need some more advanced configuration and/or initialisation which is now supported in the recently released version 7.1.

The entire flow of the configuration is as follows

– Read the configuration properties and instantiate an EmbeddedStorageFoundation with these values.
– Call any Spring bean that implements the EmbeddedStorageFoundationCustomizer interface
– Create the StorageManager and start it.
– Call any Spring bean that implements the StorageManagerInitializer interface

By calling those Spring Beans, you can perform any additional configuration programmatically that you might need for your application.

With the EmbeddedStorageFoundationCustomizer you can add additional handlers for example. The MicroStream specialised JDK8 ones or your custom ones if you have created them are an example.

@Component
public class FoundationCustomizer implements EmbeddedStorageFoundationCustomizer {

   @Override
   public void customize(EmbeddedStorageFoundation embeddedStorageFoundation) {
      embeddedStorageFoundation.onConnectionFoundation(BinaryHandlersJDK8::registerJDK8TypeHandlers);
   }
}

After the StorageManager is active, you have an additional opportunity for initialisation. Adding some basic data to an empty database is a typical action you can perform within a Bean that implements the StorageManagerInitializer interface.

@Component
public class RootPreparation implements StorageManagerInitializer {

   @Override
   public void initialize(StorageManager storageManager) {

      Root root = (Root) storageManager.root();
      // Init 'database' with some data when it is empty.
      if (root.getBooks().isEmpty()) {
         init(root, storageManager);
      }

   }
}

 

Access Root and Storage Manager

With the configuration in place, we have a StorageManager and Root object that is fully prepared to be used in your application. The integration makes these instances also available as Spring beans. This means you can inject them, field injection and constructor injection are supported as we tap into the standard Spring functionality, in any other Spring bean.

You can also inject dependencies into the class that is annotated with @Storage. Constructor injection is not supported, the class requires a no-arg constructor so that we can contract it, but field injection is available. This allows you to inject the StorageManager into the Root class itself for example so that changes to the data can be stored.

And since the Root object became a Spring bean, you can inject it into service or repository classes to access your data and perform the logic required for the user request.

Conclusion

With the new 7.1 release of MicroStream, we have added new functionality to the Spring Boot integration code. Besides the usage of configuration values that were available in earlier versions, we have added the opportunity to programmatically fine-tune the StorageManager configuration through Spring beans that implement a certain interface.

The indication of the Root object with the annotation is also a new addition and allows you now to access it very easily as Spring bean throughout your entire codebase.

These additions make the usage of the MicroStream framework in Spring boot very easy.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

Using Cloud storage platforms with MicroStream

Next Post

Store State of a Background process with MicroStream

Related Posts
Secured By miniOrange