Skip to content

Commit

Permalink
Zj/spring custom initializer 1024 (#323)
Browse files Browse the repository at this point in the history
* add custom initializer and remove some deprecated staff

* use spring aware and leave deprecated stuff

* small improvements

* add code configuration docu

* change naming

* Update spring-boot.adoc (#327)

---------

Co-authored-by: Florian Habermann <[email protected]>
  • Loading branch information
zdenek-jonas and fh-ms authored Nov 4, 2024
1 parent 67f4291 commit b911fd9
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
23 changes: 23 additions & 0 deletions docs/modules/misc/pages/integrations/spring-boot.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,26 @@ and click connect.
If you are using Spring Dev Tools, it is recommended to exclude the {product-name} classes from the classpath. Spring Dev Tools use their own restart classloader, which dynamically reloads classes. This can cause issues with the {product-name} classes, as they are not designed to be reloaded on the fly.

Spring documentation: https://docs.spring.io/spring-boot/reference/using/devtools.html

== Code Configuration Before Storage Creation

There are some use cases where it is necessary to configure the storage's context before it is created. For this purpose, you can use the `StorageContextInitializer` to create a custom storage configuration in your application.

Example for configuring `LazyReferenceManager`:

[source,java]
----
@Component
public class StorageContextInitializerImpl implements StorageContextInitializer
{
@Override
public void initialize()
{
LazyReferenceManager.set(LazyReferenceManager.New(
Lazy.Checker(
1_000_000, // timeout of lazy access
0.75 // memory quota
)));
}
}
----
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.microstream.spring.boot.example.simple.initializer;

/*-
* #%L
* EclipseStore Integrations SpringBoot
* %%
* Copyright (C) 2023 - 2024 MicroStream Software
* %%
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
* #L%
*/

import org.eclipse.serializer.reference.Lazy;
import org.eclipse.serializer.reference.LazyReferenceManager;
import org.eclipse.store.integrations.spring.boot.types.initializers.StorageContextInitializer;
import org.springframework.stereotype.Component;

/**
* This class is for demonstration purposes only. It shows how to execute code before storage is initialized.
* <a href="https://docs.eclipsestore.io/manual/storage/loading-data/lazy-loading/clearing-lazy-references.html">...</a>
*/
@Component
public class StorageContextInitializerImpl implements StorageContextInitializer
{
@Override
public void initialize()
{
LazyReferenceManager.set(LazyReferenceManager.New(
Lazy.Checker(
1_000_000, // timeout of lazy access
0.75 // memory quota
)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
import org.eclipse.store.integrations.spring.boot.types.configuration.ConfigurationPair;
import org.eclipse.store.integrations.spring.boot.types.configuration.EclipseStoreProperties;
import org.eclipse.store.integrations.spring.boot.types.converter.EclipseStoreConfigConverter;
import org.eclipse.store.integrations.spring.boot.types.initializers.StorageContextInitializer;
import org.eclipse.store.storage.embedded.configuration.types.EmbeddedStorageConfigurationBuilder;
import org.eclipse.store.storage.embedded.types.EmbeddedStorageFoundation;
import org.slf4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Map;

Expand All @@ -34,10 +39,11 @@
*
* @since 1.2.0
*/
public class EmbeddedStorageFoundationFactory
public class EmbeddedStorageFoundationFactory implements ApplicationContextAware
{
private final EclipseStoreConfigConverter converter;
private final ClassLoaderProvider classLoaderProvider;
private ApplicationContext applicationContext;

private final Logger logger = Logging.getLogger(EmbeddedStorageFoundationFactory.class);

Expand All @@ -47,6 +53,7 @@ public EmbeddedStorageFoundationFactory(final EclipseStoreConfigConverter conver
this.classLoaderProvider = classLoaderProvider;
}


/**
* Creates an {@code EmbeddedStorageFoundation} using the provided configuration. This method should be called when the additional configuration for the foundation is required.
*
Expand All @@ -56,6 +63,14 @@ public EmbeddedStorageFoundationFactory(final EclipseStoreConfigConverter conver
*/
public EmbeddedStorageFoundation<?> createStorageFoundation(final EclipseStoreProperties eclipseStoreProperties, final ConfigurationPair... additionalConfiguration)
{
// Call custom code if available
try {
StorageContextInitializer storageContextInitializer = applicationContext.getBean(StorageContextInitializer.class);
storageContextInitializer.initialize();
} catch (NoSuchBeanDefinitionException e) {
this.logger.debug("No custom storage initializer found.");
}

final EmbeddedStorageConfigurationBuilder builder = EmbeddedStorageConfigurationBuilder.New();
final Map<String, String> valueMap = this.converter.convertConfigurationToMap(eclipseStoreProperties);
for (final ConfigurationPair pair : additionalConfiguration)
Expand Down Expand Up @@ -116,4 +131,9 @@ protected Object createNewRootInstance(final EclipseStoreProperties properties)
}
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
this.applicationContext = applicationContext;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.eclipse.store.integrations.spring.boot.types.initializers;

/*-
* #%L
* EclipseStore Integrations SpringBoot
* %%
* Copyright (C) 2023 - 2024 MicroStream Software
* %%
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
* #L%
*/

/**
* Interface for storage context initializers.
* Implementations of this interface can provide custom initialization logic
* that will be executed before the creation of the storage foundation.
*/
public interface StorageContextInitializer
{
/**
* Method to be implemented with custom initialization logic.
* This method will be called immediately before the storage foundation creation process.
*/
void initialize();
}

0 comments on commit b911fd9

Please sign in to comment.