- private static void deleteListener(ListenerAdapter listener) {
- if (listener != null) {
- try {
- listener.close();
- } catch (Exception e) {
- }
- try {
- lock.lock();
- listenersByInstanceIdentifier.remove(listener.getPath());
- listenersByStreamName.remove(listener.getStreamName());
- } finally {
- lock.unlock();
- }
- }
- }
+ /**
+ * Removes all listeners.
+ */
+ public static void removeAllListeners() {
+ for (ListenerAdapter listener : listenersByInstanceIdentifier.values()) {
+ try {
+ listener.close();
+ } catch (Exception e) {
+ }
+ }
+ try {
+ lock.lock();
+ listenersByStreamName = new ConcurrentHashMap<>();
+ listenersByInstanceIdentifier = new ConcurrentHashMap<>();
+ } finally {
+ lock.unlock();
+ }
+ }
+
+ /**
+ * Checks if listener has at least one subscriber. In case it doesn't have any, delete
+ * listener.
+ *
+ * @param listener
+ * ListenerAdapter
+ */
+ public static void removeListenerIfNoSubscriberExists(
+ ListenerAdapter listener) {
+ if (!listener.hasSubscribers()) {
+ deleteListener(listener);
+ }
+ }
+
+ /**
+ * Delete {@link ListenerAdapter} listener specified in parameter.
+ *
+ * @param listener
+ * ListenerAdapter
+ */
+ private static void deleteListener(ListenerAdapter listener) {
+ if (listener != null) {
+ try {
+ listener.close();
+ } catch (Exception e) {
+ }
+ try {
+ lock.lock();
+ listenersByInstanceIdentifier.remove(listener.getPath());
+ listenersByStreamName.remove(listener.getStreamName());
+ } finally {
+ lock.unlock();
+ }
+ }
+ }