Skip to content
This repository has been archived by the owner on Mar 31, 2023. It is now read-only.

Commit

Permalink
Have a global and thread local observer in QueryObserverHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
albertogpz committed Sep 20, 2021
1 parent 2cb85bc commit 141e30f
Showing 1 changed file with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.apache.geode.cache.query.internal;


import org.apache.geode.annotations.Immutable;
import org.apache.geode.annotations.internal.MakeNotStatic;

Expand Down Expand Up @@ -45,35 +46,53 @@ public class QueryObserverHolder {
*/
@Immutable
private static final QueryObserver NO_OBSERVER = new QueryObserverAdapter();
/**
* The threadlocal current observer which will be notified of all query events.
*/
private static final ThreadLocal<QueryObserver> _instance = new ThreadLocal<>();

/**
* The current observer which will be notified of all query events.
*/
@MakeNotStatic
private static ThreadLocal<QueryObserver> _instance = ThreadLocal.withInitial(() -> NO_OBSERVER);
private static volatile QueryObserver _globalInstance = NO_OBSERVER;

/**
* Set the given observer to be notified of query events. Returns the current observer.
*/
public static QueryObserver setInstance(QueryObserver observer) {
Support.assertArg(observer != null, "setInstance expects a non-null argument!");
QueryObserver oldObserver = _instance.get();
QueryObserver oldObserver;
if (_instance.get() != null) {
oldObserver = _instance.get();
} else {
oldObserver = _globalInstance;
}
_instance.set(observer);
_globalInstance = observer;
return oldObserver;
}

public static boolean hasObserver() {
return _instance.get() != NO_OBSERVER;
if (_instance.get() != null) {
return _instance.get() != NO_OBSERVER;
}
return _globalInstance != NO_OBSERVER;
}

/** Return the current QueryObserver instance */
public static QueryObserver getInstance() {
return _instance.get();
if (_instance.get() != null) {
return _instance.get();
}
return _globalInstance;
}

/**
* Only for test purposes.
*/
public static void reset() {
_instance.set(NO_OBSERVER);
_globalInstance = NO_OBSERVER;
}
}

0 comments on commit 141e30f

Please sign in to comment.