Search in sources :

Example 1 with Listener

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();
}
Also used : Listener(org.wildfly.clustering.provider.ServiceProviderRegistration.Listener) ExecutorService(java.util.concurrent.ExecutorService) List(java.util.List) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AbstractMap(java.util.AbstractMap)

Example 2 with Listener

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();
                    }
                }
            }
        }
    });
}
Also used : Listener(org.wildfly.clustering.provider.ServiceProviderRegistration.Listener) HashSet(java.util.HashSet) Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Node(org.wildfly.clustering.group.Node) Batch(org.wildfly.clustering.ee.Batch) ExecutorService(java.util.concurrent.ExecutorService) List(java.util.List) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AbstractMap(java.util.AbstractMap)

Aggregations

AbstractMap (java.util.AbstractMap)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 ExecutorService (java.util.concurrent.ExecutorService)2 Listener (org.wildfly.clustering.provider.ServiceProviderRegistration.Listener)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 Batch (org.wildfly.clustering.ee.Batch)1 Node (org.wildfly.clustering.group.Node)1