|
35 | 35 | import org.springframework.pulsar.core.TopicResolver;
|
36 | 36 | import org.springframework.util.Assert;
|
37 | 37 | import org.springframework.util.CollectionUtils;
|
| 38 | +import org.springframework.util.ReflectionUtils; |
38 | 39 |
|
39 | 40 | /**
|
40 | 41 | * Default implementation of {@link ReactivePulsarSenderFactory}.
|
@@ -172,13 +173,44 @@ public LogAccessor logger() {
|
172 | 173 | @Override
|
173 | 174 | public void doStop() {
|
174 | 175 | try {
|
| 176 | + this.reflectivelyClearCache(); |
175 | 177 | this.reactiveMessageSenderCache.close();
|
| 178 | + |
176 | 179 | }
|
177 | 180 | catch (Exception e) {
|
178 | 181 | throw new RuntimeException(e);
|
179 | 182 | }
|
180 | 183 | }
|
181 | 184 |
|
| 185 | + /** |
| 186 | + * Workaround to reflectively clear the underlying producer cache. |
| 187 | + * |
| 188 | + * TODO: Remove once this is supported in the Reactive client. |
| 189 | + */ |
| 190 | + private void reflectivelyClearCache() { |
| 191 | + // reactiveMessageSenderCache |
| 192 | + // (org.apache.pulsar.reactive.client.internal.adapter.ProducerCache) |
| 193 | + var cacheProviderField = ReflectionUtils.findField(this.reactiveMessageSenderCache.getClass(), "cacheProvider"); |
| 194 | + ReflectionUtils.makeAccessible(cacheProviderField); |
| 195 | + |
| 196 | + // org.apache.pulsar.reactive.client.producercache.CaffeineShadedProducerCacheProvider |
| 197 | + var cacheProvider = ReflectionUtils.getField(cacheProviderField, this.reactiveMessageSenderCache); |
| 198 | + |
| 199 | + // org.apache.pulsar.reactive.shade.com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalAsyncCache |
| 200 | + var cacheField = ReflectionUtils.findField(cacheProvider.getClass(), "cache"); |
| 201 | + ReflectionUtils.makeAccessible(cacheField); |
| 202 | + var cache = ReflectionUtils.getField(cacheField, cacheProvider); |
| 203 | + |
| 204 | + // org.apache.pulsar.reactive.shade.com.github.benmanes.caffeine.cache.SSLMSAW |
| 205 | + var actualCacheField = ReflectionUtils.findField(cache.getClass(), "cache"); |
| 206 | + ReflectionUtils.makeAccessible(actualCacheField); |
| 207 | + var actualCache = ReflectionUtils.getField(actualCacheField, cache); |
| 208 | + |
| 209 | + var clearMethod = ReflectionUtils.findMethod(actualCache.getClass(), "clear"); |
| 210 | + ReflectionUtils.makeAccessible(clearMethod); |
| 211 | + ReflectionUtils.invokeMethod(clearMethod, actualCache); |
| 212 | + } |
| 213 | + |
182 | 214 | /**
|
183 | 215 | * Builder for {@link DefaultReactivePulsarSenderFactory}.
|
184 | 216 | *
|
|
0 commit comments