|
14 | 14 | */ |
15 | 15 | package org.apache.geode.cache.query.internal; |
16 | 16 |
|
| 17 | + |
17 | 18 | import org.apache.geode.annotations.Immutable; |
18 | 19 | import org.apache.geode.annotations.internal.MakeNotStatic; |
19 | 20 |
|
@@ -45,35 +46,53 @@ public class QueryObserverHolder { |
45 | 46 | */ |
46 | 47 | @Immutable |
47 | 48 | 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 | + |
48 | 54 | /** |
49 | 55 | * The current observer which will be notified of all query events. |
50 | 56 | */ |
51 | 57 | @MakeNotStatic |
52 | | - private static ThreadLocal<QueryObserver> _instance = ThreadLocal.withInitial(() -> NO_OBSERVER); |
| 58 | + private static volatile QueryObserver _globalInstance = NO_OBSERVER; |
53 | 59 |
|
54 | 60 | /** |
55 | 61 | * Set the given observer to be notified of query events. Returns the current observer. |
56 | 62 | */ |
57 | | - public static QueryObserver setInstance(QueryObserver observer) { |
| 63 | + public static synchronized QueryObserver setInstance(QueryObserver observer) { |
58 | 64 | 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 | + } |
60 | 71 | _instance.set(observer); |
| 72 | + _globalInstance = observer; |
61 | 73 | return oldObserver; |
62 | 74 | } |
63 | 75 |
|
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; |
66 | 81 | } |
67 | 82 |
|
68 | 83 | /** Return the current QueryObserver instance */ |
69 | 84 | public static QueryObserver getInstance() { |
70 | | - return _instance.get(); |
| 85 | + if (_instance.get() != null) { |
| 86 | + return _instance.get(); |
| 87 | + } |
| 88 | + return _globalInstance; |
71 | 89 | } |
72 | 90 |
|
73 | 91 | /** |
74 | 92 | * Only for test purposes. |
75 | 93 | */ |
76 | 94 | public static void reset() { |
77 | 95 | _instance.set(NO_OBSERVER); |
| 96 | + _globalInstance = NO_OBSERVER; |
78 | 97 | } |
79 | 98 | } |
0 commit comments