@Override
public ServiceProviderRegistration createRegistration(final Object service, Listener listener) {
if (this.listeners.putIfAbsent(service, listener) != null) {
throw new IllegalArgumentException(service.toString());
}
final Node node = this.group.getLocalNode();
Set<Node> nodes = new HashSet<>(Collections.singleton(node));
try (Batch batch = this.batcher.createBatch()) {
Set<Node> existing = this.cache.getAdvancedCache().withFlags(Flag.FORCE_SYNCHRONOUS).putIfAbsent(service, nodes);
if (existing != null) {
if (existing.add(node)) {
this.cache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).replace(service, existing);
}
}
}
return new AbstractServiceProviderRegistration(service, this) {
@Override
public void close() {
if (CacheServiceProviderRegistrationFactory.this.listeners.remove(service) != null) {
final Node node = CacheServiceProviderRegistrationFactory.this.getGroup().getLocalNode();
try (Batch batch = CacheServiceProviderRegistrationFactory.this.batcher.createBatch()) {
Set<Node> nodes = CacheServiceProviderRegistrationFactory.this.cache.get(service);
if ((nodes != null) && nodes.remove(node)) {
Cache<Object, Set<Node>> cache = CacheServiceProviderRegistrationFactory.this.cache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES);
if (nodes.isEmpty()) {