From ba711cb2da1a0c47186a14bfae95ad92f1435760 Mon Sep 17 00:00:00 2001 From: alandavis Date: Thu, 11 Aug 2022 15:59:17 +0100 Subject: [PATCH] Simplify WebApplicationConfig --- .../alfresco/transform/base/Application.java | 1 - .../AlfrescoSharedFileStoreClient.java | 2 + .../base/config/WebApplicationConfig.java | 21 +------- .../base/registry/TransformRegistry.java | 53 ++++++++++--------- 4 files changed, 33 insertions(+), 44 deletions(-) diff --git a/engines/base/src/main/java/org/alfresco/transform/base/Application.java b/engines/base/src/main/java/org/alfresco/transform/base/Application.java index 5c74fcec..5c096f07 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/Application.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/Application.java @@ -43,7 +43,6 @@ import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication -@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class}) @EnableAsync //@EnableScheduling //@EnableRetry diff --git a/engines/base/src/main/java/org/alfresco/transform/base/clients/AlfrescoSharedFileStoreClient.java b/engines/base/src/main/java/org/alfresco/transform/base/clients/AlfrescoSharedFileStoreClient.java index 77b02989..408d9323 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/clients/AlfrescoSharedFileStoreClient.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/clients/AlfrescoSharedFileStoreClient.java @@ -40,6 +40,7 @@ import org.springframework.core.io.Resource; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; import org.springframework.util.LinkedMultiValueMap; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; @@ -47,6 +48,7 @@ import org.springframework.web.client.RestTemplate; /** * Simple Rest client that call Alfresco Shared File Store */ +@Service public class AlfrescoSharedFileStoreClient { @Value("${fileStoreUrl}") diff --git a/engines/base/src/main/java/org/alfresco/transform/base/config/WebApplicationConfig.java b/engines/base/src/main/java/org/alfresco/transform/base/config/WebApplicationConfig.java index 03b6f0e4..709ad9e3 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/config/WebApplicationConfig.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/config/WebApplicationConfig.java @@ -33,6 +33,7 @@ import org.alfresco.transform.base.registry.TransformRegistry; import org.alfresco.transform.common.TransformerDebug; import org.alfresco.transform.messages.TransformRequestValidator; import org.alfresco.transform.registry.TransformServiceRegistry; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -61,40 +62,22 @@ public class WebApplicationConfig implements WebMvcConfigurer public void addInterceptors(InterceptorRegistry registry) { registry - .addInterceptor(transformInterceptor()) + .addInterceptor(new TransformInterceptor()) .addPathPatterns(ENDPOINT_TRANSFORM, "/live", "/ready"); } - @Bean - public TransformInterceptor transformInterceptor() - { - return new TransformInterceptor(); - } - @Bean public RestTemplate restTemplate() { return new RestTemplate(); } - @Bean - public AlfrescoSharedFileStoreClient alfrescoSharedFileStoreClient() - { - return new AlfrescoSharedFileStoreClient(); - } - @Bean public TransformRequestValidator transformRequestValidator() { return new TransformRequestValidator(); } - @Bean - public TransformServiceRegistry transformRegistry() - { - return new TransformRegistry(); - } - @Bean public TransformerDebug transformerDebug() { diff --git a/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformRegistry.java b/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformRegistry.java index c50961fd..64d5acbd 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformRegistry.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/registry/TransformRegistry.java @@ -32,6 +32,7 @@ import org.alfresco.transform.registry.TransformCache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; import org.springframework.retry.annotation.Backoff; @@ -39,6 +40,7 @@ import org.springframework.retry.annotation.Recover; import org.springframework.retry.annotation.Retryable; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; import java.util.Comparator; import java.util.List; @@ -48,8 +50,16 @@ import java.util.function.Supplier; import static org.alfresco.transform.config.CoreVersionDecorator.setCoreVersionOnSingleStepTransformers; +@Service public class TransformRegistry extends AbstractTransformRegistry { + private static final Logger log = LoggerFactory.getLogger(TransformRegistry.class); + + @Autowired + private String coreVersion; + @Autowired + private List transformConfigSources; + private static class Data extends TransformCache { private TransformConfig transformConfigBeforeIncompleteTransformsAreRemoved; @@ -66,47 +76,32 @@ public class TransformRegistry extends AbstractTransformRegistry } } - private static final Logger log = LoggerFactory.getLogger(TransformRegistry.class); - - @Autowired - private String coreVersion; - @Autowired - private List transformConfigSources; + private Data data = new Data(); // Ensures that read operations are blocked while config is being updated private ReadWriteLock configRefreshLock = new ReentrantReadWriteLock(); - private Data data = new Data(); + @EventListener + void handleContextRefreshedEvent(final ContextRefreshedEvent event) + { + final ApplicationContext context = event.getApplicationContext(); + // the local "initEngineConfigs" method has to be called through the Spring proxy + context.getBean(TransformRegistry.class).initRegistryOnAppStartup(null); + } /** * Load the registry on application startup. This allows Components in projects that extend the t-engine base * to use @PostConstruct to add to {@code transformConfigSources}, before the registry is loaded. */ - @EventListener - private void initRegistryOnAppStartup(final ContextRefreshedEvent event) - { - asyncRegistryInit(); - } - // @Async // @Retryable(include = {IllegalStateException.class}, // maxAttemptsExpression = "#{${transform.engine.config.retry.attempts}}", // backoff = @Backoff(delayExpression = "#{${transform.engine.config.retry.timeout} * 1000}")) - public void asyncRegistryInit() + public void initRegistryOnAppStartup(final ContextRefreshedEvent event) { initRegistry(); } - /** - * Recovery method in case all the retries fail. If not specified, the @Retryable method will cause the application - * to stop. - */ -// @Recover - private void recover(IllegalStateException e) - { - log.warn(e.getMessage()); - } - /** * Takes the schedule from a spring-boot property */ @@ -135,6 +130,16 @@ public class TransformRegistry extends AbstractTransformRegistry concurrentUpdate(combinedTransformConfig, transformConfigBeforeIncompleteTransformsAreRemoved); } + /** + * Recovery method in case all the retries fail. If not specified, the @Retryable method will cause the application + * to stop. + */ + // @Recover + private void recover(IllegalStateException e) + { + log.warn(e.getMessage()); + } + public TransformConfig getTransformConfig() { return getData().getTransformConfigBeforeIncompleteTransformsAreRemoved();