Skip to content

Commit

Permalink
ISPN-7823 RemoteCacheManager start/stop async
Browse files Browse the repository at this point in the history
  • Loading branch information
karesti authored and ryanemerson committed May 22, 2017
1 parent 5b704d4 commit e35dd07
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
Expand Up @@ -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;

Expand Down Expand Up @@ -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<RemoteCacheKey, RemoteCacheHolder> cacheName2RemoteCache = new HashMap<>();
private final AtomicInteger defaultCacheTopologyId = new AtomicInteger(HotRodConstants.DEFAULT_CACHE_TOPOLOGY);
Expand All @@ -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;

/**
*
Expand Down Expand Up @@ -171,6 +173,16 @@ public <K, V> RemoteCache<K, V> getCache(boolean forceReturnValue) {
return createRemoteCache("", forceReturnValue);
}

public CompletableFuture<Void> startAsync() {
createExecutorService();
return CompletableFuture.runAsync(start, asyncExecutorService);
}

public CompletableFuture<Void> stopAsync() {
createExecutorService();
return CompletableFuture.runAsync(stop, asyncExecutorService);
}

@Override
public void start() {
transportFactory = Util.getInstance(configuration.transportFactory());
Expand All @@ -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);
Expand All @@ -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()
Expand Down
Expand Up @@ -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());
Expand Down Expand Up @@ -78,12 +87,6 @@ public void testGetUndefinedCache() {
assert null == remoteCacheManager.getCache("Undefined1234");
}

private void assertWorks(RemoteCacheManager remoteCacheManager) {
RemoteCache<Object, Object> 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);
Expand Down

0 comments on commit e35dd07

Please sign in to comment.