com.infomaximum.cluster.graphql.executor.subscription.GraphQLSubscribeEngineImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cluster-graphql Show documentation
Show all versions of cluster-graphql Show documentation
Library for creating a light cluster
The newest version!
package com.infomaximum.cluster.graphql.executor.subscription;
import com.infomaximum.cluster.graphql.struct.subscribe.SubscribeKey;
import io.reactivex.ObservableEmitter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArraySet;
public class GraphQLSubscribeEngineImpl implements GraphQLSubscribeEngine {
protected final static Logger log = LoggerFactory.getLogger(GraphQLSubscribeEngineImpl.class);
private final ConcurrentMap> subscriber;
public GraphQLSubscribeEngineImpl() {
this.subscriber = new ConcurrentHashMap<>();
}
public void pushEvent(SubscribeKey subscribeKey, Optional extends Serializable> value) {
CopyOnWriteArraySet observables = subscriber.get(subscribeKey);
if (observables == null || observables.isEmpty()) return;
for (ObservableEmitter emitter : observables) {
emitter.onNext(value);
}
}
public void subscribe(UUID nodeRuntimeId, int componentId, byte[] bSubscribeKey, ObservableEmitter observable) {
SubscribeKey subscribeKey = new SubscribeKey(nodeRuntimeId, componentId, bSubscribeKey);
subscribe(subscribeKey, observable);
}
private void subscribe(SubscribeKey subscribeKey, ObservableEmitter observable) {
CopyOnWriteArraySet observables = subscriber.computeIfAbsent(subscribeKey, s -> new CopyOnWriteArraySet());
observables.add(observable);
//Подписываемся на разрыв соединения и отписку
observable.setCancellable(() -> {
unSubscribe(subscribeKey, observable);
});
}
private void unSubscribe(SubscribeKey subscribeKey, ObservableEmitter observable) {
CopyOnWriteArraySet observables = subscriber.get(subscribeKey);
observables.remove(observable);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy