Skip to content

Commit 2a98a10

Browse files
committed
Have a global and thread local observer in QueryObserverHolder
1 parent 2cb85bc commit 2a98a10

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

geode-core/src/main/java/org/apache/geode/cache/query/internal/QueryObserverHolder.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
package org.apache.geode.cache.query.internal;
1616

17+
1718
import org.apache.geode.annotations.Immutable;
1819
import org.apache.geode.annotations.internal.MakeNotStatic;
1920

@@ -45,35 +46,53 @@ public class QueryObserverHolder {
4546
*/
4647
@Immutable
4748
private static final QueryObserver NO_OBSERVER = new QueryObserverAdapter();
49+
/**
50+
* The threadlocal current observer which will be notified of all query events.
51+
*/
52+
private static final ThreadLocal<QueryObserver> _instance = new ThreadLocal<>();
53+
4854
/**
4955
* The current observer which will be notified of all query events.
5056
*/
5157
@MakeNotStatic
52-
private static ThreadLocal<QueryObserver> _instance = ThreadLocal.withInitial(() -> NO_OBSERVER);
58+
private static volatile QueryObserver _globalInstance = NO_OBSERVER;
5359

5460
/**
5561
* Set the given observer to be notified of query events. Returns the current observer.
5662
*/
57-
public static QueryObserver setInstance(QueryObserver observer) {
63+
public static synchronized QueryObserver setInstance(QueryObserver observer) {
5864
Support.assertArg(observer != null, "setInstance expects a non-null argument!");
59-
QueryObserver oldObserver = _instance.get();
65+
QueryObserver oldObserver;
66+
if (_instance.get() != null) {
67+
oldObserver = _instance.get();
68+
} else {
69+
oldObserver = _globalInstance;
70+
}
6071
_instance.set(observer);
72+
_globalInstance = observer;
6173
return oldObserver;
6274
}
6375

64-
public static boolean hasObserver() {
65-
return _instance.get() != NO_OBSERVER;
76+
public static synchronized boolean hasObserver() {
77+
if (_instance.get() != null) {
78+
return _instance.get() != NO_OBSERVER;
79+
}
80+
return _globalInstance != NO_OBSERVER;
6681
}
6782

6883
/** Return the current QueryObserver instance */
6984
public static QueryObserver getInstance() {
70-
return _instance.get();
85+
if (_instance.get() != null) {
86+
return _instance.get();
87+
}
88+
return _globalInstance;
7189
}
7290

7391
/**
7492
* Only for test purposes.
7593
*/
7694
public static void reset() {
7795
_instance.set(NO_OBSERVER);
96+
_globalInstance = NO_OBSERVER;
7897
}
7998
}

0 commit comments

Comments
 (0)