use of org.wildfly.clustering.provider.ServiceProviderRegistration.Listener in project wildfly by wildfly.
the class CacheServiceProviderRegistry method close.
@Override
public void close() {
this.group.removeListener(this);
this.cache.removeListener(this);
this.dispatcher.close();
// Cleanup any unclosed registrations
for (Map.Entry<Listener, ExecutorService> entry : this.listeners.values()) {
ExecutorService executor = entry.getValue();
if (executor != null) {
PrivilegedAction<List<Runnable>> action = () -> executor.shutdownNow();
WildFlySecurityManager.doUnchecked(action);
}
}
this.listeners.clear();
}
use of org.wildfly.clustering.provider.ServiceProviderRegistration.Listener in project wildfly by wildfly.
the class CacheServiceProviderRegistry method register.
@Override
public ServiceProviderRegistration<T> register(T service, Listener listener) {
Map.Entry<Listener, ExecutorService> newEntry = new AbstractMap.SimpleEntry<>(listener, null);
// Only create executor for new registrations
Map.Entry<Listener, ExecutorService> entry = this.listeners.computeIfAbsent(service, key -> {
if (listener != null) {
newEntry.setValue(Executors.newSingleThreadExecutor(createThreadFactory(listener.getClass())));
}
return newEntry;
});
if (entry != newEntry) {
throw new IllegalArgumentException(service.toString());
}
try (Batch batch = this.batcher.createBatch()) {
this.register(this.group.getLocalNode(), service);
}
return new SimpleServiceProviderRegistration<>(service, this, () -> {
Node node = this.getGroup().getLocalNode();
try (Batch batch = this.batcher.createBatch()) {
Set<Node> nodes = this.cache.getAdvancedCache().withFlags(Flag.FORCE_WRITE_LOCK).get(service);
if ((nodes != null) && nodes.remove(node)) {
Cache<T, Set<Node>> cache = this.cache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES);
if (nodes.isEmpty()) {
cache.remove(service);
} else {
cache.replace(service, nodes);
}
}
} finally {
Map.Entry<Listener, ExecutorService> oldEntry = this.listeners.remove(service);
if (oldEntry != null) {
ExecutorService executor = oldEntry.getValue();
if (executor != null) {
PrivilegedAction<List<Runnable>> action = () -> executor.shutdownNow();
WildFlySecurityManager.doUnchecked(action);
try {
executor.awaitTermination(this.cache.getCacheConfiguration().transaction().cacheStopTimeout(), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
});
}
Aggregations