UNOMI-168 Use concurrent maps in DefinitionService The data is mutating and can be accessed from multiple threads, so we need to use adapted data structure.
I also make sure not to store null in the cache (perhaps caused other issues, in ConcurrentHashMap it's forbidden)? Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/6728e500 Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/6728e500 Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/6728e500 Branch: refs/heads/master Commit: 6728e50040f05d38d58020caa1bc8334678aa615 Parents: e0b2c35 Author: Jarek Lipski <jlip...@jahia.com> Authored: Wed Feb 28 12:08:53 2018 +0100 Committer: Jarek Lipski <jlip...@jahia.com> Committed: Wed Feb 28 12:08:53 2018 +0100 ---------------------------------------------------------------------- .../services/services/DefinitionsServiceImpl.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/6728e500/services/src/main/java/org/apache/unomi/services/services/DefinitionsServiceImpl.java ---------------------------------------------------------------------- diff --git a/services/src/main/java/org/apache/unomi/services/services/DefinitionsServiceImpl.java b/services/src/main/java/org/apache/unomi/services/services/DefinitionsServiceImpl.java index 9f3c11c..b5f8166 100644 --- a/services/src/main/java/org/apache/unomi/services/services/DefinitionsServiceImpl.java +++ b/services/src/main/java/org/apache/unomi/services/services/DefinitionsServiceImpl.java @@ -36,6 +36,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.URL; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; public class DefinitionsServiceImpl implements DefinitionsService, SynchronousBundleListener { @@ -43,8 +44,8 @@ public class DefinitionsServiceImpl implements DefinitionsService, SynchronousBu private PersistenceService persistenceService; - private Map<String, ConditionType> conditionTypeById = new HashMap<>(); - private Map<String, ActionType> actionTypeById = new HashMap<>(); + private Map<String, ConditionType> conditionTypeById = new ConcurrentHashMap<>(); + private Map<String, ActionType> actionTypeById = new ConcurrentHashMap<>(); private Map<String, ValueType> valueTypeById = new HashMap<>(); private Map<String, Set<ValueType>> valueTypeByTag = new HashMap<>(); private Map<Long, List<PluginType>> pluginTypes = new HashMap<>(); @@ -247,7 +248,9 @@ public class DefinitionsServiceImpl implements DefinitionsService, SynchronousBu ConditionType type = conditionTypeById.get(id); if (type == null || type.getVersion() == null) { type = persistenceService.load(id, ConditionType.class); - conditionTypeById.put(id, type); + if (type != null) { + conditionTypeById.put(id, type); + } } if (type != null && type.getParentCondition() != null) { ParserHelper.resolveConditionType(this, type.getParentCondition()); @@ -289,7 +292,9 @@ public class DefinitionsServiceImpl implements DefinitionsService, SynchronousBu ActionType type = actionTypeById.get(id); if (type == null || type.getVersion() == null) { type = persistenceService.load(id, ActionType.class); - actionTypeById.put(id, type); + if (type != null) { + actionTypeById.put(id, type); + } } return type; }