You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2023/03/07 13:38:03 UTC

[cxf] branch main updated: [CXF-8745] The ApplicationListener, which redirects all ApplicationEvents to the SpringBus, now uses a WeakReference to allow a GC if the SpringBus is no longer in use because it's corresponding context has been closed. (#1077)

This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/main by this push:
     new 23fdd237d4 [CXF-8745] The ApplicationListener, which redirects all ApplicationEvents to the SpringBus, now uses a WeakReference to allow a GC if the SpringBus is no longer in use because it's corresponding context has been closed. (#1077)
23fdd237d4 is described below

commit 23fdd237d48a05ac1c3518e9bd90165067aadcf0
Author: Eric Giese <er...@googlemail.com>
AuthorDate: Tue Mar 7 14:37:55 2023 +0100

    [CXF-8745] The ApplicationListener, which redirects all ApplicationEvents to the SpringBus, now uses a WeakReference to allow a GC if the SpringBus is no longer in use because it's corresponding context has been closed. (#1077)
---
 .../java/org/apache/cxf/bus/spring/SpringBus.java  | 27 +++++++++++++++++-----
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/core/src/main/java/org/apache/cxf/bus/spring/SpringBus.java b/core/src/main/java/org/apache/cxf/bus/spring/SpringBus.java
index 8966f8dd73..c48ef86814 100644
--- a/core/src/main/java/org/apache/cxf/bus/spring/SpringBus.java
+++ b/core/src/main/java/org/apache/cxf/bus/spring/SpringBus.java
@@ -32,6 +32,8 @@ import org.springframework.context.event.ContextClosedEvent;
 import org.springframework.context.event.ContextRefreshedEvent;
 import org.springframework.context.support.AbstractApplicationContext;
 
+import java.lang.ref.WeakReference;
+
 /**
  *
  */
@@ -60,12 +62,7 @@ public class SpringBus extends ExtensionManagerBus
     /** {@inheritDoc}*/
     public void setApplicationContext(ApplicationContext applicationContext) {
         ctx = (AbstractApplicationContext)applicationContext;
-        @SuppressWarnings("rawtypes")
-        ApplicationListener listener = new ApplicationListener() {
-            public void onApplicationEvent(ApplicationEvent event) {
-                SpringBus.this.onApplicationEvent(event);
-            }
-        };
+        SpringBusApplicationListener listener = new SpringBusApplicationListener(this);
         ctx.addApplicationListener(listener);
         ApplicationContext ac = applicationContext.getParent();
         while (ac != null) {
@@ -152,4 +149,22 @@ public class SpringBus extends ExtensionManagerBus
         closeContext = b;
     }
 
+    private static class SpringBusApplicationListener implements ApplicationListener<ApplicationEvent> {
+
+        // Using a WeakReference ensures that the Listener does not prevent a SpringBus from being free'd
+        // This can happen in parent-child context constellations, where the Listener is recursively added
+        private final WeakReference<SpringBus> springBusReference;
+
+        SpringBusApplicationListener(SpringBus springBus) {
+            this.springBusReference = new WeakReference<>(springBus);
+        }
+
+        @Override
+        public void onApplicationEvent(ApplicationEvent event) {
+            var springBus = springBusReference.get();
+            if (springBus != null) {
+                springBus.onApplicationEvent(event);
+            }
+        }
+    }
 }