You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by dg...@apache.org on 2023/03/27 14:39:21 UTC

[unomi] 01/01: UNOMI-758 : async save of export config

This is an automated email from the ASF dual-hosted git repository.

dgriffon pushed a commit to branch async-save-export-config
in repository https://gitbox.apache.org/repos/asf/unomi.git

commit c1242b222a8ed8bdd049d1c789ad5400776dd180
Author: David Griffon <dg...@jahia.com>
AuthorDate: Mon Mar 27 16:38:14 2023 +0200

    UNOMI-758 : async save of export config
---
 .../services/AbstractConfigurationServiceImpl.java | 83 ----------------------
 .../router/services/AbstractCustomServiceImpl.java | 76 --------------------
 .../services/ExportConfigurationServiceImpl.java   | 43 ++++++++---
 .../services/ImportConfigurationServiceImpl.java   | 43 ++++++++---
 .../router/services/ProfileExportServiceImpl.java  | 13 ++--
 .../router/services/ProfileImportServiceImpl.java  |  8 ++-
 .../resources/OSGI-INF/blueprint/blueprint.xml     | 81 ---------------------
 7 files changed, 85 insertions(+), 262 deletions(-)

diff --git a/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/AbstractConfigurationServiceImpl.java b/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/AbstractConfigurationServiceImpl.java
deleted file mode 100644
index e7c5d67af..000000000
--- a/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/AbstractConfigurationServiceImpl.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.unomi.router.services;
-
-import org.apache.unomi.persistence.spi.PersistenceService;
-import org.apache.unomi.router.api.IRouterCamelContext;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
-import org.osgi.framework.SynchronousBundleListener;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Created by amidani on 26/06/2017.
- */
-public abstract class AbstractConfigurationServiceImpl implements SynchronousBundleListener {
-
-    private static final Logger logger = LoggerFactory.getLogger(AbstractConfigurationServiceImpl.class.getName());
-
-    protected BundleContext bundleContext;
-    protected PersistenceService persistenceService;
-    protected IRouterCamelContext routerCamelContext;
-
-    public void setBundleContext(BundleContext bundleContext) {
-        this.bundleContext = bundleContext;
-    }
-
-    public void setPersistenceService(PersistenceService persistenceService) {
-        this.persistenceService = persistenceService;
-    }
-
-    public void setRouterCamelContext(IRouterCamelContext routerCamelContext) {
-        this.routerCamelContext = routerCamelContext;
-    }
-
-    public IRouterCamelContext getRouterCamelContext() {
-        return routerCamelContext;
-    }
-
-    public void postConstruct() {
-        logger.debug("postConstruct {" + bundleContext.getBundle() + "}");
-
-        processBundleStartup(bundleContext);
-        for (Bundle bundle : bundleContext.getBundles()) {
-            if (bundle.getBundleContext() != null && bundle.getBundleId() != bundleContext.getBundle().getBundleId()) {
-                processBundleStartup(bundle.getBundleContext());
-            }
-        }
-        bundleContext.addBundleListener(this);
-        logger.info("Configuration service initialized.");
-    }
-
-    public void preDestroy() {
-        bundleContext.removeBundleListener(this);
-        logger.info("Configuration service shutdown.");
-    }
-
-    @Override
-    public void bundleChanged(BundleEvent bundleEvent) {
-
-    }
-
-    private void processBundleStartup(BundleContext bundleContext) {
-        if (bundleContext == null) {
-            return;
-        }
-    }
-}
diff --git a/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/AbstractCustomServiceImpl.java b/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/AbstractCustomServiceImpl.java
deleted file mode 100644
index dc06fff61..000000000
--- a/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/AbstractCustomServiceImpl.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.unomi.router.services;
-
-import org.apache.unomi.persistence.spi.PersistenceService;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
-import org.osgi.framework.SynchronousBundleListener;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Created by amidani on 30/06/2017.
- */
-public class AbstractCustomServiceImpl implements SynchronousBundleListener {
-
-    private static final Logger logger = LoggerFactory.getLogger(AbstractCustomServiceImpl.class);
-
-    protected PersistenceService persistenceService;
-    protected BundleContext bundleContext;
-
-    public void setPersistenceService(PersistenceService persistenceService) {
-        this.persistenceService = persistenceService;
-    }
-
-    public void setBundleContext(BundleContext bundleContext) {
-        this.bundleContext = bundleContext;
-    }
-
-    public void postConstruct() {
-        logger.debug("postConstruct {" + bundleContext.getBundle() + "}");
-
-        processBundleStartup(bundleContext);
-        for (Bundle bundle : bundleContext.getBundles()) {
-            if (bundle.getBundleContext() != null && bundle.getBundleId() != bundleContext.getBundle().getBundleId()) {
-                processBundleStartup(bundle.getBundleContext());
-            }
-        }
-        bundleContext.addBundleListener(this);
-        logger.info("Import configuration service initialized.");
-    }
-
-    public void preDestroy() {
-        bundleContext.removeBundleListener(this);
-        logger.info("Import configuration service shutdown.");
-    }
-
-    private void processBundleStartup(BundleContext bundleContext) {
-        if (bundleContext == null) {
-            return;
-        }
-    }
-
-    private void processBundleStop(BundleContext bundleContext) {
-    }
-
-    @Override
-    public void bundleChanged(BundleEvent bundleEvent) {
-
-    }
-}
diff --git a/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ExportConfigurationServiceImpl.java b/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ExportConfigurationServiceImpl.java
index 23787170e..bb9e21f23 100644
--- a/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ExportConfigurationServiceImpl.java
+++ b/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ExportConfigurationServiceImpl.java
@@ -16,22 +16,37 @@
  */
 package org.apache.unomi.router.services;
 
+import org.apache.unomi.api.services.SchedulerService;
+import org.apache.unomi.persistence.spi.PersistenceService;
 import org.apache.unomi.router.api.ExportConfiguration;
 import org.apache.unomi.router.api.IRouterCamelContext;
 import org.apache.unomi.router.api.services.ImportExportConfigurationService;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.List;
+import java.util.TimerTask;
 import java.util.UUID;
+import java.util.concurrent.TimeUnit;
 
 /**
+ * Service to manage Configuration of Item to export
  * Created by amidani on 28/04/2017.
  */
-public class ExportConfigurationServiceImpl extends AbstractConfigurationServiceImpl implements ImportExportConfigurationService<ExportConfiguration> {
+@Component(immediate = true, property = "configDiscriminator=EXPORT", service = ImportExportConfigurationService.class)
+public class ExportConfigurationServiceImpl implements ImportExportConfigurationService<ExportConfiguration> {
 
     private static final Logger logger = LoggerFactory.getLogger(ExportConfigurationServiceImpl.class.getName());
 
+    @Reference
+    private PersistenceService persistenceService;
+    @Reference
+    private SchedulerService schedulerService;
+
+    private IRouterCamelContext routerCamelContext;
+
     public ExportConfigurationServiceImpl() {
         logger.info("Initializing export configuration service...");
     }
@@ -51,12 +66,19 @@ public class ExportConfigurationServiceImpl extends AbstractConfigurationService
         if (exportConfiguration.getItemId() == null) {
             exportConfiguration.setItemId(UUID.randomUUID().toString());
         }
-        if(updateRunningRoute) {
-            try {
-                routerCamelContext.updateProfileReaderRoute(exportConfiguration, true);
-            } catch (Exception e) {
-                logger.error("Error when trying to save/update running Apache Camel Route: {}", exportConfiguration.getItemId());
-            }
+        if (updateRunningRoute) {
+            TimerTask updateRoute = new TimerTask() {
+                @Override
+                public void run() {
+                    try {
+                        routerCamelContext.updateProfileReaderRoute(exportConfiguration, true);
+                    } catch (Exception e) {
+                        logger.error("Error when trying to save/update running Apache Camel Route: {}", exportConfiguration.getItemId());
+                    }
+                }
+            };
+            // Defer config update.
+            schedulerService.getScheduleExecutorService().schedule(updateRoute, 0, TimeUnit.MILLISECONDS);
         }
         persistenceService.save(exportConfiguration);
         return persistenceService.load(exportConfiguration.getItemId(), ExportConfiguration.class);
@@ -74,6 +96,11 @@ public class ExportConfigurationServiceImpl extends AbstractConfigurationService
 
     @Override
     public void setRouterCamelContext(IRouterCamelContext routerCamelContext) {
-        super.setRouterCamelContext(routerCamelContext);
+        this.routerCamelContext = routerCamelContext;
+    }
+
+    @Override
+    public IRouterCamelContext getRouterCamelContext() {
+        return routerCamelContext;
     }
 }
diff --git a/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ImportConfigurationServiceImpl.java b/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ImportConfigurationServiceImpl.java
index 364ea73fc..28c083412 100644
--- a/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ImportConfigurationServiceImpl.java
+++ b/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ImportConfigurationServiceImpl.java
@@ -16,22 +16,37 @@
  */
 package org.apache.unomi.router.services;
 
+import org.apache.unomi.api.services.SchedulerService;
+import org.apache.unomi.persistence.spi.PersistenceService;
 import org.apache.unomi.router.api.IRouterCamelContext;
 import org.apache.unomi.router.api.ImportConfiguration;
 import org.apache.unomi.router.api.services.ImportExportConfigurationService;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.List;
+import java.util.TimerTask;
 import java.util.UUID;
+import java.util.concurrent.TimeUnit;
 
 /**
+ * Service to manage Configuration of object to import
  * Created by amidani on 28/04/2017.
  */
-public class ImportConfigurationServiceImpl extends AbstractConfigurationServiceImpl implements ImportExportConfigurationService<ImportConfiguration> {
+@Component(immediate = true, property = "configDiscriminator=IMPORT", service = ImportExportConfigurationService.class)
+public class ImportConfigurationServiceImpl implements ImportExportConfigurationService<ImportConfiguration> {
 
     private static final Logger logger = LoggerFactory.getLogger(ImportConfigurationServiceImpl.class.getName());
 
+    @Reference
+    private PersistenceService persistenceService;
+    @Reference
+    private SchedulerService schedulerService;
+
+    private IRouterCamelContext routerCamelContext;
+
     public ImportConfigurationServiceImpl() {
         logger.info("Initializing import configuration service...");
     }
@@ -51,12 +66,19 @@ public class ImportConfigurationServiceImpl extends AbstractConfigurationService
         if (importConfiguration.getItemId() == null) {
             importConfiguration.setItemId(UUID.randomUUID().toString());
         }
-        if(updateRunningRoute) {
-            try {
-                routerCamelContext.updateProfileReaderRoute(importConfiguration, true);
-            } catch (Exception e) {
-                logger.error("Error when trying to save/update running Apache Camel Route: {}", importConfiguration.getItemId());
-            }
+        if (updateRunningRoute) {
+            TimerTask updateRoute = new TimerTask() {
+                @Override
+                public void run() {
+                    try {
+                        routerCamelContext.updateProfileReaderRoute(importConfiguration, true);
+                    } catch (Exception e) {
+                        logger.error("Error when trying to save/update running Apache Camel Route: {}", importConfiguration.getItemId());
+                    }
+                }
+            };
+            // Defer config update.
+            schedulerService.getScheduleExecutorService().schedule(updateRoute, 0, TimeUnit.MILLISECONDS);
         }
         persistenceService.save(importConfiguration);
         return persistenceService.load(importConfiguration.getItemId(), ImportConfiguration.class);
@@ -74,6 +96,11 @@ public class ImportConfigurationServiceImpl extends AbstractConfigurationService
 
     @Override
     public void setRouterCamelContext(IRouterCamelContext routerCamelContext) {
-        super.setRouterCamelContext(routerCamelContext);
+        this.routerCamelContext = routerCamelContext;
+    }
+
+    @Override
+    public IRouterCamelContext getRouterCamelContext() {
+        return routerCamelContext;
     }
 }
diff --git a/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ProfileExportServiceImpl.java b/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ProfileExportServiceImpl.java
index 1b35e963d..971ccb7e6 100644
--- a/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ProfileExportServiceImpl.java
+++ b/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ProfileExportServiceImpl.java
@@ -21,10 +21,13 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.unomi.api.Profile;
 import org.apache.unomi.api.PropertyType;
 import org.apache.unomi.api.services.ConfigSharingService;
+import org.apache.unomi.persistence.spi.PersistenceService;
 import org.apache.unomi.router.api.ExportConfiguration;
 import org.apache.unomi.router.api.RouterConstants;
 import org.apache.unomi.router.api.RouterUtils;
 import org.apache.unomi.router.api.services.ProfileExportService;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -33,10 +36,14 @@ import java.util.*;
 /**
  * Created by amidani on 30/06/2017.
  */
-public class ProfileExportServiceImpl extends AbstractCustomServiceImpl implements ProfileExportService {
+@Component(immediate = true, service = ProfileExportService.class)
+public class ProfileExportServiceImpl implements ProfileExportService {
 
     private static final Logger logger = LoggerFactory.getLogger(ProfileExportServiceImpl.class.getName());
 
+    @Reference
+    private PersistenceService persistenceService;
+    @Reference
     private ConfigSharingService configSharingService;
 
     public String extractProfilesBySegment(ExportConfiguration exportConfiguration) {
@@ -104,8 +111,4 @@ public class ProfileExportServiceImpl extends AbstractCustomServiceImpl implemen
         return lineToWrite;
     }
 
-    public void setConfigSharingService(ConfigSharingService configSharingService) {
-        this.configSharingService = configSharingService;
-    }
-
 }
diff --git a/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ProfileImportServiceImpl.java b/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ProfileImportServiceImpl.java
index 86431034f..2e3acc5a7 100644
--- a/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ProfileImportServiceImpl.java
+++ b/extensions/router/router-service/src/main/java/org/apache/unomi/router/services/ProfileImportServiceImpl.java
@@ -18,8 +18,11 @@ package org.apache.unomi.router.services;
 
 import org.apache.commons.beanutils.BeanUtils;
 import org.apache.unomi.api.Profile;
+import org.apache.unomi.persistence.spi.PersistenceService;
 import org.apache.unomi.router.api.ProfileToImport;
 import org.apache.unomi.router.api.services.ProfileImportService;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -29,9 +32,12 @@ import java.util.List;
 /**
  * Created by amidani on 18/05/2017.
  */
-public class ProfileImportServiceImpl extends AbstractCustomServiceImpl implements ProfileImportService {
+@Component(immediate = true, service = ProfileImportService.class)
+public class ProfileImportServiceImpl implements ProfileImportService {
 
     private static final Logger logger = LoggerFactory.getLogger(ProfileImportServiceImpl.class.getName());
+    @Reference
+    private PersistenceService persistenceService;
 
     public boolean saveMergeDeleteImportedProfile(ProfileToImport profileToImport) throws InvocationTargetException, IllegalAccessException {
         logger.debug("Importing profile with ID : {}", profileToImport.getItemId());
diff --git a/extensions/router/router-service/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/extensions/router/router-service/src/main/resources/OSGI-INF/blueprint/blueprint.xml
deleted file mode 100644
index c00554cfa..000000000
--- a/extensions/router/router-service/src/main/resources/OSGI-INF/blueprint/blueprint.xml
+++ /dev/null
@@ -1,81 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-  ~ Licensed to the Apache Software Foundation (ASF) under one or more
-  ~ contributor license agreements.  See the NOTICE file distributed with
-  ~ this work for additional information regarding copyright ownership.
-  ~ The ASF licenses this file to You under the Apache License, Version 2.0
-  ~ (the "License"); you may not use this file except in compliance with
-  ~ the License.  You may obtain a copy of the License at
-  ~
-  ~      http://www.apache.org/licenses/LICENSE-2.0
-  ~
-  ~ Unless required by applicable law or agreed to in writing, software
-  ~ distributed under the License is distributed on an "AS IS" BASIS,
-  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-  ~ See the License for the specific language governing permissions and
-  ~ limitations under the License.
-  -->
-
-<blueprint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-           xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
-           xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
-
-    <reference id="persistenceService" interface="org.apache.unomi.persistence.spi.PersistenceService"/>
-    <reference id="configSharingService" interface="org.apache.unomi.api.services.ConfigSharingService"/>
-
-    <bean id="importConfigurationServiceImpl" class="org.apache.unomi.router.services.ImportConfigurationServiceImpl"
-          init-method="postConstruct" destroy-method="preDestroy">
-        <property name="persistenceService" ref="persistenceService"/>
-        <property name="bundleContext" ref="blueprintBundleContext"/>
-    </bean>
-    <service id="importConfigurationService" ref="importConfigurationServiceImpl">
-        <interfaces>
-            <value>org.apache.unomi.router.api.services.ImportExportConfigurationService</value>
-            <value>org.osgi.framework.SynchronousBundleListener</value>
-        </interfaces>
-        <service-properties>
-            <entry key="configDiscriminator" value="IMPORT"/>
-        </service-properties>
-    </service>
-
-    <bean id="exportConfigurationServiceImpl" class="org.apache.unomi.router.services.ExportConfigurationServiceImpl"
-          init-method="postConstruct" destroy-method="preDestroy">
-        <property name="persistenceService" ref="persistenceService"/>
-        <property name="bundleContext" ref="blueprintBundleContext"/>
-    </bean>
-    <service id="exportConfigurationService" ref="exportConfigurationServiceImpl">
-        <interfaces>
-            <value>org.osgi.framework.SynchronousBundleListener</value>
-            <value>org.apache.unomi.router.api.services.ImportExportConfigurationService</value>
-        </interfaces>
-        <service-properties>
-            <entry key="configDiscriminator" value="EXPORT"/>
-        </service-properties>
-    </service>
-
-    <bean id="profileImportServiceImpl" class="org.apache.unomi.router.services.ProfileImportServiceImpl"
-          init-method="postConstruct" destroy-method="preDestroy">
-        <property name="persistenceService" ref="persistenceService"/>
-        <property name="bundleContext" ref="blueprintBundleContext"/>
-    </bean>
-    <service id="profileImportService" ref="profileImportServiceImpl">
-        <interfaces>
-            <value>org.osgi.framework.SynchronousBundleListener</value>
-            <value>org.apache.unomi.router.api.services.ProfileImportService</value>
-        </interfaces>
-    </service>
-
-    <bean id="profileExportServiceImpl" class="org.apache.unomi.router.services.ProfileExportServiceImpl"
-          init-method="postConstruct" destroy-method="preDestroy">
-        <property name="persistenceService" ref="persistenceService"/>
-        <property name="configSharingService" ref="configSharingService" />
-        <property name="bundleContext" ref="blueprintBundleContext"/>
-    </bean>
-    <service id="profileExportService" ref="profileExportServiceImpl">
-        <interfaces>
-            <value>org.osgi.framework.SynchronousBundleListener</value>
-            <value>org.apache.unomi.router.api.services.ProfileExportService</value>
-        </interfaces>
-    </service>
-
-</blueprint>