Skip to content

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 64fe5c2
Showing 1 changed file with 22 additions and 6 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 @@ -49,31 +50,46 @@ public class QueryObserverHolder {
* The current observer which will be notified of all query events.
*/
@MakeNotStatic
private static ThreadLocal<QueryObserver> _instance = ThreadLocal.withInitial(() -> NO_OBSERVER);
private static final ThreadLocal<QueryObserver> _instance = new ThreadLocal<>();

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) {
public static synchronized 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;
public static synchronized boolean hasObserver() {
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 64fe5c2

Please sign in to comment.