From e35dd07c7b12bb494923b28e877dba8bd192a5be Mon Sep 17 00:00:00 2001 From: Katia Aresti Date: Tue, 16 May 2017 15:03:16 +0200 Subject: [PATCH] ISPN-7823 RemoteCacheManager start/stop async --- .../client/hotrod/RemoteCacheManager.java | 32 ++++++++++++++----- .../client/hotrod/RemoteCacheManagerTest.java | 15 +++++---- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/RemoteCacheManager.java b/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/RemoteCacheManager.java index a240e56b76ec..cdc9e78975c9 100644 --- a/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/RemoteCacheManager.java +++ b/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/RemoteCacheManager.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Properties; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicInteger; @@ -57,7 +58,6 @@ public class RemoteCacheManager implements RemoteCacheContainer { public static final String DEFAULT_CACHE_NAME = "___defaultcache"; public static final String HOTROD_CLIENT_PROPERTIES = "hotrod-client.properties"; - private volatile boolean started = false; private final Map cacheName2RemoteCache = new HashMap<>(); private final AtomicInteger defaultCacheTopologyId = new AtomicInteger(HotRodConstants.DEFAULT_CACHE_TOPOLOGY); @@ -68,6 +68,8 @@ public class RemoteCacheManager implements RemoteCacheContainer { protected TransportFactory transportFactory; private ExecutorService asyncExecutorService; protected ClientListenerNotifier listenerNotifier; + private final Runnable start = this::start; + private final Runnable stop = this::stop; /** * @@ -171,6 +173,16 @@ public RemoteCache getCache(boolean forceReturnValue) { return createRemoteCache("", forceReturnValue); } + public CompletableFuture startAsync() { + createExecutorService(); + return CompletableFuture.runAsync(start, asyncExecutorService); + } + + public CompletableFuture stopAsync() { + createExecutorService(); + return CompletableFuture.runAsync(stop, asyncExecutorService); + } + @Override public void start() { transportFactory = Util.getInstance(configuration.transportFactory()); @@ -184,13 +196,7 @@ public void start() { codec = CodecFactory.getCodec(configuration.version()); - if (asyncExecutorService == null) { - ExecutorFactory executorFactory = configuration.asyncExecutorFactory().factory(); - if (executorFactory == null) { - executorFactory = Util.getInstance(configuration.asyncExecutorFactory().factoryClass()); - } - asyncExecutorService = executorFactory.getExecutor(configuration.asyncExecutorFactory().properties()); - } + createExecutorService(); listenerNotifier = ClientListenerNotifier.create(codec, marshaller, transportFactory); transportFactory.start(codec, configuration, defaultCacheTopologyId, listenerNotifier); @@ -209,6 +215,16 @@ public void start() { started = true; } + private void createExecutorService() { + if (asyncExecutorService == null) { + ExecutorFactory executorFactory = configuration.asyncExecutorFactory().factory(); + if (executorFactory == null) { + executorFactory = Util.getInstance(configuration.asyncExecutorFactory().factoryClass()); + } + asyncExecutorService = executorFactory.getExecutor(configuration.asyncExecutorFactory().properties()); + } + } + private final void warnAboutUberJarDuplicates() { UberJarDuplicatedJarsWarner scanner = new ManifestUberJarDuplicatedJarsWarner(); scanner.isClasspathCorrectAsync() diff --git a/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/RemoteCacheManagerTest.java b/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/RemoteCacheManagerTest.java index 41ea6daf5159..cd2bbcbd3632 100644 --- a/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/RemoteCacheManagerTest.java +++ b/client/hotrod-client/src/test/java/org/infinispan/client/hotrod/RemoteCacheManagerTest.java @@ -47,6 +47,15 @@ public void release() { HotRodClientTestingUtil.killRemoteCacheManager(remoteCacheManager); } + public void testStartStopAsync() throws Exception { + remoteCacheManager = new RemoteCacheManager(false); + + remoteCacheManager.startAsync().get(); + assertTrue(remoteCacheManager.isStarted()); + + remoteCacheManager.stopAsync().get(); + assertFalse(remoteCacheManager.isStarted()); + } public void testNoArgConstructor() { remoteCacheManager = new RemoteCacheManager(); assertTrue(remoteCacheManager.isStarted()); @@ -78,12 +87,6 @@ public void testGetUndefinedCache() { assert null == remoteCacheManager.getCache("Undefined1234"); } - private void assertWorks(RemoteCacheManager remoteCacheManager) { - RemoteCache cache = remoteCacheManager.getCache(); - cache.put("aKey", "aValue"); - assert cache.get("aKey").equals("aValue"); - } - public void testMarshallerInstance() { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.addServer().host("127.0.0.1").port(port);