diff --git a/docs/modules/misc/pages/integrations/spring-boot.adoc b/docs/modules/misc/pages/integrations/spring-boot.adoc index e37bf8f3..5f708d38 100644 --- a/docs/modules/misc/pages/integrations/spring-boot.adoc +++ b/docs/modules/misc/pages/integrations/spring-boot.adoc @@ -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 + ))); + } +} +---- diff --git a/examples/spring-boot3-simple/src/main/java/org/microstream/spring/boot/example/simple/initializer/StorageContextInitializerImpl.java b/examples/spring-boot3-simple/src/main/java/org/microstream/spring/boot/example/simple/initializer/StorageContextInitializerImpl.java new file mode 100644 index 00000000..5d5f1159 --- /dev/null +++ b/examples/spring-boot3-simple/src/main/java/org/microstream/spring/boot/example/simple/initializer/StorageContextInitializerImpl.java @@ -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. + * ... + */ +@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 + ))); + } +} diff --git a/integrations/spring-boot3/src/main/java/org/eclipse/store/integrations/spring/boot/types/factories/EmbeddedStorageFoundationFactory.java b/integrations/spring-boot3/src/main/java/org/eclipse/store/integrations/spring/boot/types/factories/EmbeddedStorageFoundationFactory.java index 08ff159e..6a9272c6 100644 --- a/integrations/spring-boot3/src/main/java/org/eclipse/store/integrations/spring/boot/types/factories/EmbeddedStorageFoundationFactory.java +++ b/integrations/spring-boot3/src/main/java/org/eclipse/store/integrations/spring/boot/types/factories/EmbeddedStorageFoundationFactory.java @@ -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; @@ -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); @@ -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. * @@ -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 valueMap = this.converter.convertConfigurationToMap(eclipseStoreProperties); for (final ConfigurationPair pair : additionalConfiguration) @@ -116,4 +131,9 @@ protected Object createNewRootInstance(final EclipseStoreProperties properties) } } + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException + { + this.applicationContext = applicationContext; + } } diff --git a/integrations/spring-boot3/src/main/java/org/eclipse/store/integrations/spring/boot/types/initializers/StorageContextInitializer.java b/integrations/spring-boot3/src/main/java/org/eclipse/store/integrations/spring/boot/types/initializers/StorageContextInitializer.java new file mode 100644 index 00000000..9682fb15 --- /dev/null +++ b/integrations/spring-boot3/src/main/java/org/eclipse/store/integrations/spring/boot/types/initializers/StorageContextInitializer.java @@ -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(); +}